1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
use std::env;
use std::time::Duration;
use std::collections::HashMap;
use std::process::Command;
use tokio::time::sleep;
use systemstat::{System, Platform, saturating_sub_bytes, DelayedMeasurement, CPULoad};
use std::io::Write;
use std::fs::File;
use regex::Regex;

type Logo = fn(&Value) -> String;

pub enum Value {
    S(String),
    I(u8),
}

pub enum SourceData {
    U(usize),
    CPU(DelayedMeasurement<CPULoad>),
    Nothing
}

pub trait Source {
    fn unit(&self) -> Option<String>;
    fn get(&mut self) -> Value;
}

pub struct Sources {
}

/*
macro_rules! i_source {
    ($x:expr, $y:expr) =>  {
    Source {
      unit: Some($x.to_string()),
      get: |_| (Value::I($y(System::new()) as u8), SourceData::Nothing),
      data: SourceData::Nothing,
    }
  }
}

macro_rules! s_source {
    ($x:expr, $y:expr) =>  {
    Source {
      unit: Some($x.to_string()),
      get: |_| (Value::S($y), SourceData::Nothing),
      data: SourceData::Nothing,
    }
  }
}
*/

pub struct BatterySource {
}

impl Source for BatterySource {
    fn unit(&self) -> Option<String> {
        Some("%".to_string())
    }

    fn get(&mut self) -> Value {
        let s = System::new();
        Value::I(s.battery_life().map_or(0.0, |x| (
                  if x.remaining_capacity > 1.0 { 100.0 } else { x.remaining_capacity * 100.0})) as u8)
    }
}

pub struct CpuSource {
    delayed_measurement_opt: Option<DelayedMeasurement<CPULoad>>,
}

impl Source for CpuSource {

    fn unit(&self) -> Option<String> {
        Some("%".to_string())
    }

    fn get(&mut self) -> Value {
        let res = match &self.delayed_measurement_opt {
            Some(cpu) => {
                let cpu = cpu.done().unwrap();
                let cpu = cpu.system + cpu.user;
                Value::I((cpu * 100.0) as u8)
            },
            _ => Value::I(0),
        };
        self.delayed_measurement_opt = match System::new().cpu_load_aggregate() {
            Ok(r) => Some(r),
            Err(_) => None
        };
        res
    }
}

pub struct CpuTempSource {
}

impl Source for CpuTempSource {
    fn unit(&self) -> Option<String> {
        Some("°C".to_string())
    }

    fn get(&mut self) -> Value {
        Value::I(System::new().cpu_temp().unwrap_or(0.0) as u8)
    }
}

pub struct MemorySource {
}

impl Source for MemorySource {
    fn unit(&self) -> Option<String> {
        Some("%".to_string())
    }

    fn get(&mut self) -> Value {
        Value::I(System::new().memory().map_or(0.0, |mem| (saturating_sub_bytes(mem.total, mem.free).as_u64()  * 100 / mem.total.as_u64()) as f32) as u8)
    }
}

pub struct DateSource {
}

impl Source for DateSource {
    fn unit(&self) -> Option<String> {
        Some("".to_string())
    }

    fn get(&mut self) -> Value {
       Value::S({ let mut s = Command::new("sh")
           .arg("-c")
               .arg("date | sed -E 's/:[0-9]{2} .*//'").output().map_or("".to_string(), |o| String::from_utf8(o.stdout).unwrap_or("".to_string())); s.pop(); s})
    }
}

pub struct WindowSource {
    max_chars: usize
}

impl Source for WindowSource {
    fn unit(&self) -> Option<String> {
        Some("".to_string())
    }

