tgt 1.0.0

A simple TUI for Telegram
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
use crate::{
    action::Action,
    app_error::AppError,
    component_name::ComponentName,
    configs::{
        self,
        config_file::ConfigFile,
        config_type::ConfigType,
        raw::keymap_raw::{KeymapEntry, KeymapRaw},
    },
    event::Event,
};
use std::{
    collections::{hash_map::Entry, HashMap, HashSet},
    path::Path,
    str::FromStr,
};

#[derive(Clone, Debug, PartialEq, Eq)]
/// The action binding.
pub enum ActionBinding {
    /// A single action binding.
    /// It binds a single key binding to an action.
    /// In tgt a key plus a modifier is considered a single key binding.
    Single {
        action: Action,
        description: Option<String>,
    },
    /// A multiple action binding.
    /// It is used to bind multiple keys to an action.
    Multiple(HashMap<Event, ActionBinding>),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
/// The kind of keymap.
/// It is used to check for conflicts in the keymaps.
/// If a keymap entry is present in multiple keymaps, it is considered a
/// conflict
enum KeymapKind {
    CoreWindow,
    ChatList,
    Chat,
    Prompt,
}

#[derive(Clone, Debug)]
/// The keymap configuration.
pub struct KeymapConfig {
    /// The default keymap configuration.
    /// They can be used in any component.
    pub core_window: HashMap<Event, ActionBinding>,
    /// The keymap configuration for the chats list component.
    pub chat_list: HashMap<Event, ActionBinding>,
    /// The keymap configuration for the chat component.
    pub chat: HashMap<Event, ActionBinding>,
    /// The keymap configuration for the prompt component.
    pub prompt: HashMap<Event, ActionBinding>,
}
/// The keymap configuration implementation.
impl KeymapConfig {
    /// Get the default keymap configuration.
    ///
    /// # Returns
    /// The default keymap configuration.
    pub fn default_result() -> Result<Self, AppError<()>> {
        configs::deserialize_to_config_into::<KeymapRaw, Self>(Path::new(
            &configs::custom::default_config_keymap_file_path()?,
        ))
    }
    /// Get the keys associated with an action.
    ///
    /// # Arguments
    /// * `map` - A hashmap of event and action binding.
    /// * `value` - An action.
    ///
    /// # Returns
    /// A vector of events associated with the action.
    pub fn get_key_of_single_action(
        &self,
        component_name: ComponentName,
        value: Action,
    ) -> Vec<Event> {
        let map = self.get_map_of(Some(component_name));
        let mut keys = vec![];
        for (k, v) in map.iter() {
            match v {
                ActionBinding::Single { action, .. } if *action == value => {
                    keys.push(k.clone());
                }
                _ => {}
            }
        }
        keys
    }
    /// Print the configuration file error.
    /// It is used to print the error when the configuration file is not
    /// correct. It prints the unrecognized settings.
    ///
    /// # Arguments
    /// * `v` - A vector of strings that represents the unrecognized settings.
    fn print_config_file_error(s: &str, v: Vec<String>) {
        eprintln!(
            "\n\
         [TGT] ConfigFileError: Some setting were not recognized, the {} filed is {:?}\n    \
         Please check the {} configuration file in the config directory or\n    \
         the default config file in the GitHub repository.",
            s,
            v,
            ConfigType::Keymap.as_default_filename()
        );
    }
    /// Convert a vector of keymap entries to a hashmap of event and action
    /// binding.
    /// When the keymap entries are read from the default configuration file,
    /// they are stored in a vector. This function converts the vector to a
    /// hashmap.
    ///
    /// # Arguments
    /// * `keymaps` - A vector of keymap entries.
    /// * `kind` - The kind of keymap.
    ///
    /// # Returns
    /// A hashmap of event and action binding.
    fn keymaps_vec_to_map(
        keymaps: Vec<KeymapEntry>,
        kind: KeymapKind,
    ) -> HashMap<Event, ActionBinding> {
        let mut hashmap = HashMap::new();

        for keymap in keymaps {
            let event: Vec<Event> = keymap
                .keys
                .iter()
                .map(|k| match Event::from_str(k) {
                    Ok(e) => e,
                    Err(e) => {
                        if let AppError::InvalidEvent(err) = e {
                            tracing::warn!(err);
                        }
                        Event::Unknown
                    }
                })
                .filter(|e| *e != Event::Unknown)
                .collect();
            if keymap.keys.len() != event.len() {
                tracing::warn!(
                    "Some events were not recognized in {:?} section for key: {:?}",
                    kind,
                    keymap.keys
                );
                Self::print_config_file_error("keys", keymap.keys);
                std::process::exit(1);
            }
            let action: Action = match Action::from_str(&keymap.command) {
                Ok(a) => a,
                Err(e) => {
                    if let AppError::InvalidAction(err) = e {
                        tracing::warn!(err);
                    }
                    Action::Unknown
                }
            };
            if action == Action::Unknown {
                tracing::warn!(
                    "Some actions were not recognized in {:?} section for command: {:?}",
                    kind,
                    keymap.command
                );
                Self::print_config_file_error("command", Vec::from([keymap.command]));
                std::process::exit(1);
            }

            let description = keymap.description.clone();

            if let Err(AppError::AlreadyBound) =
                Self::insert_keymap(&mut hashmap, event, action, description, kind.clone())
            {
                tracing::warn!(
                    "Keymap entry {:?} is already present in the {:?} section",
                    keymap
                        .keys
                        .iter()
                        .map(|k| k.to_string())
                        .collect::<Vec<String>>(),
                    kind
                );
            }
        }
        hashmap
    }

