aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: c03c95407d3e2158e58e0cdf05490a6fc27d6bc2 (plain)
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
//! `rsblocks` A multi threaded status bar for dwm window manager.

/*

 Licence: MIT


 Author: Mustafa Salih
         [MustafaSalih](https://github.com/MustafaSalih1993)


 Contributers:

           -  AdaShoelace - for her/his contributions.
              [AdaShoelace](https://github.com/AdaShoelace)


 Thanks to:
           - wttr.in - for using their weather API.
             [wttr.in](https://github.com/chubin/wttr.in)


*/

use alsa::mixer::{Mixer, SelemChannelId, SelemId};
use breadx::{display::*, window::Window};
use chrono::prelude::*;
use dbus::blocking::stdintf::org_freedesktop_dbus::Properties;
use dbus::{arg, blocking::Connection};
use mpd::{Client, Song};
use std::fs::File;
use std::io::Error;
use std::io::Read;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
use yaml_rust::{yaml, YamlLoader};

#[derive(Debug)]
pub enum ThreadsData {
    Mpd(String),
    Sound(String),
    Disk(String),
    Memory(String),
    Time(String),
    Weather(String),
    Battery(String),
    CpuTemp(String),
    Uptime(String),
    Spotify(String),
}

#[derive(Clone)]
pub struct Config {
    pub seperator: String,
    pub time: Time,
    pub memory: Memory,
    pub disk: Disk,
    pub volume: Volume,
    pub weather: Weather,
    pub battery: Battery,
    pub cpu_temperature: CpuTemp,
    pub uptime: Uptime,
    pub mpd: Mpd,
    pub spotify: Spotify,
}

#[derive(Clone)]
pub struct Time {
    pub format: String,
    pub icon: String,
    pub delay: f64,
}

#[derive(Clone)]
pub struct Memory {
    pub icon: String,
    pub enabled: bool,
    pub delay: f64,
}

#[derive(Clone)]
pub struct Disk {
    pub icon: String,
    pub enabled: bool,
    pub delay: f64,
}
#[derive(Clone)]
pub struct Volume {
    pub icon: String,
    pub enabled: bool,
    pub delay: f64,
    pub card: String,
}

#[derive(Clone)]
pub struct Weather {
    pub city: String,
    pub format: String,
    pub icon: String,
    pub enabled: bool,
    pub delay: f64,
}

#[derive(Clone)]
pub struct Battery {
    pub source: String,
    pub icon: String,
    pub enabled: bool,
    pub delay: f64,
}

#[derive(Clone)]
pub struct CpuTemp {
    pub icon: String,
    pub enabled: bool,
    pub delay: f64,
}

#[derive(Clone)]
pub struct Uptime {
    pub icon: String,
    pub enabled: bool,
    pub delay: f64,
}

#[derive(Clone)]
pub struct Mpd {
    pub icon: String,
    pub host: String,
    pub port: String,
    pub enabled: bool,
    pub delay: f64,
}

#[derive(Clone)]
pub struct Spotify {
    pub icon: String,
    pub enabled: bool,
    pub delay: f64,
}

pub struct Blocks {
    disp: Display<name::NameConnection>,
    root: Window,
}

impl Blocks {
    pub fn new() -> Self {
        let disp = Display::create(None, None).expect("Failed to create x11 connection");
        let root = disp.default_screen().root;
        Self { disp, root }
    }
}

impl Default for Blocks {
    fn default() -> Self {
        Self::new()
    }
}

/*                            TODOS

    TODO 1: Error handling required if rsblocks.yml file is empty.

    TODO 2: This lib file growing and soon it will be annoying to move
            arround, need fix soon.

    TODO 3: Need a better comments in code, or no one will understand what happens.

    TODO 4: Need a documentation.

    TODO 5: Fix repeated code for threads in `run` function.

    TODO 6: Getting song metadata by a format provided by the user.

*/

