pyc_shell/shell/prompt/modules/
language.rs

1//! # Language
2//!
3//! `Language` is the module which resolve the lang prompt token
4
5/*
6*
7*   Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com
8*
9* 	This file is part of "Pyc"
10*
11*   Pyc is free software: you can redistribute it and/or modify
12*   it under the terms of the GNU General Public License as published by
13*   the Free Software Foundation, either version 3 of the License, or
14*   (at your option) any later version.
15*
16*   Pyc is distributed in the hope that it will be useful,
17*   but WITHOUT ANY WARRANTY; without even the implied warranty of
18*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19*   GNU General Public License for more details.
20*
21*   You should have received a copy of the GNU General Public License
22*   along with Pyc.  If not, see <http://www.gnu.org/licenses/>.
23*
24*/
25
26use crate::translator::lang::Language;
27
28use super::colors::PromptColor;
29
30pub(crate) const PROMPT_LANG: &str = "${LANG}";
31
32pub fn language_to_str(language: Language) -> String {
33    let mut lang_str: String = language.to_string();
34    if lang_str.len() < 3 {
35        lang_str.push_str("   ");
36    }
37    match language {
38        Language::Belarusian => String::from(format!(
39            "{}{}{}{}{}{}{}",
40            PromptColor::Red.to_string(),
41            lang_str.chars().nth(0).unwrap_or(' '),
42            PromptColor::Green.to_string(),
43            lang_str.chars().nth(1).unwrap_or(' '),
44            PromptColor::White.to_string(),
45            lang_str.chars().nth(2).unwrap_or(' '),
46            PromptColor::Reset.to_string()
47        )),
48        Language::Bulgarian => String::from(format!(
49            "{}{}{}{}{}{}{}",
50            PromptColor::White.to_string(),
51            lang_str.chars().nth(0).unwrap_or(' '),
52            PromptColor::Green.to_string(),
53            lang_str.chars().nth(1).unwrap_or(' '),
54            PromptColor::Red.to_string(),
55            lang_str.chars().nth(2).unwrap_or(' '),
56            PromptColor::Reset.to_string()
57        )),
58        Language::Russian => String::from(format!(
59            "{}{}{}{}{}{}{}",
60            PromptColor::White.to_string(),
61            lang_str.chars().nth(0).unwrap_or(' '),
62            PromptColor::Blue.to_string(),
63            lang_str.chars().nth(1).unwrap_or(' '),
64            PromptColor::Red.to_string(),
65            lang_str.chars().nth(2).unwrap_or(' '),
66            PromptColor::Reset.to_string()
67        )),
68        Language::Serbian => String::from(format!(
69            "{}{}{}{}{}{}{}",
70            PromptColor::Red.to_string(),
71            lang_str.chars().nth(0).unwrap_or(' '),
72            PromptColor::Blue.to_string(),
73            lang_str.chars().nth(1).unwrap_or(' '),
74            PromptColor::White.to_string(),
75            lang_str.chars().nth(2).unwrap_or(' '),
76            PromptColor::Reset.to_string()
77        )),
78        Language::Ukrainian => String::from(format!(
79            "{}{}{}{}{}{}{}",
80            PromptColor::Cyan.to_string(),
81            lang_str.chars().nth(0).unwrap_or(' '),
82            PromptColor::Yellow.to_string(),
83            lang_str.chars().nth(1).unwrap_or(' '),
84            PromptColor::Cyan.to_string(),
85            lang_str.chars().nth(2).unwrap_or(' '),
86            PromptColor::Reset.to_string()
87        )),
88        Language::Nil => String::from(format!(
89            "{}{}{}{}{}",
90            PromptColor::Blink.to_string(),
91            lang_str.chars().nth(0).unwrap_or(' '),
92            lang_str.chars().nth(1).unwrap_or(' '),
93            lang_str.chars().nth(2).unwrap_or(' '),
94            PromptColor::Reset.to_string()
95        ))
96    }
97}
98
99#[cfg(test)]
100mod tests {
101
102    use super::*;
103
104    #[test]
105    fn test_prompt_lang_flag() {
106        // Belarusian
107        let expected_str = String::from("\x1b[31mб\x1b[32mе\x1b[37mл\x1b[0m");
108        println!("{}", language_to_str(Language::Belarusian));
109        assert_eq!(language_to_str(Language::Belarusian), expected_str);
110        // Bulgarian
111        let expected_str = String::from("\x1b[37mб\x1b[32mл\x1b[31mг\x1b[0m");
112        println!("{}", language_to_str(Language::Bulgarian));
113        assert_eq!(language_to_str(Language::Bulgarian), expected_str);
114        // Russian
115        let expected_str = String::from("\x1b[37mр\x1b[34mу\x1b[31mс\x1b[0m");
116        println!("{}", language_to_str(Language::Russian));
117        assert_eq!(language_to_str(Language::Russian), expected_str);
118        // Serbian
119        let expected_str = String::from("\x1b[31mс\x1b[34mр\x1b[37mб\x1b[0m");
120        println!("{}", language_to_str(Language::Serbian));
121        assert_eq!(language_to_str(Language::Serbian), expected_str);
122        // Ukrainian
123        let expected_str = String::from("\x1b[36mу\x1b[33mк\x1b[36mр\x1b[0m");
124        println!("{}", language_to_str(Language::Ukrainian));
125        assert_eq!(language_to_str(Language::Ukrainian), expected_str);
126        // Nil
127        let expected_str = String::from("\x1b[5mnil\x1b[0m");
128        println!("{}", language_to_str(Language::Nil));
129        assert_eq!(language_to_str(Language::Nil), expected_str);
130    }
131}