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
use indexmap::indexmap;
use indexmap::map::IndexMap;
use nu_engine::CallExt;
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
use nu_protocol::record;
use nu_protocol::{
    ast::Call, engine::Command, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData,
    PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use once_cell::sync::Lazy;
use std::sync::{atomic::AtomicBool, Arc};

// Character used to separate directories in a Path Environment variable on windows is ";"
#[cfg(target_family = "windows")]
const ENV_PATH_SEPARATOR_CHAR: char = ';';
// Character used to separate directories in a Path Environment variable on linux/mac/unix is ":"
#[cfg(not(target_family = "windows"))]
const ENV_PATH_SEPARATOR_CHAR: char = ':';

#[derive(Clone)]
pub struct Char;

static CHAR_MAP: Lazy<IndexMap<&'static str, String>> = Lazy::new(|| {
    indexmap! {
        // These are some regular characters that either can't be used or
        // it's just easier to use them like this.

        // This are the "normal" characters section
        "newline" => '\n'.to_string(),
        "enter" => '\n'.to_string(),
        "nl" => '\n'.to_string(),
        "line_feed" => '\n'.to_string(),
        "lf" => '\n'.to_string(),
        "carriage_return" => '\r'.to_string(),
        "cr" => '\r'.to_string(),
        "crlf" => "\r\n".to_string(),
        "tab" => '\t'.to_string(),
        "sp" => ' '.to_string(),
        "space" => ' '.to_string(),
        "pipe" => '|'.to_string(),
        "left_brace" => '{'.to_string(),
        "lbrace" => '{'.to_string(),
        "right_brace" => '}'.to_string(),
        "rbrace" => '}'.to_string(),
        "left_paren" => '('.to_string(),
        "lp" => '('.to_string(),
        "lparen" => '('.to_string(),
        "right_paren" => ')'.to_string(),
        "rparen" => ')'.to_string(),
        "rp" => ')'.to_string(),
        "left_bracket" => '['.to_string(),
        "lbracket" => '['.to_string(),
        "right_bracket" => ']'.to_string(),
        "rbracket" => ']'.to_string(),
        "single_quote" => '\''.to_string(),
        "squote" => '\''.to_string(),
        "sq" => '\''.to_string(),
        "double_quote" => '\"'.to_string(),
        "dquote" => '\"'.to_string(),
        "dq" => '\"'.to_string(),
        "path_sep" => std::path::MAIN_SEPARATOR.to_string(),
        "psep" => std::path::MAIN_SEPARATOR.to_string(),
        "separator" => std::path::MAIN_SEPARATOR.to_string(),
        "esep" => ENV_PATH_SEPARATOR_CHAR.to_string(),
        "env_sep" => ENV_PATH_SEPARATOR_CHAR.to_string(),
        "tilde" => '~'.to_string(),                                // ~
        "twiddle" => '~'.to_string(),                              // ~
        "squiggly" => '~'.to_string(),                             // ~
        "home" => '~'.to_string(),                                 // ~
        "hash" => '#'.to_string(),                                 // #
        "hashtag" => '#'.to_string(),                              // #
        "pound_sign" => '#'.to_string(),                           // #
        "sharp" => '#'.to_string(),                                // #
        "root" => '#'.to_string(),                                 // #

        // This is the unicode section
        // Unicode names came from https://www.compart.com/en/unicode
        // Private Use Area (U+E000-U+F8FF)
        // Unicode can't be mixed with Ansi or it will break width calculation
        "nf_branch" => '\u{e0a0}'.to_string(),                     // 
        "nf_segment" => '\u{e0b0}'.to_string(),                    // 
        "nf_left_segment" => '\u{e0b0}'.to_string(),               // 
        "nf_left_segment_thin" => '\u{e0b1}'.to_string(),          // 
        "nf_right_segment" => '\u{e0b2}'.to_string(),              // 
        "nf_right_segment_thin" => '\u{e0b3}'.to_string(),         // 
        "nf_git" => '\u{f1d3}'.to_string(),                        // 
        "nf_git_branch" => "\u{e709}\u{e0a0}".to_string(),         // 
        "nf_folder1" => '\u{f07c}'.to_string(),                    // 
        "nf_folder2" => '\u{f115}'.to_string(),                    // 
        "nf_house1" => '\u{f015}'.to_string(),                     // 
        "nf_house2" => '\u{f7db}'.to_string(),                     // 

        "identical_to" => '\u{2261}'.to_string(),                  // ≡
        "hamburger" => '\u{2261}'.to_string(),                     // ≡
        "not_identical_to" => '\u{2262}'.to_string(),              // ≢
        "branch_untracked" => '\u{2262}'.to_string(),              // ≢
        "strictly_equivalent_to" => '\u{2263}'.to_string(),        // ≣
        "branch_identical" => '\u{2263}'.to_string(),              // ≣

        "upwards_arrow" => '\u{2191}'.to_string(),                 // ↑
        "branch_ahead" => '\u{2191}'.to_string(),                  // ↑
        "downwards_arrow" => '\u{2193}'.to_string(),               // ↓
        "branch_behind" => '\u{2193}'.to_string(),                 // ↓
        "up_down_arrow" => '\u{2195}'.to_string(),                 // ↕
        "branch_ahead_behind" => '\u{2195}'.to_string(),           // ↕

        "black_right_pointing_triangle" => '\u{25b6}'.to_string(), // ▶
        "prompt" => '\u{25b6}'.to_string(),                        // ▶
        "vector_or_cross_product" => '\u{2a2f}'.to_string(),       // ⨯
        "failed" => '\u{2a2f}'.to_string(),                        // ⨯
        "high_voltage_sign" => '\u{26a1}'.to_string(),             // ⚡
        "elevated" => '\u{26a1}'.to_string(),                      // ⚡

        // This is the emoji section
        // Weather symbols
        // https://www.babelstone.co.uk/Unicode/whatisit.html
        "sun" => "☀️".to_string(),         //2600 + fe0f
        "sunny" => "☀️".to_string(),       //2600 + fe0f
        "sunrise" => "☀️".to_string(),     //2600 + fe0f
        "moon" => "🌛".to_string(),        //1f31b
        "cloudy" => "☁️".to_string(),      //2601 + fe0f
        "cloud" => "☁️".to_string(),       //2601 + fe0f
        "clouds" => "☁️".to_string(),      //2601 + fe0f
        "rainy" => "🌦️".to_string(),       //1f326 + fe0f
        "rain" => "🌦️".to_string(),        //1f326 + fe0f
        "foggy" => "🌫️".to_string(),       //1f32b + fe0f
        "fog" => "🌫️".to_string(),         //1f32b + fe0f
        "mist" => '\u{2591}'.to_string(),  //2591
        "haze" => '\u{2591}'.to_string(),  //2591
        "snowy" => "❄️".to_string(),       //2744 + fe0f
        "snow" => "❄️".to_string(),        //2744 + fe0f
        "thunderstorm" => "🌩️".to_string(),//1f329 + fe0f
        "thunder" => "🌩️".to_string(),     //1f329 + fe0f

        // This is the "other" section
        "bel" => '\x07'.to_string(),       // Terminal Bell
        "backspace" => '\x08'.to_string(), // Backspace

        // separators
        "file_separator" => '\x1c'.to_string(),
        "file_sep"  => '\x1c'.to_string(),
        "fs" => '\x1c'.to_string(),
        "group_separator" => '\x1d'.to_string(),
        "group_sep" => '\x1d'.to_string(),
        "gs" => '\x1d'.to_string(),
        "record_separator" => '\x1e'.to_string(),
        "record_sep" => '\x1e'.to_string(),
        "rs" => '\x1e'.to_string(),
        "unit_separator" => '\x1f'.to_string(),
        "unit_sep" => '\x1f'.to_string(),
        "us" => '\x1f'.to_string(),
    }
});