/*
this function is responsible to check if the rsblocks.yml file
exists to call parse_config to read it OTHERWISE
it will call load_defaults to load a hardcoded default configuration

it will always return a valid configuration inside a Result.
*/
pub fn load_config() -> Result<Config, Error> {
    let yml_source = std::env::var("HOME").unwrap() + "/.config/rsblocks/rsblocks.yml";
    let mut data = String::new();
    let mut file = match File::open(&yml_source) {
        Ok(file) => file,
        Err(_) => {
            eprintln!("{} file not found, loading defaults!", &yml_source);
            return Ok(load_defaults());
        }
    };
    file.read_to_string(&mut data)?;
    let yml_content = &YamlLoader::load_from_str(&data).unwrap()[0];
    let config = parse_config(yml_content);
    Ok(config)
}

/*
this is simply returns a hardcoded configuration as default
*/
fn load_defaults() -> Config {
    Config {
        seperator: String::from("|"),
        time: Time {
            format: String::from("%T"),
            icon: String::from(""),
            delay: 1.0,
        },
        memory: Memory {
            icon: String::from(""),
            enabled: true,
            delay: 2.0,
        },
        disk: Disk {
            icon: String::from(""),
            enabled: false,
            delay: 60.0,
        },
        volume: Volume {
            icon: String::from(""),
            enabled: false,
            delay: 0.17,
            card: String::from("ALSA"),
        },
        weather: Weather {
            city: String::from(""),
            format: String::from("+%t"),
            icon: String::from(""),
            enabled: false,
            delay: 7200.0, //7200 seconds = 2 hours
        },
        battery: Battery {
            source: String::from("BAT0"),
            icon: String::from(""),
            enabled: false,
            delay: 120.0,
        },
        cpu_temperature: CpuTemp {
            icon: String::from(""),
            enabled: false,
            delay: 120.0,
        },
        uptime: Uptime {
            icon: String::from(""),
            enabled: false,
            delay: 60.0,
        },
        mpd: Mpd {
            icon: String::from(""),
            host: String::from("127.0.0.1"),
            port: String::from("6600"),
            enabled: false,
            delay: 15.0,
        },
        spotify: Spotify {
            icon: String::from(""),
            enabled: false,
            delay: 15.0,
        },
    }
}