    /// Insert a keymap entry into the keymap hashmap.
    /// It is used to insert a keymap entry into the keymap hashmap. It is
    /// recursive and it is used to insert a keymap entry with multiple events.
    /// It returns an error if the key is already bound to a command.
    /// Note that can not exist two keymap entries that start with the same
    /// key (event), for example "q" and ["q", "q"]. If the keymap entry
    /// already exists, it returns an error.
    ///
    /// # Arguments
    /// * `keymap` - A mutable reference to the keymap hashmap.
    /// * `event` - A vector of events.
    /// * `action` - An action.
    /// * `description` - An optional description.
    /// * `kind` - The kind of keymap.
    ///
    /// # Returns
    /// An error if the key is already bound to a command.
    fn insert_keymap(
        keymap: &mut HashMap<Event, ActionBinding>,
        event: Vec<Event>,
        action: Action,
        description: Option<String>,
        kind: KeymapKind,
    ) -> Result<(), AppError<()>> {
        let num_events = event.len();
        match num_events {
            0 => Ok(()),
            1 => {
                match keymap.entry(event[0].clone()) {
                    Entry::Occupied(_) => {
                        tracing::error!(
                            "Key {:?} already bound to a command in {:?} section",
                            event[0].to_string(),
                            kind
                        );
                        return Err(AppError::AlreadyBound);
                    }
                    Entry::Vacant(e) => {
                        e.insert(ActionBinding::Single {
                            action,
                            description,
                        });
                    }
                }
                Ok(())
            }
            _ => match keymap.entry(event[0].clone()) {
                Entry::Occupied(mut entry) => match entry.get_mut() {
                    ActionBinding::Multiple(ref mut map) => {
                        Self::insert_keymap(map, event[1..].to_vec(), action, description, kind)
                    }
                    _ => {
                        tracing::error!(
                            "Key {:?} already bound to a command in {:?} section",
                            event[0].to_string(),
                            kind
                        );
                        Err(AppError::AlreadyBound)
                    }
                },
                Entry::Vacant(entry) => {
                    let mut map = HashMap::new();
                    let res = Self::insert_keymap(
                        &mut map,
                        event[1..].to_vec(),
                        action,
                        description,
                        kind,
                    );
                    if res.is_ok() {
                        entry.insert(ActionBinding::Multiple(map));
                    }
                    res
                }
            },
        }
    }

    /// Check for duplicates in the keymaps.
    /// It is used to check for duplicates in the keymaps. If a keymap entry is
    /// present in multiple keymaps, it is considered a conflict.
    ///
    /// # Arguments
    /// * `default` - The default keymap.
    /// * `chat_list` - The chat list keymap.
    /// * `chat` - The chat keymap.
    /// * `prompt` - The prompt keymap.
    fn check_duplicates(
        default: &HashMap<Event, ActionBinding>,
        chat_list: &HashMap<Event, ActionBinding>,
        chat: &HashMap<Event, ActionBinding>,
        prompt: &HashMap<Event, ActionBinding>,
    ) {
        let mut all: Vec<&Event> = vec![];
        all.extend(default.keys());
        all.extend(chat_list.keys());
        all.extend(chat.keys());
        all.extend(prompt.keys());

        let mut duplicates = HashSet::new();
        for k in all {
            if !duplicates.insert(k) {
                tracing::warn!(
                    "Keymap entry {:?} is already present in another keymap",
                    k.to_string(),
                );
            }
        }
    }

