lumesh 0.18.2

a lighting shell ⚡ bash alternative
Documentation
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
// 复用现有的高亮逻辑

use common_macros::hash_map;
use std::collections::{BTreeMap, HashMap};

use crate::{
    Diagnostic, Expression, TokenKind,
    cmdhelper::is_valid_command,
    libs::{is_lib, is_top_or_se},
    tokenize,
};

const DEFAULT: &str = "";

pub fn highlight_dark_theme(line: &str) -> String {
    highlight(line, &get_dark_theme())
}
pub fn highlight(line: &str, theme: &HashMap<String, String>) -> String {
    let (tokens, diagnostics) = tokenize(line);

    let mut result = String::new();
    let mut is_colored = false;

    let mut is_cmd_ctx = true;
    for (token, diagnostic) in tokens.iter().zip(&diagnostics) {
        let tk = token.range.to_str(line);
        match (token.kind, tk) {
            (TokenKind::ValueSymbol, b) => {
                result.push_str(
                    theme
                        .get("value_symbol")
                        .unwrap_or(&"".to_string())
                        .as_str(),
                );
                is_colored = true;
                result.push_str(b);
            }
            (TokenKind::Punctuation, o) => {
                result.push_str(get_color("punctuation", theme));
                is_colored = true;
                result.push_str(o);
            }
            (TokenKind::Keyword, k) => {
                result.push_str(get_color("keyword", theme));
                is_colored = true;
                result.push_str(k);
            }
            (TokenKind::Operator, k) => {
                result.push_str(get_color("operator", theme));
                is_colored = true;
                result.push_str(k);
            }
            (TokenKind::OperatorPrefix, k) => {
                result.push_str(get_color("operator_prefix", theme));
                is_colored = true;
                result.push_str(k);
            }
            (TokenKind::OperatorInfix, k) => {
                result.push_str(get_color("operator_infix", theme));
                is_colored = true;
                result.push_str(k);
            }
            (TokenKind::OperatorPostfix, k) => {
                result.push_str(get_color("operator_postfix", theme));
                is_colored = true;
                result.push_str(k);
            }
            (TokenKind::StringRaw, s) => {
                result.push_str(get_color("string_raw", theme));
                is_colored = true;
                result.push_str(s);
            }
            (TokenKind::StringTemplate, s) => {
                result.push_str(get_color("string_template", theme));
                is_colored = true;
                result.push_str(s);
            }
            (TokenKind::StringLiteral, s) => {
                result.push_str(get_color("string_literal", theme));
                is_colored = true;
                result.push_str(s);
            }
            (TokenKind::IntegerLiteral, s) => {
                if let Diagnostic::InvalidNumber(e) = diagnostic {
                    result.push_str(get_color("number_error", theme));
                    result.push_str(e.to_str(line));
                    is_colored = true;
                } else {
                    result.push_str(get_color("integer_literal", theme));
                    is_colored = true;
                    result.push_str(s);
                }
            }
            (TokenKind::Radix2, s) | (TokenKind::Radix8, s) | (TokenKind::Radix16, s) => {
                if let Diagnostic::InvalidNumber(e) = diagnostic {
                    result.push_str(get_color("number_error", theme));
                    result.push_str(e.to_str(line));
                    is_colored = true;
                } else {
                    result.push_str(get_color("radix", theme));
                    is_colored = true;
                    result.push_str(s);
                }
            }
            (TokenKind::FloatLiteral, s) => {
                if let Diagnostic::InvalidNumber(e) = diagnostic {
                    result.push_str(get_color("number_error", theme));
                    result.push_str(e.to_str(line));
                    is_colored = true;
                } else {
                    result.push_str(get_color("float_literal", theme));
                    is_colored = true;
                    result.push_str(s);
                }
            }
            (TokenKind::Symbol, l) => {
                if let Diagnostic::IllegalChar(e) = diagnostic {
                    result.push_str(get_color("string_error", theme));
                    result.push_str(e.to_str(line));
                    is_colored = true;
                } else {
                    if is_cmd_ctx {
                        if is_top_or_se(l) {
                            result.push_str(get_color("builtin_cmd", theme));
                        } else if is_lib(l) {
                            result.push_str(get_color("builtin_lib", theme));
                        } else if is_valid_command(l) {
                            result.push_str(get_color("command_valid", theme));
                        } else {
                            result.push_str(get_color("warning", theme));
                        }
                    } else {
                        result.push_str(get_color("symbol", theme));
                    }
                    result.push_str(l);
                    is_colored = true;
                }
            }
            (TokenKind::Whitespace, w) => {
                result.push_str(w);
            }
            (TokenKind::LineBreak, w) => {
                result.push_str(w);
            }
            (TokenKind::Comment, w) => {
                result.push_str(get_color("comment", theme));
                is_colored = true;
                result.push_str(w);
            }
            (TokenKind::ModeTip, w) => {
                result.push_str(get_color("mode", theme));
                is_colored = true;
                result.push_str(w);
            }
            (TokenKind::Regex, s) => {
                result.push_str(get_color("regex", theme));
                is_colored = true;
                result.push_str(s);
            }
            (TokenKind::Time, s) => {
                result.push_str(get_color("time", theme));
                is_colored = true;
                result.push_str(s);
            }
            (TokenKind::StringSafe, s) => {
                result.push_str(get_color("string_safe", theme));
                is_colored = true;
                result.push_str(s);
            }
            (TokenKind::Bytes, s) => {
                result.push_str(get_color("bytes", theme));
                is_colored = true;
                result.push_str(s);
            }
        }

        // 非空白才更新
        if token.kind != TokenKind::Whitespace {
            is_cmd_ctx = matches!(token.kind, TokenKind::ModeTip | TokenKind::LineBreak)
                || matches!(tk, "|" | "{" | "[" | "(");
        }
    }

    if diagnostics.len() > tokens.len() {
        for diagnostic in &diagnostics[tokens.len()..] {
            if let Diagnostic::NotTokenized(e) = diagnostic {
                result.push_str(get_color("string_error", theme));
                result.push_str(e.to_str(line));
                is_colored = true;
            }
        }
    }

    if is_colored {
        result.push_str(get_color("reset", theme));
    }

    result
}