    fn get(&mut self) -> Value {
        let s = Command::new("sh")
            .arg("-c")
            .arg("xdotool getwindowfocus getwindowpid getwindowname 2>/dev/null").output().map_or("".to_string(), 
                |o| String::from_utf8(o.stdout).unwrap_or("".to_string())); 
        let lines : Vec<&str> = s.split("\n").collect();
        if lines.len() >= 2 {
            let mut comm = std::fs::read_to_string(format!("/proc/{}/comm", lines[0])).unwrap_or("".to_string());
            comm.pop();
            let s = format!("{} - {}", comm, lines[1]);
                    Value::S(match s.char_indices().nth(self.max_chars) {
                        None => s,
                        Some((idx, _)) => (&s[..idx]).to_string(),
                    })
        }
        else {
                Value::S("".to_string())
        }
    }
}


impl Sources {

  pub fn battery() -> Box<BatterySource> {
      Box::new(BatterySource { })
  }

  pub fn cpu() -> Box<CpuSource> {
      Box::new(CpuSource{ delayed_measurement_opt: None })
  }
  pub fn cpu_temp() -> Box<CpuTempSource> {
      Box::new(CpuTempSource { })
  }

  pub fn memory() -> Box<MemorySource> {
      Box::new(MemorySource { })
  }

  pub fn date() -> Box<DateSource> {
      Box::new(DateSource { })
  }

  pub fn window(max_chars: usize) -> Box<WindowSource> {
      Box::new(WindowSource { max_chars: max_chars })
  }
}

pub struct Logos {
}

macro_rules! i_logo {
    ($x:expr) =>  {
        |v| match v {
            Value::S(_) => "",
            Value::I(i) => $x(i)
        }.to_string()
  }
}

impl Logos {

    pub fn battery() -> Logo {
        i_logo!(|i| ["", "", "", "", "", "", "", "", "", "", ""].get((i/10) as usize).unwrap_or(&""))
    }

    pub fn cpu() -> Logo {
        |_| " ".to_string()
    }

    pub fn cpu_temp() -> Logo {
        |_| " ".to_string()
    }

    pub fn memory() -> Logo {
        |_| " ".to_string()
    }

    pub fn date() -> Logo {
        |_| " ".to_string()
    }

    fn website(s: &str) -> String {
        let browsers = "^(chrom|firefox|qutebrowser)";
        format!("{}.* {}.*", browsers, s)
    }

    fn terminals() -> String {
    "^(alacritty|termite|xterm)".to_string()
    }

    fn terminal(s: &str) -> String {
        format!("{}.* {}.*", Logos::terminals(), s)
    }

    pub fn window() -> Logo {
        |value| { 
            match value {
                Value::S(value) => {
                    let mut matches : HashMap<String, Regex> = HashMap::new();
                    macro_rules! nm {
                        ($x:expr, $y:expr) =>  {
                            matches.insert($x.to_string(), Regex::new($y).unwrap());
                        }
                    }
                    nm!(" ", &Logos::website("Stack Overflow"));
                    nm!(" ", &Logos::website("Facebook"));
                    nm!("暑", &Logos::website("Twitter"));
                    nm!(" ", &Logos::website("YouTube"));
                    nm!(" ", &Logos::website("reddit"));
                    nm!(" ", &Logos::website("Wikipedia"));
                    nm!(" ", &Logos::website("GitHub"));
                    nm!(" ", &Logos::website("WhatsApp"));
                    nm!(" ", "signal-desktop .*");
                    nm!(" ", &Logos::terminal("n?vim"));
                    nm!(" ", "^(mpv|mplayer).*");
                    nm!(" ", &(Logos::terminals() + ".*"));
                    nm!(" ", "^firefox.*");
                    nm!(" ", "^chrom.*");
                    nm!(" ", "^gimp.*");
                    matches.insert(" ".to_string(), Regex::new("^firefox.*").unwrap());
                    for (logo, reg) in &matches {
                        if reg.is_match(value) {
                            return logo.to_string();
                        }
                    }
                },
                Value::I(_) => {},
            }
            " ".to_string()
        }
    }

}

type Color = u32;

#[derive(Clone)]
pub enum ColoredStringItem {
    S(String),
    BgColor(Color),
    FgColor(Color),
    StopFg,
    StopBg,
}