/*
it will read and parse the rsblocks.yml file content and return a valid configuration
IF some content is missing in the rsblocks.yml file, it will set
a default values to that.

NOTE: (get_or_set) functions job below getting the values from the configuration doc IF
      a value is not exist in the config it will SET a value givin in the last argument.
*/
fn parse_config(doc: &yaml::Yaml) -> Config {
    let seperator = get_or_set_string(doc, "general", "seperator", "|");

    // time values
    let time_icon = get_or_set_string(doc, "time", "icon", "");
    let time_format = get_or_set_string(doc, "time", "format", "%T");
    let time_delay = get_or_set_f64(doc, "time", "delay", 1.0);

    // memory values
    let mem_icon = get_or_set_string(doc, "memory", "icon", "");
    let memory_enabled = get_or_set_bool(doc, "memory", "enable");
    let memory_delay = get_or_set_f64(doc, "memory", "delay", 2.0);

    //disk values
    let disk_icon = get_or_set_string(doc, "disk", "icon", "");
    let disk_enabled = get_or_set_bool(doc, "disk", "enable");
    let disk_delay = get_or_set_f64(doc, "disk", "delay", 120.0);

    // volume values
    let volume_icon = get_or_set_string(doc, "volume", "icon", "");
    let volume_enabled = get_or_set_bool(doc, "volume", "enable");
    let volume_delay = get_or_set_f64(doc, "volume", "delay", 0.17);
    let volume_card = get_or_set_string(doc, "volume", "card", "ALSA");

    // weather values
    let weather_icon = get_or_set_string(doc, "weather", "icon", "");
    let weather_format = get_or_set_string(doc, "weather", "format", "%l:+%t");
    let weather_city = get_or_set_string(doc, "weather", "city", "");
    let weather_enabled = get_or_set_bool(doc, "weather", "enable");
    let weather_delay = get_or_set_f64(doc, "weather", "delay", 7200.0);

    // battery values
    let battery_icon = get_or_set_string(doc, "battery", "icon", "");
    let battery_enabled = get_or_set_bool(doc, "battery", "enable");
    let battery_source = get_or_set_string(doc, "battery", "source", "BAT0");
    let battery_delay = get_or_set_f64(doc, "battery", "delay", 120.0);

    // cpu values
    let cpu_temperature_icon = get_or_set_string(doc, "cpu_temperature", "icon", "");
    let cpu_temperature_enabled = get_or_set_bool(doc, "cpu_temperature", "enable");
    let cpu_temperature_delay = get_or_set_f64(doc, "cpu_temperature", "delay", 120.0);

    // uptime values
    let uptime_icon = get_or_set_string(doc, "uptime", "icon", "");
    let uptime_enabled = get_or_set_bool(doc, "uptime", "enable");
    let uptime_delay = get_or_set_f64(doc, "uptime", "delay", 60.0);

    // mpd values
    let mpd_icon = get_or_set_string(doc, "mpd", "icon", "");
    let mpd_host = get_or_set_string(doc, "mpd", "host", "127.0.0.1");
    let mpd_port = get_or_set_string(doc, "mpd", "port", "6600");
    let mpd_enabled = get_or_set_bool(doc, "mpd", "enable");
    let mpd_delay = get_or_set_f64(doc, "mpd", "delay", 15.0);

    //spotify values
    let spotify_icon = get_or_set_string(doc, "spotify", "icon", "");
    let spotify_enabled = get_or_set_bool(doc, "spotify", "enable");
    let spotify_delay = get_or_set_f64(doc, "spotify", "delay", 10.0);

    Config {
        seperator,
        time: Time {
            format: time_format,
            icon: time_icon,
            delay: time_delay,
        },
        memory: Memory {
            icon: mem_icon,
            enabled: memory_enabled,
            delay: memory_delay,
        },
        disk: Disk {
            icon: disk_icon,
            enabled: disk_enabled,
            delay: disk_delay,
        },
        volume: Volume {
            icon: volume_icon,
            enabled: volume_enabled,
            delay: volume_delay,
            card: volume_card,
        },
        weather: Weather {
            city: weather_city,
            format: weather_format,
            icon: weather_icon,
            enabled: weather_enabled,
            delay: weather_delay,
        },
        battery: Battery {
            source: battery_source,
            icon: battery_icon,
            enabled: battery_enabled,
            delay: battery_delay,
        },
        cpu_temperature: CpuTemp {
            icon: cpu_temperature_icon,
            enabled: cpu_temperature_enabled,
            delay: cpu_temperature_delay,
        },
        uptime: Uptime {
            icon: uptime_icon,
            enabled: uptime_enabled,
            delay: uptime_delay,
        },
        mpd: Mpd {
            icon: mpd_icon,
            host: mpd_host,
            port: mpd_port,
            enabled: mpd_enabled,
            delay: mpd_delay,
        },
        spotify: Spotify {
            icon: spotify_icon,
            enabled: spotify_enabled,
            delay: spotify_delay,
        },
    }
}

// getting a f64 value from rsblocks.yml file or set default (last argument)
fn get_or_set_f64(doc: &yaml::Yaml, parent: &str, child: &str, default: f64) -> f64 {
    let val: f64 = if doc[parent][child].is_badvalue() {
        default
    } else {
        doc[parent][child].as_f64().unwrap()
    };
    val
}

// getting a boolean value from rsblocks.yml file or set it false if it does not exist
fn get_or_set_bool(doc: &yaml::Yaml, parent: &str, child: &str) -> bool {
    let val: bool;
    if doc[parent][child].is_badvalue() {
        val = false;
    } else {
        val = doc[parent][child].as_bool().unwrap()
    }
    val
}

