disk.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. use std::process::Command;
  2. use style;
  3. use ui::context::Context;
  4. use widgets::{Message, Update, Widget, WidgetParams};
  5. pub struct Disk {
  6. context: Context,
  7. mount: String,
  8. space: String
  9. }
  10. pub fn disk(params: WidgetParams) -> Box<Widget> {
  11. let config: DiskConfig = params.config.try_into().unwrap();
  12. let widget = Disk {
  13. context: params.context,
  14. mount: config.mount,
  15. space: "???".to_string()
  16. };
  17. Box::new(widget)
  18. }
  19. impl Widget for Disk {
  20. fn render(&mut self, x: u16, width: u16) {
  21. style::render(&self.context, &self.mount, &self.space, x, width);
  22. }
  23. fn width(&mut self) -> u16 {
  24. style::width(&self.context, &self.mount, &self.space)
  25. }
  26. fn handle_event(&mut self, event: &Message) -> Update {
  27. match event {
  28. &Message::Update => {
  29. let output = Command::new("df")
  30. .arg("--output=avail")
  31. .arg("-h")
  32. .arg(&self.mount)
  33. .output()
  34. .ok()
  35. .expect("Could not run df");
  36. let output = String::from_utf8_lossy(&output.stdout);
  37. let space = output.lines().nth(1).expect("Could not get space");
  38. let space = space.trim().to_string();
  39. let changed = space != self.space;
  40. self.space = space;
  41. if changed {
  42. Update::Relayout
  43. }
  44. else {
  45. Update::Nothing
  46. }
  47. },
  48. _ => Update::Nothing
  49. }
  50. }
  51. }
  52. #[derive(Deserialize)]
  53. struct DiskConfig {
  54. mount: String
  55. }