emulsion
emulsion copied to clipboard
Allow piping command to emulsion
I mentioned this in #64 as well.
Allowing to pipe arbitrary commands that return binary image data to emulsion would open up all sorts of new possibilities. The most obvious one is networking:
curl http://some_url | emulsion
#12 proposes to integrate this functionality in emulsion, but the above works without the networking
feature. Also only the above solution profits from the myriad of configuration options curl
provides, e.g.
curl http://some_url -u user:password | emulsion # request with http basic auth
There are countless other possibilities with this. As another example, you could display a file over SSH, without having to download it.
Implementation
If emulsion is invoked with a path, it should open that file. Otherwise, it should attempt to read an image as binary data from stdin
.
This means that emulsion will hang forever if no file is provided and stdin
is empty. A workaround is to only read from stdin
if it's not a terminal, and show an error otherwise.
Proof-of-concept implementation with the atty crate:
if !atty::is(atty::Stream::Stdin) {
let mut buf = Vec::<u8>::new();
let mut stdin = std::io::stdin();
stdin.read_to_end(&mut buf).unwrap();
dbg!(buf.len());
} else {
eprintln!("Error: No image file provided");
return;
}
See output
$ emulsion
Error: No image file provided.
$ curl http://some_url | emulsion
[src/main.rs:71] buf.len() = 13987
$ emulsion < ~/Pictures/picture.jpg
[src/main.rs:71] buf.len() = 260991
$ curl invalid_url | emulsion
curl: (6) Could not resolve host: invalid_url
[src/main.rs:71] buf.len() = 0
$ echo "Hello world" | emulsion
[src/main.rs:71] buf.len() = 12
Are there any downsides to this?
I think mainly the reason why I haven't responded to this yet is that the scenarios described, namely fetching the image with curl or through ssh are not seomthing that I have ever done so I have a hard time imagining a real word scenario where this would be useful.
But considering that this is relatively straightforward to implement in a way that doesn't really affect the other aspects of the program I'm not opposed to it. If there's a PR I'll consider merging it.