// getting a String value from the rsblocks.yml file or set the default(last argument)
fn get_or_set_string(doc: &yaml::Yaml, parent: &str, child: &str, default_val: &str) -> String {
    let val: String;
    if doc[parent][child].is_badvalue() {
        val = String::from(default_val)
    } else {
        val = String::from(doc[parent][child].as_str().unwrap());
    }

    val
}

/*######################## LOADING CONFIGS ENDS HERE  ############################

*/

// Running the program:

pub fn run(config: Config, mut blocks: Blocks) {
    let (tx, rx) = mpsc::channel();

    // spotify thread
    if config.spotify.enabled {
        let spotify_tx = tx.clone();
        let configcp = config.clone();
        let mut spotify_data = ThreadsData::Spotify(get_spotify(&configcp));
        thread::spawn(move || loop {
            spotify_tx.send(spotify_data).unwrap();
            spotify_data = ThreadsData::Spotify(get_spotify(&configcp));
            thread::sleep(Duration::from_secs_f64(configcp.spotify.delay))
        });
    }

    // mpd thread
    if config.mpd.enabled {
        let mpd_tx = tx.clone();
        let configcp = config.clone();
        let mut mpd_data = ThreadsData::Mpd(get_mpd_current(&configcp));
        thread::spawn(move || loop {
            mpd_tx.send(mpd_data).unwrap();
            mpd_data = ThreadsData::Mpd(get_mpd_current(&configcp));
            thread::sleep(Duration::from_secs_f64(configcp.mpd.delay))
        });
    }

    // volume thread
    if config.volume.enabled {
        let volume_tx = tx.clone();
        let configcp = config.clone();
        let mut vol_data = ThreadsData::Sound(get_volume(&configcp));
        thread::spawn(move || loop {
            volume_tx.send(vol_data).unwrap();
            vol_data = ThreadsData::Sound(get_volume(&configcp));
            thread::sleep(Duration::from_secs_f64(configcp.volume.delay))
        });
    }

    // Disk thread
    if config.disk.enabled {
        let disk_tx = tx.clone();
        let configcp = config.clone();
        let mut disk_data = ThreadsData::Disk(get_disk(&configcp));
        thread::spawn(move || loop {
            disk_tx.send(disk_data).unwrap();
            disk_data = ThreadsData::Disk(get_disk(&configcp));
            thread::sleep(Duration::from_secs_f64(configcp.disk.delay))
        });
    }

    // Memory thread
    if config.memory.enabled {
        let memory_tx = tx.clone();
        let configcp = config.clone();
        let memory_data = get_memory(&configcp).unwrap();
        let mut memory_data = ThreadsData::Memory(memory_data);
        thread::spawn(move || loop {
            memory_tx.send(memory_data).unwrap();
            memory_data = ThreadsData::Memory(get_memory(&configcp).unwrap());
            thread::sleep(Duration::from_secs_f64(configcp.memory.delay))
        });
    }

    // Weather thread
    if config.weather.enabled {
        let weather_tx = tx.clone();
        let configcp = config.clone();
        let weather_data = get_weather(&configcp);
        let mut weather_data = ThreadsData::Weather(weather_data);
        thread::spawn(move || loop {
            weather_tx.send(weather_data).unwrap();
            weather_data = ThreadsData::Weather(get_weather(&configcp));
            thread::sleep(Duration::from_secs_f64(configcp.weather.delay))
        });
    }

    // Battery thread
    if config.battery.enabled {
        let battery_tx = tx.clone();
        let configcp = config.clone();
        let battery_data = get_battery(&configcp).unwrap();
        let mut battery_data = ThreadsData::Battery(battery_data);
        thread::spawn(move || loop {
            battery_tx.send(battery_data).unwrap();
            battery_data = ThreadsData::Battery(get_battery(&configcp).unwrap());
            thread::sleep(Duration::from_secs_f64(configcp.battery.delay))
        });
    }

    // Cpu temperature thread
    if config.cpu_temperature.enabled {
        let cpu_temp_tx = tx.clone();
        let configcp = config.clone();
        let cpu_temp_data = get_cpu_temp(&configcp).unwrap();
        let mut cpu_temp_data = ThreadsData::CpuTemp(cpu_temp_data);
        thread::spawn(move || loop {
            cpu_temp_tx.send(cpu_temp_data).unwrap();
            cpu_temp_data = ThreadsData::CpuTemp(get_cpu_temp(&configcp).unwrap());
            thread::sleep(Duration::from_secs_f64(configcp.cpu_temperature.delay))
        });
    }

    // Uptime thread
    if config.uptime.enabled {
        let uptime_tx = tx.clone();
        let configcp = config.clone();
        let uptime_data = get_uptime(&configcp).unwrap();
        let mut uptime_data = ThreadsData::Uptime(uptime_data);
        thread::spawn(move || loop {
            uptime_tx.send(uptime_data).unwrap();
            uptime_data = ThreadsData::Uptime(get_uptime(&configcp).unwrap());
            thread::sleep(Duration::from_secs_f64(configcp.uptime.delay))
        });
    }

    // Time thread
    {
        let time_tx = tx;
        let configcp = config;
        let mut time_data = ThreadsData::Time(get_time(&configcp));
        thread::spawn(move || loop {
            time_tx.send(time_data).unwrap();
            time_data = ThreadsData::Time(get_time(&configcp));
            thread::sleep(Duration::from_secs_f64(configcp.time.delay))
        });
    }

    //Main
    {
        // NOTE: order matters to the final format
        let mut bar: Vec<String> = vec!["".to_string(); 10];
        //iterating the values recieved from the threads
        for data in rx {
            match data {
                ThreadsData::Spotify(x) => bar[0] = x,
                ThreadsData::Mpd(x) => bar[1] = x,
                ThreadsData::Sound(x) => bar[2] = x,
                ThreadsData::Weather(x) => bar[3] = x,
                ThreadsData::Disk(x) => bar[4] = x,
                ThreadsData::Memory(x) => bar[5] = x,
                ThreadsData::CpuTemp(x) => bar[6] = x,
                ThreadsData::Battery(x) => bar[7] = x,
                ThreadsData::Uptime(x) => bar[8] = x,
                ThreadsData::Time(x) => bar[9] = x,
            }

            // match ends here
            update(&bar, &mut blocks);
        }
    }
}

