JHHM

lol-cal

terminal lolesports schedule viewer
git clone https://jhhm.no/repos/lol-cal.git

lol-cal/src/config/mod.rs

get raw file: mod.rs

1use std::{
2    collections::HashMap,
3    ops::{Deref, DerefMut},
4    path::PathBuf,
5};
6
7use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
8use lazy_static::lazy_static;
9use ratatui::{
10    style::{Style, Stylize},
11    symbols::line,
12    widgets::BorderType,
13};
14use utils::{get_config_dir, get_data_dir};
15
16use crate::event::AppEvent;
17
18mod error;
19pub use error::Error;
20pub mod parser;
21pub mod utils;
22
23lazy_static! {
24    pub static ref PROJECT_NAME: String = env!("CARGO_CRATE_NAME").to_uppercase().to_string();
25}
26
27#[derive(Debug)]
28pub struct KeyBindings(pub HashMap<KeyEvent, AppEvent>);
29
30impl Default for KeyBindings {
31    fn default() -> Self {
32        let mut map = HashMap::new();
33
34        macro_rules! bind {
35            ($code:expr => $event:expr) => {
36                map.insert(KeyEvent::new($code, KeyModifiers::NONE), $event);
37            };
38            ($code:expr, $mods:expr => $event:expr) => {
39                map.insert(KeyEvent::new($code, $mods), $event);
40            };
41        }
42
43        bind!(KeyCode::Char('q') => AppEvent::Quit);
44        bind!(KeyCode::Char('c'), KeyModifiers::CONTROL => AppEvent::Quit);
45
46        bind!(KeyCode::Char('k') => AppEvent::Up);
47        bind!(KeyCode::Char('j') => AppEvent::Down);
48        bind!(KeyCode::Char('h') => AppEvent::Left);
49        bind!(KeyCode::Char('l') => AppEvent::Right);
50
51        bind!(KeyCode::Up => AppEvent::Up);
52        bind!(KeyCode::Down => AppEvent::Down);
53        bind!(KeyCode::Left => AppEvent::Left);
54        bind!(KeyCode::Right => AppEvent::Right);
55        bind!(KeyCode::Char(' ') => AppEvent::Select);
56
57        bind!(KeyCode::Char('g'), KeyModifiers::CONTROL => AppEvent::GotoToday);
58        bind!(KeyCode::Char('s'), KeyModifiers::CONTROL => AppEvent::ToggleSpoilResults);
59        bind!(KeyCode::Char('s'), KeyModifiers::SHIFT => AppEvent::ToggleSpoilMatches);
60
61        bind!(KeyCode::Char('r') => AppEvent::ReloadSchedule);
62
63        KeyBindings(map)
64    }
65}
66
67impl Deref for KeyBindings {
68    type Target = HashMap<KeyEvent, AppEvent>;
69
70    fn deref(&self) -> &Self::Target {
71        &self.0
72    }
73}
74
75impl DerefMut for KeyBindings {
76    fn deref_mut(&mut self) -> &mut Self::Target {
77        &mut self.0
78    }
79}
80
81#[derive(Debug)]
82pub struct Styles {
83    pub border: Option<BorderType>,
84    pub border_set: Option<line::Set>,
85    pub default: Style,
86    pub highlight: Style,
87    pub selected: Style,
88    pub winner: Option<Style>,
89    pub loser: Option<Style>,
90}
91
92impl Default for Styles {
93    fn default() -> Self {
94        Styles {
95            border: Some(BorderType::Plain),
96            border_set: Some(line::NORMAL),
97            default: Style::default(),
98            highlight: Style::default().blue(),
99            selected: Style::default().red().bold(),
100            winner: Some(Style::default().green()),
101            loser: None,
102        }
103    }
104}
105
106#[derive(Debug)]
107pub struct Config {
108    pub config_dir: PathBuf,
109    pub data_dir: PathBuf,
110    pub default_leagues: Vec<String>,
111    pub spoil_results: bool,
112    pub spoil_matches: bool,
113    pub automatic_reload: bool,
114    pub keybindings: KeyBindings,
115    pub style: Styles,
116}
117
118impl Default for Config {
119    fn default() -> Self {
120        Self {
121            config_dir: get_config_dir(),
122            data_dir: get_data_dir(),
123            default_leagues: Vec::new(),
124            spoil_results: false,
125            spoil_matches: true,
126            automatic_reload: true,
127            keybindings: KeyBindings::default(),
128            style: Styles::default(),
129        }
130    }
131}