    /// Get the keymap configuration of a component.
    /// It is used to get the keymap configuration of a component.
    ///
    /// # Arguments
    /// * `component_name` - The name of the component.
    ///
    /// # Returns
    /// The keymap configuration of the component.
    pub fn get_map_of(
        &self,
        component_name: Option<ComponentName>,
    ) -> &HashMap<Event, ActionBinding> {
        match component_name {
            Some(componnt) => match componnt {
                ComponentName::ChatList => &self.chat_list,
                ComponentName::Chat => &self.chat,
                ComponentName::Prompt => &self.prompt,
                _ => &self.core_window,
            },
            None => &self.core_window,
        }
    }
}

/// The implementation of the configuration file for the keymap.
impl ConfigFile for KeymapConfig {
    type Raw = KeymapRaw;

    fn get_type() -> ConfigType {
        ConfigType::Keymap
    }

    fn override_fields() -> bool {
        true
    }

    fn merge(&mut self, other: Option<Self::Raw>) -> Self {
        match other {
            None => self.clone(),
            Some(other) => {
                tracing::info!("Merging keymap config");
                // It is important that the default keymap is merged first.
                // The other keymaps can override the default keymap, but the
                // default keymap can not override the other keymaps.
                if let Some(default) = other.core_window {
                    for (k, v) in Self::keymaps_vec_to_map(default.keymap, KeymapKind::CoreWindow) {
                        if self.core_window.insert(k.clone(), v).is_some() {
                            tracing::warn!(
                                    "Keymap entry {:?} is already present in the default section, you are overriding it",
                                    k.to_string()
                                );
                        }
                    }
                }
                if let Some(chat_list) = other.chat_list {
                    for (k, v) in Self::keymaps_vec_to_map(chat_list.keymap, KeymapKind::ChatList) {
                        if self.chat_list.insert(k.clone(), v).is_some() {
                            tracing::warn!(
                                    "Keymap entry {:?} is already present in the chat list section, you are overriding it",
                                    k.to_string()
                                );
                        }
                    }
                }
                if let Some(chat) = other.chat {
                    for (k, v) in Self::keymaps_vec_to_map(chat.keymap, KeymapKind::Chat) {
                        if self.chat.insert(k.clone(), v).is_some() {
                            tracing::warn!(
                                    "Keymap entry {:?} is already present in the chat section, you are overriding it",
                                    k.to_string()
                                );
                        }
                    }
                }
                if let Some(prompt) = other.prompt {
                    for (k, v) in Self::keymaps_vec_to_map(prompt.keymap, KeymapKind::Prompt) {
                        if self.prompt.insert(k.clone(), v).is_some() {
                            tracing::warn!(
                                    "Keymap entry {:?} is already present in the prompt section, you are overriding it",
                                    k.to_string()
                                );
                        }
                    }
                }
                Self::check_duplicates(
                    &self.core_window,
                    &self.chat_list,
                    &self.chat,
                    &self.prompt,
                );
                self.clone()
            }
        }
    }
}
/// The default keymap configuration.
impl Default for KeymapConfig {
    fn default() -> Self {
        Self::default_result().unwrap()
    }
}
/// The conversion from the raw keymap configuration to the keymap
/// configuration.
impl From<KeymapRaw> for KeymapConfig {
    fn from(raw: KeymapRaw) -> Self {
        let core_window =
            Self::keymaps_vec_to_map(raw.core_window.unwrap().keymap, KeymapKind::CoreWindow);
        let chat_list =
            Self::keymaps_vec_to_map(raw.chat_list.unwrap().keymap, KeymapKind::ChatList);
        let chat = Self::keymaps_vec_to_map(raw.chat.unwrap().keymap, KeymapKind::Chat);
        let prompt = Self::keymaps_vec_to_map(raw.prompt.unwrap().keymap, KeymapKind::Prompt);
        Self::check_duplicates(&core_window, &chat_list, &chat, &prompt);
        Self {
            core_window,
            chat_list,
            chat,
            prompt,
        }
    }
}

#[cfg(test)]
mod tests {
    use {
        crate::{
            action::Action,
            configs::{
                config_file::ConfigFile,
                custom::keymap_custom::{ActionBinding, KeymapConfig},
                raw::keymap_raw::{KeymapEntry, KeymapMode, KeymapRaw},
            },
            event::Event,
        },
        std::str::FromStr,
    };