pub fn update(bar: &[String], blocks: &mut Blocks) {
    let mut x = String::new();
    for i in bar.iter() {
        x.push_str(i.as_str());
    }

    blocks
        .root
        .set_title(&mut blocks.disp, &x)
        .expect("Failed to set title of root");
}

/*############################ RUNNING THE PROGRAM ENDS HERE ###########################################*/

/*




############################# HELPER FUNCTIONS BELOW ###################################
*/

pub fn get_time(config: &Config) -> String {
    let now = Local::now();

    format!(
        "  {}  {}  {}",
        config.time.icon,
        now.format(&config.time.format),
        config.seperator
    )
}

// will make a GET request from wttr.in
fn get_weather(config: &Config) -> String {
    let format = if config.weather.format.is_empty() {
        String::from("%l:+%t")
    } else {
        config.weather.format.clone()
    };

    let url = format!("http://wttr.in/{}?format=\"{}", config.weather.city, format);
    let err_string = String::from("Error");
    let res = match minreq::get(url).send() {
        Ok(resp) => match resp.as_str() {
            Ok(res_str) => res_str.trim_matches('"').to_string(),
            Err(_) => err_string,
        },
        Err(_) => err_string,
    };

    format!("  {}  {}  {}", config.weather.icon, res, config.seperator)
}

