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
use ratatui::{
style::{Color, Style},
text::Span,
};
use crate::config::ColorScheme;
// JSON syntax highlighting
pub fn highlight_json_line(line: &str, colorscheme: &ColorScheme) -> Vec<Span<'static>> {
let mut spans = Vec::new();
let mut chars = line.chars().peekable();
let mut current = String::new();
while let Some(ch) = chars.next() {
match ch {
'"' => {
// Push accumulated text
if !current.is_empty() {
spans.push(Span::styled(
current.clone(),
Style::default().fg(colorscheme.text),
));
current.clear();
}
// Start collecting string
let mut string_content = String::from("\"");
let mut escaped = false;
while let Some(next_ch) = chars.next() {
string_content.push(next_ch);
if next_ch == '\\' && !escaped {
escaped = true;
} else if next_ch == '"' && !escaped {
break;
} else {
escaped = false;
}
}
// Determine if this is a key (followed by ':')
let mut temp_chars = chars.clone();
let mut is_key = false;
while let Some(peek_ch) = temp_chars.next() {
if peek_ch == ':' {
is_key = true;
break;
} else if !peek_ch.is_whitespace() {
break;
}
}
let color = if is_key {
colorscheme.key // Keys in light blue
} else {
colorscheme.string // String values in orange/peach
};
spans.push(Span::styled(
string_content,
Style::default().fg(color),
));
}
'{' | '}' | '[' | ']' => {
if !current.is_empty() {
spans.push(Span::styled(
current.clone(),
Style::default().fg(colorscheme.text),
));
current.clear();
}
spans.push(Span::styled(
ch.to_string(),
Style::default().fg(colorscheme.bracket), // Yellow/gold
));
}
':' | ',' => {
if !current.is_empty() {
spans.push(Span::styled(
current.clone(),
Style::default().fg(colorscheme.text),
));
current.clear();
}
spans.push(Span::styled(
ch.to_string(),
Style::default().fg(Color::White),
));
}
't' | 'f' | 'n' => {
// Check for true, false, null
let peek_str: String = std::iter::once(ch)
.chain(chars.clone().take(4))
.collect();
if peek_str.starts_with("true") || peek_str.starts_with("false") || peek_str.starts_with("null") {
if !current.is_empty() {
spans.push(Span::styled(
current.clone(),
Style::default().fg(colorscheme.text),
));
current.clear();
}
let keyword = if peek_str.starts_with("true") {
chars.nth(2); // skip 'r', 'u', 'e'
"true"
} else if peek_str.starts_with("false") {
chars.nth(3); // skip 'a', 'l', 's', 'e'
"false"
} else {
chars.nth(2); // skip 'u', 'l', 'l'
"null"
};
spans.push(Span::styled(
keyword.to_string(),
Style::default().fg(colorscheme.boolean), // Purple/blue
));
} else {
current.push(ch);
}
}
'0'..='9' | '-' => {
// Numbers
let mut num = String::from(ch);
while let Some(&next_ch) = chars.peek() {
if next_ch.is_ascii_digit() || next_ch == '.' || next_ch == 'e' || next_ch == 'E' || next_ch == '-' || next_ch == '+' {
num.push(chars.next().unwrap());
} else {
break;
}
}
if !current.is_empty() {
spans.push(Span::styled(
current.clone(),
Style::default().fg(colorscheme.text),
));
current.clear();
}
spans.push(Span::styled(
num,
Style::default().fg(colorscheme.number), // Light green
));
}
_ => {
current.push(ch);
}
}
}
if !current.is_empty() {
spans.push(Span::styled(
current,
Style::default().fg(colorscheme.text),
));
}
if spans.is_empty() {
spans.push(Span::styled(
String::new(),
Style::default().fg(colorscheme.text),
));
}
spans
}