1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- use std::process::Command;
- use style;
- use ui::context::Context;
- use widgets::{Message, Update, Widget, WidgetParams};
- pub struct Disk {
- context: Context,
- mount: String,
- space: String
- }
- pub fn disk(params: WidgetParams) -> Box<Widget> {
- let config: DiskConfig = params.config.try_into().unwrap();
- let widget = Disk {
- context: params.context,
- mount: config.mount,
- space: "???".to_string()
- };
- Box::new(widget)
- }
- impl Widget for Disk {
- fn render(&mut self, x: u16, width: u16) {
- style::render(&self.context, &self.mount, &self.space, x, width);
- }
- fn width(&mut self) -> u16 {
- style::width(&self.context, &self.mount, &self.space)
- }
- fn handle_event(&mut self, event: &Message) -> Update {
- match event {
- &Message::Update => {
- let output = Command::new("df")
- .arg("--output=avail")
- .arg("-h")
- .arg(&self.mount)
- .output()
- .ok()
- .expect("Could not run df");
- let output = String::from_utf8_lossy(&output.stdout);
- let space = output.lines().nth(1).expect("Could not get space");
- let space = space.trim().to_string();
- let changed = space != self.space;
- self.space = space;
- if changed {
- Update::Relayout
- }
- else {
- Update::Nothing
- }
- },
- _ => Update::Nothing
- }
- }
- }
- #[derive(Deserialize)]
- struct DiskConfig {
- mount: String
- }
|