Do what clippy says
Getty Ritter
4 years ago
4 | 4 | pub const BG_COLOR: (f64, f64, f64) = (0.1, 0.1, 0.1); |
5 | 5 | pub const FG_COLOR: (f64, f64, f64) = (1.0, 1.0, 1.0); |
6 | 6 | |
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"; | |
9 | 9 | } |
10 | 10 | |
11 | 11 | pub struct Config { |
49 | 49 | height: 0, |
50 | 50 | buffer: 0, |
51 | 51 | }; |
52 |
let table = input.as_table().ok_or |
|
52 | let table = input.as_table().ok_or_else(|| format_err!("invalid config"))?; | |
53 | 53 | let widgets = &table["widgets"]; |
54 | 54 | 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 |
|
|
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"))? { | |
58 | 58 | "box" => target.push(Box::new(w::SmallBox)), |
59 | 59 | "battery" => target.push(Box::new(w::Battery::new()?)), |
60 | 60 | "caesura" => target.push(Box::new(w::Caesura)), |
69 | 69 | conf.bg_color = color_from_hex( |
70 | 70 | color |
71 | 71 | .as_str() |
72 |
.ok_or |
|
72 | .ok_or_else(|| format_err!("`background` not a str"))?, | |
73 | 73 | )?; |
74 | 74 | } |
75 | 75 | if let Some(color) = table.get("foreground") { |
76 | 76 | conf.fg_color = color_from_hex( |
77 | 77 | color |
78 | 78 | .as_str() |
79 |
.ok_or |
|
79 | .ok_or_else(|| format_err!("`foreground` not a str"))?, | |
80 | 80 | )?; |
81 | 81 | } |
82 | 82 | if let Some(font) = table.get("font") { |
83 | 83 | conf.font = font |
84 | 84 | .as_str() |
85 |
.ok_or |
|
85 | .ok_or_else(|| format_err!("`font` not a str"))? | |
86 | 86 | .to_string(); |
87 | 87 | } |
88 | 88 | conf.right.reverse(); |
129 | 129 | |
130 | 130 | // set up a struct with everything that widgets need to draw |
131 | 131 | let d = w::Drawing { |
132 |
ctx |
|
132 | ctx, | |
133 | 133 | lyt: &layout, |
134 | 134 | size, |
135 | 135 | stdin, |
54 | 54 | // To begin with, our left-hand side---which normally is whatever |
55 | 55 | // was last passed in on stdin---will start as a generic |
56 | 56 | // message... |
57 |
let mut input = |
|
57 | let mut input = "Loading...".to_string(); | |
58 | 58 | // And let's get a buffered stdin handle now |
59 | 59 | let mut stdin = std::io::BufReader::new(std::io::stdin()); |
60 | 60 | |
72 | 72 | let ctx = cairo::Context::new(&surf); |
73 | 73 | |
74 | 74 | let layout = pangocairo::functions::create_layout(&ctx) |
75 |
.ok_or |
|
75 | .ok_or_else(|| format_err!("unable to create layout"))?; | |
76 | 76 | |
77 | 77 | // allow for the whole width of the bar, minus a small fixed amount |
78 | 78 | layout.set_width((w.width - 20) * pango::SCALE); |
119 | 119 | use std::io::BufRead; |
120 | 120 | input = String::new(); |
121 | 121 | stdin.read_line(&mut input)?; |
122 |
if input. |
|
122 | if input.is_empty() { | |
123 | 123 | break; |
124 | 124 | } |
125 | 125 | for (ctx, layout, sz) in ctxs.iter() { |
15 | 15 | } |
16 | 16 | |
17 | 17 | impl Located { |
18 |
fn draw_text( |
|
18 | fn draw_text(self, d: &Drawing, msg: &str) -> i32 { | |
19 | 19 | d.lyt.set_text(msg); |
20 | 20 | let (w, _) = d.lyt.get_size(); |
21 | 21 | d.ctx.move_to(self.target_x(d, w / pango::SCALE), d.buffer); |
23 | 23 | w / pango::SCALE |
24 | 24 | } |
25 | 25 | |
26 |
fn target_x( |
|
26 | fn target_x(self, d: &Drawing, w: i32) -> f64 { | |
27 | 27 | match self { |
28 |
Located::FromLeft(x) => |
|
28 | Located::FromLeft(x) => x as f64, | |
29 | 29 | Located::FromRight(x) => (d.size.wd - (x + w)) as f64, |
30 | 30 | } |
31 | 31 | } |
51 | 51 | impl Time { |
52 | 52 | pub fn new() -> Time { |
53 | 53 | Time { |
54 |
fmt: |
|
54 | fmt: "%a %b %d %H:%M".to_string(), | |
55 | 55 | } |
56 | 56 | } |
57 | 57 | } |
39 | 39 | let si = screen_info |
40 | 40 | .offset(i as isize) |
41 | 41 | .as_ref() |
42 |
.ok_or |
|
42 | .ok_or_else(|| format_err!("bad pointer"))?; | |
43 | 43 | widths.push((si.x_org as i32, si.width as i32)); |
44 | 44 | } |
45 | 45 | } |
268 | 268 | xlib::GenericEvent => { |
269 | 269 | let mut cookie: xlib::XGenericEventCookie = unsafe { From::from(*e.as_ptr()) }; |
270 | 270 | 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 | }); | |
280 | 277 | } |
281 | 278 | } |
282 | 279 | _ => (), |
322 | 319 | w: &mut Window, |
323 | 320 | f: impl FnOnce(&mut Window, u64, *const u8), |
324 | 321 | ) -> Result<(), failure::Error> { |
325 |
f(w, xlib::XA_CARDINAL, |
|
322 | f(w, xlib::XA_CARDINAL, xs.as_ptr() as *const u8); | |
326 | 323 | Ok(()) |
327 | 324 | } |
328 | 325 | } |
334 | 331 | f: impl FnOnce(&mut Window, u64, *const u8), |
335 | 332 | ) -> Result<(), failure::Error> { |
336 | 333 | let xs: Result<Vec<u64>, failure::Error> = xs.iter().map(|s| w.intern(s)).collect(); |
337 |
f(w, xlib::XA_ATOM, |
|
334 | f(w, xlib::XA_ATOM, xs?.as_ptr() as *const u8); | |
338 | 335 | Ok(()) |
339 | 336 | } |
340 | 337 | } |