#[derive(Clone)]
pub struct ColoredString {
    string: Vec<ColoredStringItem>,
}

impl ColoredString {

    pub fn new() -> ColoredString {
        ColoredString {
            string: vec![],
        }
    }

    pub fn bg(&mut self, color: Color) -> &mut ColoredString {
        self.string.push(ColoredStringItem::BgColor(color));
        self
    }

    pub fn fg(&mut self, color: Color) -> &mut ColoredString {
        self.string.push(ColoredStringItem::FgColor(color));
        self
    }

    pub fn fg_bg(&mut self, colors: &(Color, Color)) -> &mut ColoredString {
        self.fg(colors.0).bg(colors.1)
    }


    pub fn s(&mut self, s: &str) -> &mut ColoredString {
        self.string.push(ColoredStringItem::S(s.to_string()));
        self
    }

    pub fn ebg(&mut self) -> &mut ColoredString {
        self.string.push(ColoredStringItem::StopBg);
        self
    }

    pub fn efg(&mut self) -> &mut ColoredString {
        self.string.push(ColoredStringItem::StopFg);
        self
    }

    fn htc(color: Color) -> String {
        let b = color & 0xff;
        let g = (color >> 8) & 0xff;
        let r = (color >> 16) & 0xff;
        format!("{};{};{}", r, g, b)
    }

    pub fn to_string(&self) -> String {
        self.string.clone().into_iter().map ( |item|
            match item {
                ColoredStringItem::S(s) => s,
                ColoredStringItem::BgColor(c) => format!("\x1b[48;2;{}m", ColoredString::htc(c)),
                ColoredStringItem::FgColor(c) => format!("\x1b[38;2;{}m", ColoredString::htc(c)),
                ColoredStringItem::StopBg => "\x1b[49m".to_string(),
                ColoredStringItem::StopFg => "\x1b[39m".to_string(),
            }
        ).collect::<Vec<String>>().join("")
    }

    pub fn len(&self) -> usize {
        self.string.clone().into_iter().map ( |item|
            match item {
                ColoredStringItem::S(s) => s.chars().count(),
                _ => 0,
            }
        ).fold(0, |a, b| a + b)
    }

}

pub type FgColorAndBgColor = (Color, Color);

pub struct Palette {
    _source: Option<String>,
    colors: Vec<FgColorAndBgColor>,
}

impl Palette {

    pub fn black() -> Palette {
        Palette {
            _source: None,
            colors: vec![(0xfffff, 0)]
        }
    }

    pub fn grey_blue_cold_winter() -> Palette {
        Palette {
            _source: Some("https://colorhunt.co/palette/252807".to_string()),
            colors: vec![
                (0,0xf6f5f5),
                (0,0xd3e0ea),
                (0xf6f5f5,0x1687a7),
                (0xd3e0ea,0x276678),
            ]
        }
    }

    pub fn black_grey_turquoise_dark() -> Palette {
        Palette {
            _source: Some("https://colorhunt.co/palette/2763".to_string()),
            colors: vec![
                (0xeeeeee,0x222831),
                (0xeeeeee,0x393e46),
                (0,0x00adb5),
                (0,0xeeeeee),
            ]
        }
    }

    pub fn red_pink_turquoise_spring() -> Palette {
        Palette {
            _source: Some("https://colorhunt.co/palette/2257091".to_string()),
            colors: vec![
                (0,0xef4f4f),
                (0,0xee9595),
                (0,0xffcda3),
                (0,0x74c7b8),
            ]
        }
    }

    pub fn get(&self, i: usize) -> &FgColorAndBgColor {
        self.colors.get(i % self.colors.len()).unwrap_or(&(0,0xff))
    }

}

pub struct ThemedWidgets {
}

impl ThemedWidgets {