impl Command for Char {
    fn name(&self) -> &str {
        "char"
    }

    fn signature(&self) -> Signature {
        Signature::build("char")
            .input_output_types(vec![(Type::Nothing, Type::Any)])
            .optional(
                "character",
                SyntaxShape::Any,
                "The name of the character to output.",
            )
            .rest("rest", SyntaxShape::Any, "Multiple Unicode bytes.")
            .switch("list", "List all supported character names", Some('l'))
            .switch("unicode", "Unicode string i.e. 1f378", Some('u'))
            .switch("integer", "Create a codepoint from an integer", Some('i'))
            .allow_variants_without_examples(true)
            .category(Category::Strings)
    }

    fn is_const(&self) -> bool {
        true
    }

    fn usage(&self) -> &str {
        "Output special characters (e.g., 'newline')."
    }

    fn search_terms(&self) -> Vec<&str> {
        vec!["line break", "newline", "Unicode"]
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                description: "Output newline",
                example: r#"char newline"#,
                result: Some(Value::test_string("\n")),
            },
            Example {
                description: "List available characters",
                example: r#"char --list"#,
                result: None,
            },
            Example {
                description: "Output prompt character, newline and a hamburger menu character",
                example: r#"(char prompt) + (char newline) + (char hamburger)"#,
                result: Some(Value::test_string("\u{25b6}\n\u{2261}")),
            },
            Example {
                description: "Output Unicode character",
                example: r#"char --unicode 1f378"#,
                result: Some(Value::test_string("\u{1f378}")),
            },
            Example {
                description: "Create Unicode from integer codepoint values",
                example: r#"char --integer (0x60 + 1) (0x60 + 2)"#,
                result: Some(Value::test_string("ab")),
            },
            Example {
                description: "Output multi-byte Unicode character",
                example: r#"char --unicode 1F468 200D 1F466 200D 1F466"#,
                result: Some(Value::test_string(
                    "\u{1F468}\u{200D}\u{1F466}\u{200D}\u{1F466}",
                )),
            },
        ]
    }

    fn run_const(
        &self,
        working_set: &StateWorkingSet,
        call: &Call,
        _input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let call_span = call.head;
        let list = call.has_flag_const(working_set, "list")?;
        let integer = call.has_flag_const(working_set, "integer")?;
        let unicode = call.has_flag_const(working_set, "unicode")?;
        let ctrlc = working_set.permanent().ctrlc.clone();

        // handle -l flag
        if list {
            return generate_character_list(ctrlc, call.head);
        }

        // handle -i flag
        if integer {
            let int_args: Vec<i64> = call.rest_const(working_set, 0)?;
            handle_integer_flag(int_args, call, call_span)
        }
        // handle -u flag
        else if unicode {
            let string_args: Vec<String> = call.rest_const(working_set, 0)?;
            handle_unicode_flag(string_args, call, call_span)
        }
        // handle the rest
        else {
            let string_args: Vec<String> = call.rest_const(working_set, 0)?;
            handle_the_rest(string_args, call, call_span)
        }
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        _input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let call_span = call.head;
        let list = call.has_flag(engine_state, stack, "list")?;
        let integer = call.has_flag(engine_state, stack, "integer")?;
        let unicode = call.has_flag(engine_state, stack, "unicode")?;
        let ctrlc = engine_state.ctrlc.clone();

        // handle -l flag
        if list {
            return generate_character_list(ctrlc, call_span);
        }

        // handle -i flag
        if integer {
            let int_args: Vec<i64> = call.rest(engine_state, stack, 0)?;
            handle_integer_flag(int_args, call, call_span)
        }
        // handle -u flag
        else if unicode {
            let string_args: Vec<String> = call.rest(engine_state, stack, 0)?;
            handle_unicode_flag(string_args, call, call_span)
        }
        // handle the rest
        else {
            let string_args: Vec<String> = call.rest(engine_state, stack, 0)?;
            handle_the_rest(string_args, call, call_span)
        }
    }
}

