external.rs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. 'T' => comm::send(tx, "title", line),
  14. 'M' => comm::send(tx, "cmus", &format!("%{{A:cmus:}}{}%{{A}}", line)),
  15. 'W' => comm::send(tx, "desktops", &parse_bspwm(line)),
  16. _ => ()
  17. }
  18. }
  19. }
  20. fn parse_bspwm(line: &str) -> String {
  21. let mut out = String::new();
  22. let mut monitor_count = 0;
  23. let mut monitor_selected = false;
  24. let elems = line.split(':');
  25. for elem in elems {
  26. let mut chars = elem.chars();
  27. let kind = chars.next().unwrap();
  28. let name = chars.collect::<String>();
  29. if kind == 'M' || kind == 'm' {
  30. out = format!("{}%{{S{num}}}", out, num = monitor_count);
  31. monitor_count += 1;
  32. monitor_selected = kind.is_uppercase();
  33. }
  34. else if kind == 'L' {
  35. }
  36. else if kind == 'G' {
  37. }
  38. else if kind == 'T' {
  39. }
  40. else {
  41. let desktop = format!("%{{A:w{name}:}} {name} %{{A}}", name = name);
  42. if kind.is_uppercase() {
  43. if monitor_selected {
  44. out = format!("{}%{{B#66CC66}}{}%{{B-}}", out, desktop);
  45. }
  46. else {
  47. out = format!("{}%{{B#666666}}{}%{{B-}}", out, desktop);
  48. }
  49. }
  50. else if kind == 'U' || kind == 'u' {
  51. out = format!("{}%{{B#CC0033}}{}%{{B-}}", out, desktop);
  52. }
  53. else if kind == 'O' || kind == 'o' {
  54. out = format!("{}{}", out, desktop);
  55. }
  56. }
  57. }
  58. out
  59. }