    pub fn simple(widget_position: WidgetPosition, sources_logos: Vec<(Box<dyn Source>, Logo)>, palette: &Palette) -> (WidgetPosition, Vec<Widget>) {
        (widget_position,
         sources_logos.into_iter().enumerate().map( |(i, source_logo)| {
             let fg_bg = palette.get(i);
             Widget {
                 source: source_logo.0,
                 prefix: ColoredString::new().fg_bg(fg_bg).s(" ").clone(),
                 suffix: ColoredString::new().s(" ").ebg().efg().s(" ").clone(),
                 logo: source_logo.1,
             }}).collect())
    }

    pub fn detached(left_separator: &str, right_separator: &str, widget_position: WidgetPosition, sources_logos: Vec<(Box<dyn Source>, Logo)>, palette: &Palette) -> (WidgetPosition, Vec<Widget>) {
        (widget_position,
         sources_logos.into_iter().enumerate().map( |(i, source_logo)| {
             let fg_bg = palette.get(i);
             Widget {
                 source: source_logo.0,
                 prefix: ColoredString::new().fg(fg_bg.1).s(left_separator).fg_bg(fg_bg).s(" ").clone(),
                 suffix: ColoredString::new().s(" ").ebg().fg(fg_bg.1).s(right_separator).efg().s(" ").clone(),
                 logo: source_logo.1,
             }}).collect())
    }

    pub fn slash(widget_position: WidgetPosition, sources_logos: Vec<(Box<dyn Source>, Logo)>, palette: &Palette) -> (WidgetPosition, Vec<Widget>) {
        ThemedWidgets::detached(" ", "", widget_position, sources_logos, palette)
    }

    pub fn tab(widget_position: WidgetPosition, sources_logos: Vec<(Box<dyn Source>, Logo)>, palette: &Palette) -> (WidgetPosition, Vec<Widget>) {
        ThemedWidgets::detached(" ", " ", widget_position, sources_logos, palette)
    }

    pub fn attached(left_separator: &str, right_separator: &str, widget_position: WidgetPosition, sources_logos: Vec<(Box<dyn Source>, Logo)>, palette: &Palette) -> (WidgetPosition, Vec<Widget>) {
        let sources_logos_len = sources_logos.len();
        let left = widget_position == WidgetPosition::Left;
        (widget_position,
         sources_logos.into_iter().enumerate().map( |(i, source_logo)| {
             let fg_bg = palette.get(i);
             let n_fg_bg = palette.get(i + 1);
             let prefix = if left {
                 ColoredString::new().fg_bg(fg_bg).s(" ").clone()
             } else {
                 let mut s = ColoredString::new();
                 if i + 1 < sources_logos_len {
                     s.bg(n_fg_bg.1);
                 }
                 s.fg(fg_bg.1).s(right_separator).fg_bg(fg_bg).s(" ").clone()
             };
             let suffix = if left { 
                 let mut s = ColoredString::new();
                 s.s(" ").ebg().fg(fg_bg.1);
                 if i + 1 < sources_logos_len { s.bg(n_fg_bg.1); };
                 s.s(left_separator).ebg().efg().clone()
             } else {
                 ColoredString::new().s(" ").ebg().efg().clone()
             };
             Widget {
                 source: source_logo.0,
                 prefix: prefix,
                 suffix: suffix,
                 logo: source_logo.1,
             }}).collect())
    }

    pub fn powerline(widget_position: WidgetPosition, sources_logos: Vec<(Box<dyn Source>, Logo)>, palette: &Palette) -> (WidgetPosition, Vec<Widget>) {
        ThemedWidgets::attached("", "", widget_position, sources_logos, palette)

    }

    pub fn flames(widget_position: WidgetPosition, sources_logos: Vec<(Box<dyn Source>, Logo)>, palette: &Palette) -> (WidgetPosition, Vec<Widget>) {
        ThemedWidgets::attached(" ", " ", widget_position, sources_logos, palette)
    }
}

pub struct Widget {
    pub source: Box<dyn Source>,
    pub prefix: ColoredString,
    pub suffix: ColoredString,
    pub logo: Logo,
}

struct Ansi {
}

impl Ansi {