// getting disk usage
pub fn get_disk(config: &Config) -> String {
    const GB: u64 = (1024 * 1024) * 1024;
    let statvfs = nix::sys::statvfs::statvfs("/").unwrap();
    let mut disk_used = String::new();

    let total = (statvfs.blocks() * statvfs.fragment_size()) / GB;
    let available = (statvfs.blocks_free() * statvfs.fragment_size()) / GB;
    let used = total - available;

    disk_used.push_str(&format!("{}G", used));
    format!(
        "  {}  {}  {}",
        config.disk.icon, disk_used, config.seperator
    )
}

// getting volume percentage
pub fn get_volume(config: &Config) -> String {
    let card = if config.volume.card == "PULSE" {
        "pulse"
    } else {
        "default"
    };

    let mixer = Mixer::new(card, false).expect("Failed to open mixer");
    let selem_id = SelemId::new("Master", 0);
    let selem = mixer.find_selem(&selem_id).expect("Couldn't find selem");
    let selem_chan_id = SelemChannelId::FrontLeft;

    let (min, max) = selem.get_playback_volume_range();
    let mut raw_volume = selem
        .get_playback_volume(selem_chan_id)
        .expect("Failed to get raw_volume");

    let range = max - min;
    let vol = if range == 0 {
        0
    } else {
        raw_volume -= min;
        ((raw_volume as f64 / range as f64) * 100.) as u64
    };

    format!("  {}  {}%  {}", config.volume.icon, vol, config.seperator)
}

/*
mem_used = (mem_total + shmem - mem_free - mem_buffers - mem_cached - mem_srecl
thanks for htop's developer on stackoverflow for providing this algorithm to
calculate used memory.
*/
pub fn get_memory(config: &Config) -> Result<String, std::io::Error> {
    let mut buf = String::new();

    File::open("/proc/meminfo")?.read_to_string(&mut buf)?;

    let mut mem_total: u32 = 0;
    let mut shmem: u32 = 0;
    let mut mem_free: u32 = 0;
    let mut mem_buffers: u32 = 0;
    let mut mem_cached: u32 = 0;
    let mut mem_srecl: u32 = 0;

    for line in buf.lines() {
        if mem_total > 0
            && shmem > 0
            && mem_free > 0
            && mem_buffers > 0
            && mem_cached > 0
            && mem_srecl > 0
        {
            break;
        }
        if line.starts_with("MemTotal") {
            assign_val(line, &mut mem_total);
        }
        if line.starts_with("SReclaimable") {
            assign_val(line, &mut mem_srecl)
        }
        if line.starts_with("Cached") {
            assign_val(line, &mut mem_cached)
        }

        if line.starts_with("Shmem") {
            assign_val(line, &mut shmem);
        }

        if line.starts_with("MemFree") {
            assign_val(line, &mut mem_free);
        }
        if line.starts_with("Buffers") {
            assign_val(line, &mut mem_buffers);
        }
    }

    let mem_used = (mem_total + shmem - mem_free - mem_buffers - mem_cached - mem_srecl) / 1024;
    let result: String;
    if mem_used > 1000 {
        result = format!(
            "  {}  {:.1}G  {}",
            config.memory.icon,
            mem_used as f32 / 1000.0,
            config.seperator
        );
    } else {
        result = format!(
            "  {}  {}M  {}",
            config.memory.icon, mem_used, config.seperator
        );
    }
    Ok(result)
}

/*
this helper function will split the line(first argument) by the character(:)
and then parse the right splited item as u32
then assign that to the "assignable"(2nd argument).
*/
fn assign_val(line: &str, assignable: &mut u32) {
    let parsed: u32 = line.split(':').collect::<Vec<&str>>()[1]
        .trim()
        .split(' ')
        .collect::<Vec<&str>>()[0]
        .parse()
        .unwrap();
    *assignable = parsed;
}