fn generate_character_list(
    ctrlc: Option<Arc<AtomicBool>>,
    call_span: Span,
) -> Result<PipelineData, ShellError> {
    Ok(CHAR_MAP
        .iter()
        .map(move |(name, s)| {
            let unicode = Value::string(
                s.chars()
                    .map(|c| format!("{:x}", c as u32))
                    .collect::<Vec<String>>()
                    .join(" "),
                call_span,
            );
            let record = record! {
                "name" => Value::string(*name, call_span),
                "character" => Value::string(s, call_span),
                "unicode" => unicode,
            };

            Value::record(record, call_span)
        })
        .into_pipeline_data(ctrlc))
}

fn handle_integer_flag(
    int_args: Vec<i64>,
    call: &Call,
    call_span: Span,
) -> Result<PipelineData, ShellError> {
    if int_args.is_empty() {
        return Err(ShellError::MissingParameter {
            param_name: "missing at least one unicode character".into(),
            span: call_span,
        });
    }
    let mut multi_byte = String::new();
    for (i, &arg) in int_args.iter().enumerate() {
        let span = call
            .positional_nth(i)
            .expect("Unexpected missing argument")
            .span;
        multi_byte.push(integer_to_unicode_char(arg, span)?)
    }
    Ok(Value::string(multi_byte, call_span).into_pipeline_data())
}