fn get_color<'a>(color: &str, theme: &'a HashMap<String, String>) -> &'a str {
    match theme.get(color) {
        Some(c) => c.as_str(),
        _ => DEFAULT,
    }
}

pub fn get_merged_theme(
    mut base: HashMap<String, String>,
    modify: &BTreeMap<String, Expression>,
) -> HashMap<String, String> {
    for (k, v) in modify {
        if let Expression::String(vs) = v {
            base.insert(k.clone(), vs.clone());
        }
    }
    base
}

pub fn get_dark_theme() -> HashMap<String, String> {
    hash_map! {
        // 基础颜色
        String::from("reset") => "\x1b[m\x1b[0m".to_string(),
        String::from("bold") => "\x1b[1m".to_string(),
        String::from("dim") => "\x1b[2m".to_string(),

        // One Dark 核心语法颜色 - 每种都使用不同的颜色
        String::from("keyword") => "\x1b[38;5;170m".to_string(),           // 紫色 (#C678DD)
        String::from("value_symbol") => "\x1b[38;5;141m".to_string(),      // 淡紫色
        String::from("operator") => "\x1b[38;5;67m".to_string(),           // 蓝灰色 (#56B6C2)
        String::from("operator_prefix") => "\x1b[38;5;73m".to_string(),    // 青蓝色
        String::from("operator_infix") => "\x1b[38;5;80m".to_string(),     // 深青色
        String::from("operator_postfix") => "\x1b[38;5;87m".to_string(),   // 亮青色

        // 字符串相关颜色
        String::from("string_raw") => "\x1b[38;5;114m".to_string(),        // 绿色 (#98C379)
        String::from("string_template") => "\x1b[38;5;120m".to_string(),   // 亮绿色
        String::from("string_literal") => "\x1b[38;5;107m".to_string(),    // 橄榄绿
        String::from("string_safe") => "\x1b[38;5;127m".to_string(),      //
        String::from("bytes") => "\x1b[38;5;130m".to_string(),      //
        String::from("string_error") => "\x1b[38;5;204m".to_string(),      // 红色 (#E06C75)

        // 数字和字面量
        String::from("number_literal") => "\x1b[38;5;209m".to_string(),    // 橙色 (#D19A66)
        String::from("number_error") => "\x1b[38;5;196m".to_string(),      // 亮红色
        String::from("integer_literal") => "\x1b[38;5;215m".to_string(),   // 金橙色
        String::from("radix") => "\x1b[38;5;218m".to_string(),
        String::from("float_literal") => "\x1b[38;5;221m".to_string(),     // 黄色

        // 符号和标识符
        String::from("mode") => "\x1b[38;5;200m".to_string(),              // 粉红色
        // String::from("symbol_none") => "\x1b[38;5;203m".to_string(),       // 粉红色
        String::from("builtin_cmd") => "\x1b[38;5;75m".to_string(),        // 蓝色 (#61AFEF)
        String::from("builtin_lib") => "\x1b[38;5;77m".to_string(),        // 蓝色 (#61AFEF)
        String::from("symbol") => "\x1b[38;5;81m".to_string(),             // 天蓝色

        // 注释和标点
        String::from("comment") => "\x1b[38;5;59m".to_string(),             // 灰色 (#5C6370)
        String::from("punctuation") => "\x1b[38;5;117m".to_string(), // 淡蓝色
        // String::from("punctuation_special") => "\x1b[38;5;145m".to_string(),        // 中灰色

        // REPL 和交互相关
        String::from("command_valid") => "\x1b[38;5;120m\x1b[1m".to_string(), // 绿色加粗
        // String::from("hint") => "\x1b[38;5;102m".to_string(),               // 深灰色
        // String::from("completion_cmd") => "\x1b[38;5;244m".to_string(),     // 浅灰色
        // String::from("completion_ai") => "\x1b[38;5;111m".to_string(),      // 浅蓝色

        // 新增:Time token 颜色
        // String::from("time_literal") => "\x1b[38;5;180m".to_string(),      // 金黄色 (#E5C07B)
        // String::from("time_format") => "\x1b[38;5;186m".to_string(),       // 淡黄色
        String::from("time") => "\x1b[38;5;173m".to_string(),     // 棕黄色

        // 新增:Regex token 颜色
        // String::from("regex_pattern") => "\x1b[38;5;167m".to_string(),     // 玫瑰红
        // String::from("regex_flags") => "\x1b[38;5;176m".to_string(),       // 淡紫红
        String::from("regex") => "\x1b[38;5;183m".to_string(),      // 淡粉色
        // String::from("regex_group") => "\x1b[38;5;139m".to_string(),       // 紫灰色

        // 错误和警告
        // String::from("error") => "\x1b[38;5;196m".to_string(),             // 亮红色
        String::from("warning") => "\x1b[38;5;214m".to_string(),           // 橙黄色
        // String::from("info") => "\x1b[38;5;117m".to_string(),              // 信息蓝

        // 特殊语法元素
        // String::from("whitespace") => "\x1b[0m".to_string(),               // 无颜色
        // String::from("line_break") => "\x1b[0m".to_string(),               // 无颜色
        // String::from("illegal_char") => "\x1b[38;5;160m".to_string(),      // 深红色
    }
}
pub fn get_ayu_dark_theme() -> HashMap<String, String> {
    hash_map! {
        // 基础颜色
        String::from("reset") => "\x1b[m\x1b[0m".to_string(),
        String::from("bold") => "\x1b[1m".to_string(),
        String::from("dim") => "\x1b[2m".to_string(),

        // Ayu Dark 核心语法颜色 - 基于 #0A0E14 背景
        String::from("keyword") => "\x1b[38;5;173m".to_string(),           // 橙色 (#FF8F40)
        String::from("value_symbol") => "\x1b[38;5;179m".to_string(),      // 淡橙色
        String::from("operator") => "\x1b[38;5;67m".to_string(),           // 青色 (#39BAE6)
        String::from("operator_prefix") => "\x1b[38;5;74m".to_string(),    // 亮青色
        String::from("operator_infix") => "\x1b[38;5;81m".to_string(),     // 天蓝色
        String::from("operator_postfix") => "\x1b[38;5;87m".to_string(),   // 浅青色

        // 字符串相关颜色
        String::from("string_raw") => "\x1b[38;5;107m".to_string(),        // 绿色 (#AAD94C)
        String::from("string_template") => "\x1b[38;5;113m".to_string(),   // 亮绿色
        String::from("string_literal") => "\x1b[38;5;114m".to_string(),    // 草绿色
        String::from("string_safe") => "\x1b[38;5;120m".to_string(),      //
        String::from("bytes") => "\x1b[38;5;130m".to_string(),      //
        String::from("string_error") => "\x1b[38;5;203m".to_string(),      // 红色 (#F07178)

        // 数字和字面量
        String::from("number_literal") => "\x1b[38;5;215m".to_string(),    // 黄色 (#FFEE99)
        String::from("number_error") => "\x1b[38;5;196m".to_string(),      // 亮红色
        String::from("integer_literal") => "\x1b[38;5;221m".to_string(),   // 金黄色
        String::from("radix") => "\x1b[38;5;224m".to_string(),
        String::from("float_literal") => "\x1b[38;5;228m".to_string(),     // 淡黄色

        // 符号和标识符
        String::from("mode") => "\x1b[38;5;200m".to_string(),              // 粉红色
        // String::from("symbol_none") => "\x1b[38;5;204m".to_string(),       // 粉红色
        String::from("builtin_cmd") => "\x1b[38;5;111m".to_string(),       // 蓝色 (#59C2FF)
        String::from("builtin_lib") => "\x1b[38;5;113m".to_string(),        // 蓝色 (#61AFEF)
        String::from("symbol") => "\x1b[38;5;117m".to_string(),            // 淡蓝色

        // 注释和标点
        String::from("comment") => "\x1b[38;5;102m".to_string(),           // 灰色 (#5C6773)
        // String::from("punctuation_special") => "\x1b[38;5;180m".to_string(), // 金色
        String::from("punctuation") => "\x1b[38;5;145m".to_string(),       // 中灰色

        // REPL 和交互相关
        String::from("command_valid") => "\x1b[38;5;107m\x1b[1m".to_string(), // 绿色加粗
        // String::from("hint") => "\x1b[38;5;59m".to_string(),               // 深灰色
        // String::from("completion_cmd") => "\x1b[38;5;244m".to_string(),    // 浅灰色
        // String::from("completion_ai") => "\x1b[38;5;75m".to_string(),      // 浅蓝色

        // Time token 颜色
        // String::from("time_literal") => "\x1b[38;5;186m".to_string(),      // 淡黄色
        // String::from("time_format") => "\x1b[38;5;192m".to_string(),       // 浅黄绿色
        String::from("time") => "\x1b[38;5;179m".to_string(),     // 棕橙色

        // Regex token 颜色
        // String::from("regex_pattern") => "\x1b[38;5;176m".to_string(),     // 紫红色
        // String::from("regex_flags") => "\x1b[38;5;183m".to_string(),       // 淡紫色
        String::from("regex") => "\x1b[38;5;139m".to_string(),      // 紫灰色
        // String::from("regex_group") => "\x1b[38;5;146m".to_string(),       // 灰紫色

        // 错误和警告
        // String::from("error") => "\x1b[38;5;196m".to_string(),             // 亮红色
        String::from("warning") => "\x1b[38;5;214m".to_string(),           // 橙黄色
        // String::from("info") => "\x1b[38;5;117m".to_string(),              // 信息蓝

        // 特殊语法元素
        // String::from("whitespace") => "\x1b[0m".to_string(),               // 无颜色
        // String::from("line_break") => "\x1b[0m".to_string(),               // 无颜色
        // String::from("illegal_char") => "\x1b[38;5;160m".to_string(),      // 深红色
    }
}

