From b2a9475e4c77a9b2028220c2dec2cae2b2285f08 Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Thu, 12 Jan 2023 00:13:27 +0100 Subject: [PATCH] cargo fmt --- src/keyboard/keysyms.rs | 2 +- src/main.rs | 25 +++++--- src/widgets/backlight.rs | 9 +-- src/widgets/bar_widget.rs | 6 +- src/widgets/battery.rs | 129 ++++++++++++++++++++------------------ src/widgets/launcher.rs | 128 +++++++++++++++++++++---------------- src/widgets/mod.rs | 4 +- src/widgets/widget.rs | 9 +-- 8 files changed, 170 insertions(+), 142 deletions(-) diff --git a/src/keyboard/keysyms.rs b/src/keyboard/keysyms.rs index bbde9a2..898303f 100644 --- a/src/keyboard/keysyms.rs +++ b/src/keyboard/keysyms.rs @@ -16,5 +16,5 @@ pub const XKB_KEY_End: u32 = 0xff57; /* EOL */ pub const XKB_KEY_a: u32 = 0x0061; /* U+0061 LATIN SMALL LETTER A */ pub const XKB_KEY_e: u32 = 0x0065; /* U+0065 LATIN SMALL LETTER E */ //pub const XKB_KEY_c: u32 = 0x0063; /* U+0063 LATIN SMALL LETTER C */ -pub const XKB_KEY_k: u32 = 0x006b; /* U+006B LATIN SMALL LETTER K */ +pub const XKB_KEY_k: u32 = 0x006b; /* U+006B LATIN SMALL LETTER K */ pub const XKB_KEY_u: u32 = 0x0075; /* U+0075 LATIN SMALL LETTER U */ diff --git a/src/main.rs b/src/main.rs index ed77192..b33d0a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,11 +30,11 @@ use color::Color; use fonts::FontMap; use keyboard::Keyboard; use widgets::{ - Battery, Backlight, Clock, Date, Geometry, HorizontalLayout, IndexedLayout, Interface, Layout, Line, - Margin, VerticalLayout, Widget, WidgetUpdater, + Backlight, Battery, Clock, Date, Geometry, HorizontalLayout, IndexedLayout, Interface, Layout, + Line, Margin, VerticalLayout, Widget, WidgetUpdater, }; -use std::{cell::RefCell, env, mem, rc::Rc, thread, process::exit}; +use std::{cell::RefCell, env, mem, process::exit, rc::Rc, thread}; enum MaybeFontMap { Waiting(thread::JoinHandle), @@ -143,7 +143,7 @@ fn main() { let bufmgr = BufferManager { buffers: Vec::new(), }; - + let (ping_sender, ping_source) = calloop::ping::make_ping().unwrap(); let clock = Box::new(Clock::new()); @@ -212,7 +212,7 @@ fn main() { for _ in 0..state.widgets.len() { damage.push(Geometry::new()); } - + while state.running { event_loop .dispatch(None, &mut state) @@ -461,11 +461,19 @@ impl Dispatch for State { _: &QueueHandle, ) { match event { - xdg_toplevel::Event::Configure { width, height, states, .. } => { + xdg_toplevel::Event::Configure { + width, + height, + states, + .. + } => { if (width, height) == (0, 0) { return; } - let activated = states.iter().find(|&st| *st as u32 == xdg_toplevel::State::Activated as u32).is_some(); + let activated = states + .iter() + .find(|&st| *st as u32 == xdg_toplevel::State::Activated as u32) + .is_some(); if activated { state.activated = true; @@ -473,7 +481,6 @@ impl Dispatch for State { exit(0); } - if state.dimensions != (width, height) { state.bufmgr.clear_buffers(); state.dimensions = (width, height); @@ -486,7 +493,7 @@ impl Dispatch for State { widget: Box::new(IndexedLayout { widget_idx: 1 }), margin: (16, 8, 0, 0), }), - Box::new(VerticalLayout{ + Box::new(VerticalLayout { widgets: vec![ Box::new(Margin { widget: Box::new(IndexedLayout { widget_idx: 2 }), diff --git a/src/widgets/backlight.rs b/src/widgets/backlight.rs index b972ee4..b046d6b 100644 --- a/src/widgets/backlight.rs +++ b/src/widgets/backlight.rs @@ -1,15 +1,12 @@ use std::{ fs, - io::{ErrorKind, Error}, + io::{Error, ErrorKind}, path::{Path, PathBuf}, }; use crate::{ color::Color, - widgets::bar_widget::{ - BarWidget, - BarWidgetImpl, - }, + widgets::bar_widget::{BarWidget, BarWidgetImpl}, }; fn read_file_as_u64(path: &Path) -> Result { @@ -33,7 +30,7 @@ impl Backlight { } pub fn new(path: &str) -> BarWidget { - let mut dev = Backlight{ + let mut dev = Backlight { device_path: Path::new("/sys/class/backlight").to_path_buf().join(path), cur: 0, max: 0, diff --git a/src/widgets/bar_widget.rs b/src/widgets/bar_widget.rs index f281d9c..77e73b7 100644 --- a/src/widgets/bar_widget.rs +++ b/src/widgets/bar_widget.rs @@ -1,7 +1,4 @@ -use std::{ - cmp::min, - default::Default, -}; +use std::{cmp::min, default::Default}; use crate::{ buffer::BufferView, @@ -94,4 +91,3 @@ impl Widget for BarWidget { self.geometry } } - diff --git a/src/widgets/battery.rs b/src/widgets/battery.rs index 862df51..776a9ad 100644 --- a/src/widgets/battery.rs +++ b/src/widgets/battery.rs @@ -1,25 +1,19 @@ use std::{ + sync::{Arc, Mutex}, thread, time::Duration, - sync::{Arc, Mutex}, }; use calloop::ping::Ping; use dbus::blocking::{ + stdintf::org_freedesktop_dbus::{Properties, PropertiesPropertiesChanged}, LocalConnection, - stdintf::org_freedesktop_dbus::{ - Properties, - PropertiesPropertiesChanged, - }, }; use crate::{ color::Color, - widgets::bar_widget::{ - BarWidget, - BarWidgetImpl, - }, + widgets::bar_widget::{BarWidget, BarWidgetImpl}, }; enum UpowerBatteryState { @@ -40,59 +34,75 @@ fn start_monitor(inner: Arc>, ping: Ping) { thread::Builder::new() .name("battmon".to_string()) .spawn(move || { - let conn = LocalConnection::new_system().unwrap(); - let device_path = "/org/freedesktop/UPower/devices/DisplayDevice"; - let proxy = conn.with_proxy("org.freedesktop.UPower", device_path, Duration::from_millis(500)); + let conn = LocalConnection::new_system().unwrap(); + let device_path = "/org/freedesktop/UPower/devices/DisplayDevice"; + let proxy = conn.with_proxy( + "org.freedesktop.UPower", + device_path, + Duration::from_millis(500), + ); - let capacity: f64 = proxy.get("org.freedesktop.UPower.Device", "Percentage").expect("unable to get property"); - let state: u32 = proxy.get("org.freedesktop.UPower.Device", "State").expect("unable to get state"); + let capacity: f64 = proxy + .get("org.freedesktop.UPower.Device", "Percentage") + .expect("unable to get property"); + let state: u32 = proxy + .get("org.freedesktop.UPower.Device", "State") + .expect("unable to get state"); - { - let mut inner = inner.lock().unwrap(); - inner.value = capacity as f32 / 100.; - inner.state = match state { - 1 => UpowerBatteryState::Charging, - 2 => UpowerBatteryState::Discharging, - 3 => UpowerBatteryState::Empty, - 4 => UpowerBatteryState::Full, - 5 => UpowerBatteryState::NotCharging, - 6 => UpowerBatteryState::Discharging, - _ => UpowerBatteryState::Unknown, - } - } - - - proxy.match_signal(move |c: PropertiesPropertiesChanged, _: &LocalConnection, _: &dbus::Message| { - if c.interface_name != "org.freedesktop.UPower.Device" { - return true; - } - let mut inner = inner.lock().unwrap(); - for (key, value) in c.changed_properties { - match key.as_str() { - "State" => { - inner.state = match value.0.as_u64().unwrap() { - 1 => UpowerBatteryState::Charging, - 2 => UpowerBatteryState::Discharging, - 3 => UpowerBatteryState::Empty, - 4 => UpowerBatteryState::Full, - 5 => UpowerBatteryState::NotCharging, - 6 => UpowerBatteryState::Discharging, - _ => UpowerBatteryState::Unknown, - }; - ping.ping(); - }, - "Percentage" => { - inner.value = value.0.as_f64().unwrap() as f32 / 100.; - ping.ping(); - }, - _ => (), + { + let mut inner = inner.lock().unwrap(); + inner.value = capacity as f32 / 100.; + inner.state = match state { + 1 => UpowerBatteryState::Charging, + 2 => UpowerBatteryState::Discharging, + 3 => UpowerBatteryState::Empty, + 4 => UpowerBatteryState::Full, + 5 => UpowerBatteryState::NotCharging, + 6 => UpowerBatteryState::Discharging, + _ => UpowerBatteryState::Unknown, } } - true - }).unwrap(); - loop { conn.process(Duration::from_millis(60000)).unwrap(); } - }).unwrap(); + proxy + .match_signal( + move |c: PropertiesPropertiesChanged, + _: &LocalConnection, + _: &dbus::Message| { + if c.interface_name != "org.freedesktop.UPower.Device" { + return true; + } + let mut inner = inner.lock().unwrap(); + for (key, value) in c.changed_properties { + match key.as_str() { + "State" => { + inner.state = match value.0.as_u64().unwrap() { + 1 => UpowerBatteryState::Charging, + 2 => UpowerBatteryState::Discharging, + 3 => UpowerBatteryState::Empty, + 4 => UpowerBatteryState::Full, + 5 => UpowerBatteryState::NotCharging, + 6 => UpowerBatteryState::Discharging, + _ => UpowerBatteryState::Unknown, + }; + ping.ping(); + } + "Percentage" => { + inner.value = value.0.as_f64().unwrap() as f32 / 100.; + ping.ping(); + } + _ => (), + } + } + true + }, + ) + .unwrap(); + + loop { + conn.process(Duration::from_millis(60000)).unwrap(); + } + }) + .unwrap(); } pub struct Battery { @@ -102,8 +112,8 @@ pub struct Battery { impl Battery { pub fn new(ping: Ping) -> BarWidget { - let battery = Battery{ - inner: Arc::new(Mutex::new(InnerBattery{ + let battery = Battery { + inner: Arc::new(Mutex::new(InnerBattery { value: 0., state: UpowerBatteryState::Unknown, })), @@ -114,7 +124,6 @@ impl Battery { } } - impl BarWidgetImpl for Battery { fn get_dirty(&self) -> bool { self.dirty diff --git a/src/widgets/launcher.rs b/src/widgets/launcher.rs index 0da7e35..ca39206 100644 --- a/src/widgets/launcher.rs +++ b/src/widgets/launcher.rs @@ -110,11 +110,11 @@ impl Prompt { "!" => { self.mode = PromptMode::Shell; return; - }, + } "=" => { self.mode = PromptMode::Calc; return; - }, + } _ => (), } } @@ -159,7 +159,9 @@ impl Prompt { fn clear_right(&mut self) { let indices: Vec<(usize, &str)> = self.input.grapheme_indices(true).collect(); if !indices.is_empty() { - self.input = indices[0..self.cursor].iter().fold("".into(), |acc, el| acc + el.1); + self.input = indices[0..self.cursor] + .iter() + .fold("".into(), |acc, el| acc + el.1); } else { self.mode = PromptMode::Normal; } @@ -168,7 +170,9 @@ impl Prompt { fn clear_left(&mut self) { let indices: Vec<(usize, &str)> = self.input.grapheme_indices(true).collect(); if !indices.is_empty() { - self.input = indices[self.cursor..indices.len()].iter().fold("".into(), |acc, el| acc + el.1); + self.input = indices[self.cursor..indices.len()] + .iter() + .fold("".into(), |acc, el| acc + el.1); self.cursor = 0; } else { self.mode = PromptMode::Normal; @@ -186,7 +190,12 @@ impl Prompt { trait InterfaceWidget { fn trigger(&mut self, intf: &InnerInterface); - fn draw(&mut self, intf: &InnerInterface, fonts: &mut FontMap, view: &mut BufferView) -> Geometry; + fn draw( + &mut self, + intf: &InnerInterface, + fonts: &mut FontMap, + view: &mut BufferView, + ) -> Geometry; fn update(&mut self, intf: &InnerInterface); } @@ -266,7 +275,12 @@ impl InterfaceWidget for Launcher { self.matches = matcher.matches(); } - fn draw(&mut self, intf: &InnerInterface, fonts: &mut FontMap, view: &mut BufferView) -> Geometry { + fn draw( + &mut self, + intf: &InnerInterface, + fonts: &mut FontMap, + view: &mut BufferView, + ) -> Geometry { let fg = Color::new(1., 1., 1., 1.); let bg = Color::new(0., 0., 0., 1.); @@ -290,13 +304,8 @@ impl InterfaceWidget for Launcher { .unwrap(); let font = fonts.get_font(LAUNCHER_FONT, LAUNCHER_SIZE); - font.auto_draw_text( - &mut prompt_line, - &bg, - &fg, - ">", - ) - .unwrap(); + font.auto_draw_text(&mut prompt_line, &bg, &fg, ">") + .unwrap(); // Draw entries let dimfg = Color::new(0.5, 0.5, 0.5, 1.0); @@ -331,8 +340,11 @@ impl InterfaceWidget for Launcher { } } - let content_height = min(intf.geometry.height, (self.matches.len() + 1) as u32 * line_height + 16); - Geometry{ + let content_height = min( + intf.geometry.height, + (self.matches.len() + 1) as u32 * line_height + 16, + ); + Geometry { x: intf.geometry.x, y: intf.geometry.y + intf.geometry.height - content_height, width: intf.geometry.width, @@ -341,7 +353,7 @@ impl InterfaceWidget for Launcher { } } -struct Shell (); +struct Shell(); impl InterfaceWidget for Shell { fn trigger(&mut self, intf: &InnerInterface) { @@ -359,7 +371,12 @@ impl InterfaceWidget for Shell { fn update(&mut self, _intf: &InnerInterface) {} - fn draw(&mut self, intf: &InnerInterface, fonts: &mut FontMap, view: &mut BufferView) -> Geometry { + fn draw( + &mut self, + intf: &InnerInterface, + fonts: &mut FontMap, + view: &mut BufferView, + ) -> Geometry { let fg = Color::new(1., 1., 1., 1.); let bg = Color::new(0., 0., 0., 1.); @@ -382,15 +399,10 @@ impl InterfaceWidget for Shell { ) .unwrap(); let font = fonts.get_font(LAUNCHER_FONT, LAUNCHER_SIZE); - font.auto_draw_text( - &mut prompt_line, - &bg, - &Color::new(1., 0.75, 0.5, 1.), - "!", - ) - .unwrap(); + font.auto_draw_text(&mut prompt_line, &bg, &Color::new(1., 0.75, 0.5, 1.), "!") + .unwrap(); - Geometry{ + Geometry { x: intf.geometry.x, y: intf.geometry.y + intf.geometry.height - line_height, width: intf.geometry.width, @@ -412,16 +424,22 @@ impl InterfaceWidget for Calc { } fn update(&mut self, intf: &InnerInterface) { - let res = rcalc_lib::parse::eval(&intf.prompt.input, &mut rcalc_lib::parse::CalcState::new()) - .map(|x| format!("{}", x)) - .map_err(|x| format!("{}", x)); + let res = + rcalc_lib::parse::eval(&intf.prompt.input, &mut rcalc_lib::parse::CalcState::new()) + .map(|x| format!("{}", x)) + .map_err(|x| format!("{}", x)); self.result = match res { Ok(v) => Some(v), Err(_) => None, }; } - fn draw(&mut self, intf: &InnerInterface, fonts: &mut FontMap, view: &mut BufferView) -> Geometry { + fn draw( + &mut self, + intf: &InnerInterface, + fonts: &mut FontMap, + view: &mut BufferView, + ) -> Geometry { let fg = Color::new(1., 1., 1., 1.); let bg = Color::new(0., 0., 0., 1.); @@ -435,28 +453,25 @@ impl InterfaceWidget for Calc { // Draw prompt let font = fonts.get_font(LAUNCHER_FONT, LAUNCHER_SIZE); - let res_off = font.auto_draw_text_with_cursor( - &mut prompt_line, - &bg, - &fg, - &format!(" {} ", &intf.prompt.input), - intf.prompt.cursor + 3, - ) - .unwrap(); + let res_off = font + .auto_draw_text_with_cursor( + &mut prompt_line, + &bg, + &fg, + &format!(" {} ", &intf.prompt.input), + intf.prompt.cursor + 3, + ) + .unwrap(); let font = fonts.get_font(LAUNCHER_FONT, LAUNCHER_SIZE); - font.auto_draw_text( - &mut prompt_line, - &bg, - &Color::new(1., 0.75, 0.5, 1.), - "=", - ) - .unwrap(); + font.auto_draw_text(&mut prompt_line, &bg, &Color::new(1., 0.75, 0.5, 1.), "=") + .unwrap(); if let Some(res) = &self.result { let resfg = Color::new(1.0, 0.65, 0., 1.0); let mut result_line = view.offset((res_off.0, prompt_offset)).unwrap(); - font.auto_draw_text(&mut result_line, &bg, &resfg, &format!(" = {}", &res)).unwrap(); + font.auto_draw_text(&mut result_line, &bg, &resfg, &format!(" = {}", &res)) + .unwrap(); } for m in self.old.iter().rev() { @@ -466,11 +481,15 @@ impl InterfaceWidget for Calc { } prompt_offset -= LAUNCHER_SIZE.ceil() as u32; let mut result_line = view.offset((0, prompt_offset)).unwrap(); - font.auto_draw_text(&mut result_line, &bg, &dimfg, &m).unwrap(); + font.auto_draw_text(&mut result_line, &bg, &dimfg, &m) + .unwrap(); } - let content_height = min(intf.geometry.height, (self.old.len() + 1) as u32 * line_height + 16); - Geometry{ + let content_height = min( + intf.geometry.height, + (self.old.len() + 1) as u32 * line_height + 16, + ); + Geometry { x: intf.geometry.x, y: intf.geometry.y + intf.geometry.height - content_height, width: intf.geometry.width, @@ -495,14 +514,14 @@ pub struct Interface { impl Interface { pub fn new() -> Interface { - Interface{ + Interface { launcher: Launcher::new(), - shell: Shell{}, - calc: Calc{ + shell: Shell {}, + calc: Calc { old: Vec::new(), result: None, }, - inner: InnerInterface{ + inner: InnerInterface { dirty: false, geometry: Default::default(), selection: 0, @@ -554,12 +573,12 @@ impl Widget for Interface { self.inner.prompt.clear_left(); self.inner.selection = 0; widget.update(&self.inner); - }, + } keysyms::XKB_KEY_k if event.modifiers.ctrl => { self.inner.prompt.clear_right(); self.inner.selection = 0; widget.update(&self.inner); - }, + } keysyms::XKB_KEY_Home => self.inner.prompt.home(), keysyms::XKB_KEY_End => self.inner.prompt.end(), keysyms::XKB_KEY_BackSpace => { @@ -599,4 +618,3 @@ impl Widget for Interface { self.launcher.next_token = Some(token.to_string()); } } - diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs index 679eb67..46f7cb4 100644 --- a/src/widgets/mod.rs +++ b/src/widgets/mod.rs @@ -1,14 +1,14 @@ +mod backlight; mod bar_widget; mod battery; -mod backlight; mod clock; mod date; mod launcher; mod line; mod widget; -pub use battery::Battery; pub use backlight::Backlight; +pub use battery::Battery; pub use clock::Clock; pub use date::Date; pub use launcher::Interface; diff --git a/src/widgets/widget.rs b/src/widgets/widget.rs index d1c40b3..a5fef27 100644 --- a/src/widgets/widget.rs +++ b/src/widgets/widget.rs @@ -1,4 +1,4 @@ -use std::cmp::{min, max}; +use std::cmp::{max, min}; use std::fmt; use crate::{buffer::BufferView, fonts::FontMap, keyboard::KeyEvent}; @@ -23,7 +23,7 @@ impl fmt::Display for Geometry { impl Geometry { pub fn new() -> Geometry { - Geometry{ + Geometry { x: 0, y: 0, width: 0, @@ -35,7 +35,8 @@ impl Geometry { let x = min(self.x, other.x); let y = min(self.y, other.y); Geometry { - x, y, + x, + y, width: max(self.width + self.x, other.width + other.x) - x, height: max(self.height + self.y, other.height + other.height) - y, } @@ -148,7 +149,7 @@ impl Layout for Margin { geometry: &Geometry, user_data: &mut U, ) -> Geometry { - let geo = Geometry{ + let geo = Geometry { x: geometry.x + self.margin.0, y: geometry.y + self.margin.1, width: geometry.width - self.margin.0 - self.margin.2,