fn handle_unicode_flag(
    string_args: Vec<String>,
    call: &Call,
    call_span: Span,
) -> Result<PipelineData, ShellError> {
    if string_args.is_empty() {
        return Err(ShellError::MissingParameter {
            param_name: "missing at least one unicode character".into(),
            span: call_span,
        });
    }
    let mut multi_byte = String::new();
    for (i, arg) in string_args.iter().enumerate() {
        let span = call
            .positional_nth(i)
            .expect("Unexpected missing argument")
            .span;
        multi_byte.push(string_to_unicode_char(arg, span)?)
    }
    Ok(Value::string(multi_byte, call_span).into_pipeline_data())
}

fn handle_the_rest(
    string_args: Vec<String>,
    call: &Call,
    call_span: Span,
) -> Result<PipelineData, ShellError> {
    if string_args.is_empty() {
        return Err(ShellError::MissingParameter {
            param_name: "missing name of the character".into(),
            span: call_span,
        });
    }
    let special_character = str_to_character(&string_args[0]);
    if let Some(output) = special_character {
        Ok(Value::string(output, call_span).into_pipeline_data())
    } else {
        Err(ShellError::TypeMismatch {
            err_message: "error finding named character".into(),
            span: call
                .positional_nth(0)
                .expect("Unexpected missing argument")
                .span,
        })
    }
}

fn integer_to_unicode_char(value: i64, t: Span) -> Result<char, ShellError> {
    let decoded_char = value.try_into().ok().and_then(std::char::from_u32);

    if let Some(ch) = decoded_char {
        Ok(ch)
    } else {
        Err(ShellError::TypeMismatch {
            err_message: "not a valid Unicode codepoint".into(),
            span: t,
        })
    }
}

fn string_to_unicode_char(s: &str, t: Span) -> Result<char, ShellError> {
    let decoded_char = u32::from_str_radix(s, 16)
        .ok()
        .and_then(std::char::from_u32);

    if let Some(ch) = decoded_char {
        Ok(ch)
    } else {
        Err(ShellError::TypeMismatch {
            err_message: "error decoding Unicode character".into(),
            span: t,
        })
    }
}

fn str_to_character(s: &str) -> Option<String> {
    CHAR_MAP.get(s).map(|s| s.into())
}

#[cfg(test)]
mod tests {
    use super::Char;

    #[test]
    fn examples_work_as_expected() {
        use crate::test_examples;

        test_examples(Char {})
    }
}