    fn hide_cursor() {
        print!("\x1b[?25l");
    }

    fn move_back(n: usize) {
        print!("\x1b[{}D", n);
    }

    fn move_to(line: usize, col: usize) {
        print!("\x1b[{};{}H", line, col);
    }
}

#[derive(Debug, Clone, PartialEq, std::cmp::Eq, Hash)]
pub enum WidgetPosition {
    Left,
    Right
}

impl Widget {
    pub async fn draw(&mut self, widget_position: &WidgetPosition) {
        let value = (*self.source).get();
        let unit = (*self.source).unit();
        let logo = (self.logo)(&value);
        let value_s = match value {
            Value::S(s) => s,
            Value::I(i) => i.to_string()
        };
        let s = format!("{}{} {}{}{}", self.prefix.to_string(), logo, value_s, unit.clone().unwrap_or(String::from("")), self.suffix.to_string());
        if widget_position == &WidgetPosition::Left {
            print!("{}", s);
        } else {
            let len = format!("{} {}{}", logo, value_s, unit.clone().unwrap_or(String::from("")), ).chars().count() + self.prefix.len() + self.suffix.len();
            Ansi::move_back(len);
            print!("{}", s);
            Ansi::move_back(len);
        }
    }
}

pub struct Conf {
    pub font: String,
    pub font_size: u8,
    pub terminal_width: u16,
    pub refresh_time: Duration,
    pub widgets: HashMap<WidgetPosition, Vec<Widget>>,
}

pub struct UmberBar {
    pub conf: Conf
}

impl UmberBar {

    pub async fn draw_at(widget_position: &WidgetPosition, widgets: &mut Vec<Widget>) {
        for widget in widgets {
            widget.draw(&widget_position).await
        }
    }

    pub async fn draw(&mut self) {
        let left = WidgetPosition::Left;
        let right = WidgetPosition::Right;
        Ansi::move_to(0, 0);
        print!("{}", " ".repeat(self.conf.terminal_width as usize));
        Ansi::move_to(0, 0);
        UmberBar::draw_at(&left, self.conf.widgets.get_mut(&left).unwrap_or(&mut vec![])).await;
        Ansi::move_to(0, self.conf.terminal_width as usize);
        UmberBar::draw_at(&right, self.conf.widgets.get_mut(&right).unwrap_or(&mut vec![])).await;
        let _ = std::io::stdout().flush();
    }

    fn is_child_process() -> bool {
        match env::var("within_umberbar") {
            Ok(_) => true,
            Err(_) => false,
        }

    }

    async fn run_inside_terminal(&mut self) {
        Ansi::hide_cursor();
        loop {
            self.draw().await;
            sleep(self.conf.refresh_time).await;
        }
    }

    fn run_terminal(&mut self) {
          let output = format!("font:\n  family: {}\n  size: {}\nbackground_opacity: 0\nwindow:\n  position:\n    x: 0\n    y: 0\n  dimensions:\n    columns: {}\n    lines: 1\n", self.conf.font, self.conf.font_size, self.conf.terminal_width);
          let alacritty_conf_path = "/tmp/alacritty-umberbar-rs.yml";
          let mut ofile = File::create(alacritty_conf_path)
              .expect("unable to create file");
        ofile.write_all(output.as_bytes())
            .expect("unable to write");
        match std::env::current_exe() {
            Ok(cmd) => {
                let _ = Command::new("alacritty")
                    .env("within_umberbar", "true")
                    .arg("--config-file")
                    .arg(alacritty_conf_path)
                    .arg("--class")
                    .arg("xscreensaver")
                    .arg("--command")
                    .arg(cmd)
                    .status();
                }
            Err(e) => { print!("{}", e); }
        }
    }

    pub async fn run(&mut self) {
        if UmberBar::is_child_process() {
            self.run_inside_terminal().await
        } else {
            self.run_terminal()
        }
    }
}

pub fn umberbar(conf: Conf) -> UmberBar {
    UmberBar {
        conf: conf
    }
}