gdritter repos knurling / 232d33a
Do what clippy says Getty Ritter 4 years ago
4 changed file(s) with 26 addition(s) and 29 deletion(s). Collapse all Expand all
44 pub const BG_COLOR: (f64, f64, f64) = (0.1, 0.1, 0.1);
55 pub const FG_COLOR: (f64, f64, f64) = (1.0, 1.0, 1.0);
66
7 pub const FONT_FAMILY: &'static str = "Fira Mono";
8 pub const FONT_SIZE: &'static str = "18";
7 pub const FONT_FAMILY: &str = "Fira Mono";
8 pub const FONT_SIZE: &str = "18";
99 }
1010
1111 pub struct Config {
4949 height: 0,
5050 buffer: 0,
5151 };
52 let table = input.as_table().ok_or(format_err!("invalid config"))?;
52 let table = input.as_table().ok_or_else(|| format_err!("invalid config"))?;
5353 let widgets = &table["widgets"];
5454 let mut target = &mut conf.left;
55 for section in widgets.as_array().ok_or(format_err!("invalid config"))? {
56 let section = section.as_table().ok_or(format_err!("invalid config"))?;
57 match section["name"].as_str().ok_or(format_err!(""))? {
55 for section in widgets.as_array().ok_or_else(|| format_err!("invalid config"))? {
56 let section = section.as_table().ok_or_else(|| format_err!("invalid config"))?;
57 match section["name"].as_str().ok_or_else(|| format_err!("invalid config"))? {
5858 "box" => target.push(Box::new(w::SmallBox)),
5959 "battery" => target.push(Box::new(w::Battery::new()?)),
6060 "caesura" => target.push(Box::new(w::Caesura)),
6969 conf.bg_color = color_from_hex(
7070 color
7171 .as_str()
72 .ok_or(format_err!("`background` not a str"))?,
72 .ok_or_else(|| format_err!("`background` not a str"))?,
7373 )?;
7474 }
7575 if let Some(color) = table.get("foreground") {
7676 conf.fg_color = color_from_hex(
7777 color
7878 .as_str()
79 .ok_or(format_err!("`foreground` not a str"))?,
79 .ok_or_else(|| format_err!("`foreground` not a str"))?,
8080 )?;
8181 }
8282 if let Some(font) = table.get("font") {
8383 conf.font = font
8484 .as_str()
85 .ok_or(format_err!("`font` not a str"))?
85 .ok_or_else(|| format_err!("`font` not a str"))?
8686 .to_string();
8787 }
8888 conf.right.reverse();
129129
130130 // set up a struct with everything that widgets need to draw
131131 let d = w::Drawing {
132 ctx: ctx,
132 ctx,
133133 lyt: &layout,
134134 size,
135135 stdin,
5454 // To begin with, our left-hand side---which normally is whatever
5555 // was last passed in on stdin---will start as a generic
5656 // message...
57 let mut input = format!("Loading...");
57 let mut input = "Loading...".to_string();
5858 // And let's get a buffered stdin handle now
5959 let mut stdin = std::io::BufReader::new(std::io::stdin());
6060
7272 let ctx = cairo::Context::new(&surf);
7373
7474 let layout = pangocairo::functions::create_layout(&ctx)
75 .ok_or(format_err!("unable to create layout"))?;
75 .ok_or_else(|| format_err!("unable to create layout"))?;
7676
7777 // allow for the whole width of the bar, minus a small fixed amount
7878 layout.set_width((w.width - 20) * pango::SCALE);
119119 use std::io::BufRead;
120120 input = String::new();
121121 stdin.read_line(&mut input)?;
122 if input.len() == 0 {
122 if input.is_empty() {
123123 break;
124124 }
125125 for (ctx, layout, sz) in ctxs.iter() {
1515 }
1616
1717 impl Located {
18 fn draw_text(&self, d: &Drawing, msg: &str) -> i32 {
18 fn draw_text(self, d: &Drawing, msg: &str) -> i32 {
1919 d.lyt.set_text(msg);
2020 let (w, _) = d.lyt.get_size();
2121 d.ctx.move_to(self.target_x(d, w / pango::SCALE), d.buffer);
2323 w / pango::SCALE
2424 }
2525
26 fn target_x(&self, d: &Drawing, w: i32) -> f64 {
26 fn target_x(self, d: &Drawing, w: i32) -> f64 {
2727 match self {
28 Located::FromLeft(x) => *x as f64,
28 Located::FromLeft(x) => x as f64,
2929 Located::FromRight(x) => (d.size.wd - (x + w)) as f64,
3030 }
3131 }
5151 impl Time {
5252 pub fn new() -> Time {
5353 Time {
54 fmt: format!("%a %b %d %H:%M"),
54 fmt: "%a %b %d %H:%M".to_string(),
5555 }
5656 }
5757 }
3939 let si = screen_info
4040 .offset(i as isize)
4141 .as_ref()
42 .ok_or(format_err!("bad pointer"))?;
42 .ok_or_else(|| format_err!("bad pointer"))?;
4343 widths.push((si.x_org as i32, si.width as i32));
4444 }
4545 }
268268 xlib::GenericEvent => {
269269 let mut cookie: xlib::XGenericEventCookie = unsafe { From::from(*e.as_ptr()) };
270270 unsafe { xlib::XGetEventData(self.display.display, &mut cookie) };
271 match cookie.evtype {
272 xinput2::XI_ButtonPress => {
273 let data: &xinput2::XIDeviceEvent = unsafe { mem::transmute(cookie.data) };
274 return Some(Event::MouseEvent {
275 x: data.event_x,
276 y: data.event_y,
277 });
278 }
279 _ => (),
271 if let xinput2::XI_ButtonPress = cookie.evtype {
272 let data: &xinput2::XIDeviceEvent = unsafe { &*(cookie.data as *const xinput2::XIDeviceEvent) };
273 return Some(Event::MouseEvent {
274 x: data.event_x,
275 y: data.event_y,
276 });
280277 }
281278 }
282279 _ => (),
322319 w: &mut Window,
323320 f: impl FnOnce(&mut Window, u64, *const u8),
324321 ) -> Result<(), failure::Error> {
325 f(w, xlib::XA_CARDINAL, unsafe { mem::transmute(xs.as_ptr()) });
322 f(w, xlib::XA_CARDINAL, xs.as_ptr() as *const u8);
326323 Ok(())
327324 }
328325 }
334331 f: impl FnOnce(&mut Window, u64, *const u8),
335332 ) -> Result<(), failure::Error> {
336333 let xs: Result<Vec<u64>, failure::Error> = xs.iter().map(|s| w.intern(s)).collect();
337 f(w, xlib::XA_ATOM, unsafe { mem::transmute(xs?.as_ptr()) });
334 f(w, xlib::XA_ATOM, xs?.as_ptr() as *const u8);
338335 Ok(())
339336 }
340337 }