external.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. use comm;
  2. use comm::Channel;
  3. use config::Config;
  4. use std::io;
  5. pub fn external(tx: &Channel, _cfg: &Config) {
  6. let mut line = String::new();
  7. loop {
  8. line.clear();
  9. io::stdin().read_line(&mut line).ok().expect("Failed to read line");
  10. let kind = line.remove(0);
  11. let line = line.trim();
  12. match kind {
  13. 'M' => comm::send(tx, "cmus", &format!("%{{A:cmus:}}{}%{{A}}", line)),
  14. 'W' => comm::send(tx, "desktops", &parse_bspwm(line)),
  15. _ => ()
  16. }
  17. }
  18. }
  19. fn parse_bspwm(line: &str) -> String {
  20. let mut out = String::new();
  21. let mut monitor_count = 0;
  22. let mut monitor_selected = false;
  23. let elems = line.split(':');
  24. for elem in elems {
  25. let mut chars = elem.chars();
  26. let kind = chars.next().unwrap();
  27. let name = chars.collect::<String>();
  28. if kind == 'M' || kind == 'm' {
  29. out = format!("{}%{{S{num}}}", out, num = monitor_count);
  30. monitor_count += 1;
  31. monitor_selected = kind.is_uppercase();
  32. }
  33. else if kind == 'L' {
  34. }
  35. else if kind == 'G' {
  36. }
  37. else if kind == 'T' {
  38. }
  39. else {
  40. let desktop = format!("%{{A:w{name}:}} {name} %{{A}}", name = name);
  41. if kind.is_uppercase() {
  42. if monitor_selected {
  43. out = format!("{}%{{B#66CC66}}{}%{{B-}}", out, desktop);
  44. }
  45. else {
  46. out = format!("{}%{{B#666666}}{}%{{B-}}", out, desktop);
  47. }
  48. }
  49. else if kind == 'U' || kind == 'u' {
  50. out = format!("{}%{{B#CC0033}}{}%{{B-}}", out, desktop);
  51. }
  52. else if kind == 'O' || kind == 'o' {
  53. out = format!("{}{}", out, desktop);
  54. }
  55. }
  56. }
  57. out
  58. }