// getting battery percentage
pub fn get_battery(config: &Config) -> Result<String, Error> {
    let battery_full_cap_file = format!(
        "/sys/class/power_supply/{}/charge_full_design",
        config.battery.source
    );
    let battery_charge_now_file = format!(
        "/sys/class/power_supply/{}/charge_now",
        config.battery.source
    );

    let mut buf = String::new();

    // FIXME: ugly error handling AGAIN fixing later, im lazy
    match File::open(&battery_full_cap_file) {
        Ok(mut file) => file.read_to_string(&mut buf)?,
        Err(_) => return Ok(String::from("check your battery source name")),
    };
    let full_design = buf.trim().parse::<u32>().unwrap();
    buf.clear();

    // NOTE: no need to error check if passed the above match
    File::open(&battery_charge_now_file)?.read_to_string(&mut buf)?;

    let charge_now = buf.trim().parse::<u32>().unwrap();

    let battery_percentage = (charge_now as f32 / full_design as f32) * 100.0;
    let result = format!(
        "  {}  {:.0}%  {}",
        config.battery.icon, battery_percentage, config.seperator
    );
    Ok(result)
}

// getting cpu temperature
pub fn get_cpu_temp(config: &Config) -> Result<String, std::io::Error> {
    let mut buf = String::new();
    File::open("/sys/class/thermal/thermal_zone0/temp")?.read_to_string(&mut buf)?;
    let value = buf.trim().parse::<f32>().unwrap();

    let result = format!(
        "  {}  {}°  {}",
        config.cpu_temperature.icon,
        value / 1000.0,
        config.seperator
    );
    Ok(result)
}

pub fn get_uptime(config: &Config) -> Result<String, std::io::Error> {
    let mut buf = String::new();
    File::open("/proc/uptime")?.read_to_string(&mut buf)?;

    let buf: f32 = buf.split(' ').collect::<Vec<&str>>()[0].parse().unwrap();

    let hour = buf.round() as u32 / 3600;
    let rem = buf as u32 - hour * 3600;
    let minutes = rem / 60;

    let uptime = if hour > 0 {
        format!("{}:{}", hour, minutes)
    } else {
        format!("{} min", minutes)
    };
    let result = format!("  {}  {}  {}", config.uptime.icon, uptime, config.seperator);
    Ok(result)
}

// yes, error handling looks fucking sucks!
// getting mpd song file
pub fn get_mpd_current(config: &Config) -> String {
    let stream_path = format!("{}:{}", config.mpd.host, config.mpd.port);
    let empty_string = String::from("");
    let mut conn = match Client::connect(&stream_path) {
        Ok(connection) => connection,
        _ => return empty_string,
    };
    let current: Song = match conn.currentsong() {
        Ok(opt) => match opt {
            Some(song) => song,
            _ => return empty_string,
        },
        _ => return empty_string,
    };

    let result = format!(
        "  {}  {}  {}",
        config.mpd.icon, current.file, config.seperator
    );

    result
}

// getting spotify current artist and title.
// FIXME: I know im lazy asshole, this error handling looks ugly, i dont like it too, need to fix soon.
fn get_spotify(config: &Config) -> String {
    let conn = match Connection::new_session() {
        Ok(conn) => conn,
        _ => return String::from(""),
    };

    let p = conn.with_proxy(
        "org.mpris.MediaPlayer2.spotify",
        "/org/mpris/MediaPlayer2",
        Duration::from_millis(5000),
    );

    let metadata: arg::PropMap = match p.get("org.mpris.MediaPlayer2.Player", "Metadata") {
        Ok(data) => data,
        _ => return String::from(""),
    };

    let title: Option<&String> = arg::prop_cast(&metadata, "xesam:title");
    let artist: Option<&Vec<String>> = arg::prop_cast(&metadata, "xesam:artist");

    let title = match title {
        Some(title) => title,
        _ => "",
    };

    let artist = match artist {
        Some(artist_vec) => match artist_vec.first() {
            Some(name) => name,
            _ => "",
        },
        None => "",
    };

    format!(
        "  {}  {} - {}  {}",
        config.spotify.icon, artist, title, config.seperator
    )
}