    #[test]
    fn test_keymap_config_default() {
        let keymap_config = KeymapConfig::default();
        assert_eq!(keymap_config.core_window.len(), 15);
        assert_eq!(keymap_config.chat_list.len(), 5);
        assert_eq!(keymap_config.chat.len(), 9);
        assert_eq!(keymap_config.prompt.len(), 0);
    }

    #[test]
    fn test_keymap_config_from_raw_empty() {
        let keymap_raw = KeymapRaw {
            core_window: Some(KeymapMode { keymap: vec![] }),
            chat_list: Some(KeymapMode { keymap: vec![] }),
            chat: Some(KeymapMode { keymap: vec![] }),
            prompt: Some(KeymapMode { keymap: vec![] }),
        };
        let keymap_config = KeymapConfig::from(keymap_raw);
        assert_eq!(keymap_config.core_window.len(), 0);
        assert_eq!(keymap_config.chat_list.len(), 0);
        assert_eq!(keymap_config.chat.len(), 0);
        assert_eq!(keymap_config.prompt.len(), 0);
    }

    #[test]
    fn test_keymap_config_from_raw() {
        let keymap_raw = KeymapRaw {
            core_window: Some(KeymapMode {
                keymap: vec![KeymapEntry {
                    keys: vec!["q".to_string()],
                    command: "quit".to_string(),
                    description: None,
                }],
            }),
            chat_list: Some(KeymapMode { keymap: vec![] }),
            chat: Some(KeymapMode { keymap: vec![] }),
            prompt: Some(KeymapMode { keymap: vec![] }),
        };
        let keymap_config = KeymapConfig::from(keymap_raw);
        assert_eq!(keymap_config.core_window.len(), 1);
        assert_eq!(keymap_config.chat_list.len(), 0);
        assert_eq!(keymap_config.chat.len(), 0);
        assert_eq!(keymap_config.prompt.len(), 0);
    }

    #[test]
    fn test_keymap_config_merge() {
        let keymap_raw = KeymapRaw {
            core_window: Some(KeymapMode {
                keymap: vec![KeymapEntry {
                    keys: vec!["q".to_string()],
                    command: "quit".to_string(),
                    description: None,
                }],
            }),
            chat_list: Some(KeymapMode { keymap: vec![] }),
            chat: Some(KeymapMode { keymap: vec![] }),
            prompt: Some(KeymapMode { keymap: vec![] }),
        };
        let mut keymap_config = KeymapConfig::from(keymap_raw);
        let keymap_raw = KeymapRaw {
            core_window: Some(KeymapMode {
                keymap: vec![KeymapEntry {
                    keys: vec!["q".to_string()],
                    command: "render".to_string(),
                    description: None,
                }],
            }),
            chat_list: Some(KeymapMode { keymap: vec![] }),
            chat: Some(KeymapMode { keymap: vec![] }),
            prompt: Some(KeymapMode { keymap: vec![] }),
        };
        keymap_config = keymap_config.merge(Some(keymap_raw));
        assert_eq!(keymap_config.core_window.len(), 1);
        assert_eq!(keymap_config.chat_list.len(), 0);
        assert_eq!(keymap_config.chat.len(), 0);
        assert_eq!(keymap_config.prompt.len(), 0);
        assert_eq!(
            keymap_config
                .core_window
                .get(&Event::from_str("q").unwrap())
                .unwrap()
                .clone(),
            ActionBinding::Single {
                action: Action::from_str("render").unwrap(),
                description: None
            }
        );
    }

    #[test]
    fn test_keymap_config_override_fields() {
        assert!(KeymapConfig::override_fields());
    }

    #[test]
    fn test_merge_all_fields() {
        let mut keymap_config = KeymapConfig::default();
        let keymap_raw = KeymapRaw {
            core_window: Some(KeymapMode { keymap: vec![] }),
            chat_list: Some(KeymapMode { keymap: vec![] }),
            chat: Some(KeymapMode { keymap: vec![] }),
            prompt: Some(KeymapMode { keymap: vec![] }),
        };
        keymap_config = keymap_config.merge(Some(keymap_raw));
        assert_eq!(keymap_config.core_window.len(), 15);
        assert_eq!(keymap_config.chat_list.len(), 5);
        assert_eq!(keymap_config.chat.len(), 9);
        assert_eq!(keymap_config.prompt.len(), 0);
    }

    #[test]
    fn test_get_type() {
        assert_eq!(
            KeymapConfig::get_type(),
            crate::configs::config_type::ConfigType::Keymap
        );
    }
}