pub fn get_light_theme() -> HashMap<String, String> {
    hash_map! {
        // 基础颜色
        String::from("reset") => "\x1b[m\x1b[0m".to_string(),
        String::from("bold") => "\x1b[1m".to_string(),
        String::from("dim") => "\x1b[2m".to_string(),

        // Ayu Light 核心语法颜色 - 基于 #FAFAFA 背景
        String::from("keyword") => "\x1b[38;5;166m".to_string(),           // 深橙色 (#FA8D3E)
        String::from("value_symbol") => "\x1b[38;5;172m".to_string(),      // 棕橙色
        String::from("operator") => "\x1b[38;5;25m".to_string(),           // 深蓝色 (#4CBF99)
        String::from("operator_prefix") => "\x1b[38;5;31m".to_string(),    // 深青色
        String::from("operator_infix") => "\x1b[38;5;37m".to_string(),     // 青绿色
        String::from("operator_postfix") => "\x1b[38;5;43m".to_string(),   // 绿青色

        // 字符串相关颜色
        String::from("string_raw") => "\x1b[38;5;64m".to_string(),         // 深绿色 (#86B300)
        String::from("string_template") => "\x1b[38;5;70m".to_string(),    // 草绿色
        String::from("string_literal") => "\x1b[38;5;76m".to_string(),     // 亮绿色
        String::from("string_safe") => "\x1b[38;5;79m".to_string(),      //
        String::from("bytes") => "\x1b[38;5;130m".to_string(),      //
        String::from("string_error") => "\x1b[38;5;124m".to_string(),      // 深红色 (#F51818)

        // 数字和字面量
        String::from("number_literal") => "\x1b[38;5;130m".to_string(),    // 深黄色 (#A37ACC)
        String::from("number_error") => "\x1b[38;5;160m".to_string(),      // 深红色
        String::from("integer_literal") => "\x1b[38;5;136m".to_string(),   // 棕黄色
        String::from("radix") => "\x1b[38;5;139m".to_string(),
        String::from("float_literal") => "\x1b[38;5;142m".to_string(),     // 橄榄色

        // 符号和标识符
        String::from("mode") => "\x1b[38;5;200m".to_string(),              // 粉红色
        // String::from("symbol_none") => "\x1b[38;5;125m".to_string(),       // 深粉色
        String::from("builtin_cmd") => "\x1b[38;5;26m".to_string(),        // 深蓝色 (#399EE6)
        String::from("builtin_lib") => "\x1b[38;5;28m".to_string(),        // 蓝色 (#61AFEF)
        String::from("symbol") => "\x1b[38;5;32m".to_string(),             // 深青色

        // 注释和标点
        String::from("comment") => "\x1b[38;5;244m".to_string(),           // 中灰色 (#ABB0B6)
        // String::from("punctuation_special") => "\x1b[38;5;130m".to_string(), // 深黄色
        String::from("punctuation") => "\x1b[38;5;240m".to_string(),       // 深灰色

        // REPL 和交互相关
        String::from("command_valid") => "\x1b[38;5;64m\x1b[1m".to_string(), // 深绿色加粗
        // String::from("hint") => "\x1b[38;5;244m".to_string(),              // 中灰色
        // String::from("completion_cmd") => "\x1b[38;5;240m".to_string(),    // 深灰色
        // String::from("completion_ai") => "\x1b[38;5;26m".to_string(),      // 深蓝色

        // Time token 颜色
        // String::from("time_literal") => "\x1b[38;5;136m".to_string(),      // 棕黄色
        // String::from("time_format") => "\x1b[38;5;142m".to_string(),       // 橄榄色
        String::from("time") => "\x1b[38;5;148m".to_string(),     // 浅橄榄色

        // Regex token 颜色
        // String::from("regex_pattern") => "\x1b[38;5;125m".to_string(),     // 深紫色
        // String::from("regex_flags") => "\x1b[38;5;131m".to_string(),       // 深紫红色
        String::from("regex") => "\x1b[38;5;137m".to_string(),      // 棕紫色
        // String::from("regex_group") => "\x1b[38;5;143m".to_string(),       // 浅棕色

        // 错误和警告
        // String::from("error") => "\x1b[38;5;160m".to_string(),             // 深红色
        // String::from("warning") => "\x1b[38;5;166m".to_string(),           // 深橙色
        // String::from("info") => "\x1b[38;5;32m".to_string(),               // 深青色

        // 特殊语法元素
        // String::from("whitespace") => "\x1b[0m".to_string(),               // 无颜色
        // String::from("line_break") => "\x1b[0m".to_string(),               // 无颜色
        // String::from("illegal_char") => "\x1b[38;5;124m".to_string(),      // 深红色
    }
}