-
Notifications
You must be signed in to change notification settings - Fork 4
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks solid overall, I went and picked a lot of small nits.
src/main.rs
Outdated
"stretch" => BackgroundMode::Stretch, | ||
"center" => BackgroundMode::Center, | ||
"tile" => BackgroundMode::Tile, | ||
_ => BackgroundMode::Fill, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file seems to have a preference for .expect()
and ::std::process::exit(1);
so far.
Does it make sense to do that here as well? Return a Result<BackgroundMode>
and let the caller call expect
on it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I agree with @christophebiocca, we should be returning a Result
here that fails if it's not one of the options.
src/main.rs
Outdated
} | ||
|
||
impl BackgroundMode { | ||
fn from_str(s: &str) -> BackgroundMode { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider implementing FromStr
here, as it is a standard trait for this kind of task.
src/main.rs
Outdated
@@ -80,7 +112,12 @@ fn main() { | |||
let color = Color::from_u32(color); | |||
generate_solid_background(color, &mut background_surface, &env) | |||
} else { | |||
generate_image_background(input.as_str(), &mut background_surface, &env) | |||
let mode = if args.len() >= 3 { | |||
BackgroundMode::from_str(&args[2]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you implement FromStr
above, you can then use args[2].parse()
here.
src/main.rs
Outdated
} | ||
} | ||
} | ||
|
||
fn main() { | ||
let args: Vec<_> = env::args().collect(); | ||
if args.len() < 1 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that there's more than one argument, it might be worth it to call out to an argument parsing library, otherwise users will have to remember which order arguments go in.
As a bonus, I think at least clap
(and maybe others) know how to automate the parsing of arguments that implement FromStr
which means a nicer, human friendly message if the wrong type of scaling mode is passed in.
src/main.rs
Outdated
|
||
// Mode image processing | ||
// The output must be scr_width x scr_height resolution | ||
image = match mode { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of these reassignments could be replaced by rebinding (using let image =
a second time). This is something that Rust lets you do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So shadowing is preferred than mutable, right?
src/main.rs
Outdated
}, | ||
// TODO: Padding background color | ||
BackgroundMode::Center => { | ||
// FIXME: How if image bigger than screen size? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To handle that case, you'd want to use sub_image
.
It seems possible to combine all 4 cases (<W,<H), (>W,<H), (<W,>H), (>W, >H)
by using sub_image
and copy_from
with the appropriate mix of max(difference,0)
and max(-difference,0)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll try. This is interesting problem to solve..
src/main.rs
Outdated
imagepad | ||
}, | ||
BackgroundMode::Tile => { | ||
let repeat_x_count: u32 = ((scr_width / img_width) as f64).ceil() as u32 + 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think ((scr_width / img_width) as f64).ceil() as u32
doesn't do a ton here:
scr_width / img_width
truncates- casting it to
f64
works but it's already too late, it's truncated. .ceil()
is then guaranteed to do nothing.
I think ((scr_width as f64) / (img_width as f64)).ceil() as u32
is what you want? Then the + 1
at the end can safely be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lol to myself: It's truncated! 😆
Thanks @christophebiocca
src/main.rs
Outdated
@@ -10,6 +10,7 @@ extern crate dbus; | |||
use std::env; | |||
use std::process::exit; | |||
use image::{load_from_memory, open}; | |||
use image::{GenericImage, DynamicImage, FilterType}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might as well combine this use statement with the one above it (which is fetching from the same crate/module)
Mutable variable used on more limited scope.
I added many things:
Any feedback and review welcome.. |
Excellent, I'll take another look. If there's anything major I'll bring it up, otherwise I'll merge it in, bump the release to 0.2.0, and push to crates.io (and update the install script on the website |
Thanks again for you hard work! I'll update your listing on the contributors list in the main repo to include this. |
And thanks to @christophebiocca for pitching in and reviewing the code! Nice job catching that floating-point conversion/truncation error. |
Implement background modes following other desktop environment (Windows, MacOS, etc). Feedback is welcome, since this is not optimized yet. Plus, I'm not very well on describing neither stride nor pool.
This will change the arguments into