memtui 0.3.0

An interactive TUI (Terminal User Interface) for browsing and inspecting key-value stores (Redis, Memcached, etc.)
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
use crate::backend::utils::is_json;
use crate::types::Value;
use ratatui::style::{Color, Style};
use ratatui::text::{Line, Span};
use serde_json;

use super::{FormatError, Formatter};

/// Configuration for JSON formatting and coloring
#[derive(Debug, Clone)]
pub struct JsonColorConfig {
    pub indent: usize,
    pub key_color: Color,
    pub string_color: Color,
    pub number_color: Color,
    pub boolean_color: Color,
    pub null_color: Color,
    pub brace_color: Color,
    pub bracket_color: Color,
    pub comma_color: Color,
    pub colon_color: Color,
}

impl Default for JsonColorConfig {
    fn default() -> Self {
        Self {
            indent: 2,
            key_color: Color::Cyan,
            string_color: Color::Green,
            number_color: Color::Magenta,
            boolean_color: Color::Yellow,
            null_color: Color::DarkGray,
            brace_color: Color::White,
            bracket_color: Color::White,
            comma_color: Color::White,
            colon_color: Color::White,
        }
    }
}

/// JSON formatter with syntax highlighting for ratatui
pub struct JsonFormatter {
    config: JsonColorConfig,
}

impl JsonFormatter {
    pub fn new(config: JsonColorConfig) -> Self {
        Self { config }
    }

    /// Format JSON into styled lines for ratatui
    pub fn format_to_lines(&self, value: &Value) -> Result<Vec<Line<'static>>, FormatError> {
        let json_str = String::from_utf8(value.data.clone()).map_err(|_| FormatError {
            message: "Invalid UTF-8".to_string(),
        })?;

        // Parse JSON to validate and prettify
        let parsed: serde_json::Value =
            serde_json::from_str(&json_str).map_err(|e| FormatError {
                message: format!("Invalid JSON: {}", e),
            })?;

        // Format with indentation (serde_json uses 2 spaces by default)
        let mut pretty = serde_json::to_string_pretty(&parsed).map_err(|e| FormatError {
            message: format!("Failed to format JSON: {}", e),
        })?;

        // Apply configured indent
        pretty = self.apply_indent(&pretty);

        // Colorize the formatted JSON
        Ok(self.colorize_json(&pretty))
    }

    /// Apply configured indentation to a JSON string
    /// This converts serde_json's default 2-space indent to the configured indent
    fn apply_indent(&self, json: &str) -> String {
        if self.config.indent == 2 {
            return json.to_string();
        }

        let lines: Vec<&str> = json.lines().collect();
        let mut result = Vec::new();

        for line in lines {
            // Count leading spaces (serde_json uses 2 spaces per indent level)
            let leading_spaces = line.chars().take_while(|&c| c == ' ').count();
            let indent_level = leading_spaces / 2;
            let new_indent = " ".repeat(indent_level * self.config.indent);
            let content = &line[leading_spaces..];
            result.push(format!("{}{}", new_indent, content));
        }

        result.join("\n")
    }

    /// Colorize JSON string into styled lines
    pub fn colorize_json(&self, json: &str) -> Vec<Line<'static>> {
        // Apply configured indent before colorizing
        let json_with_indent = self.apply_indent(json);
        let mut lines = Vec::new();

        for line in json_with_indent.lines() {
            let spans = self.colorize_line(line);
            lines.push(Line::from(spans));
        }

        lines
    }

    /// Colorize a single line of JSON
    fn colorize_line(&self, line: &str) -> Vec<Span<'static>> {
        let mut spans = Vec::new();
        let mut current = String::new();
        let mut in_string = false;
        let mut in_key = false;
        let mut escape_next = false;
        let mut chars = line.chars().peekable();

        // Track if we're in a key position (after : we're in value)
        let mut after_colon = false;

        #[allow(clippy::while_let_on_iterator)]
        while let Some(ch) = chars.next() {
            if escape_next {
                current.push(ch);
                escape_next = false;
                continue;
            }

            match ch {
                '\\' if in_string => {
                    current.push(ch);
                    escape_next = true;
                }
                '"' => {
                    if in_string {
                        // End of string
                        current.push(ch);
                        let color = if in_key {
                            self.config.key_color
                        } else {
                            self.config.string_color
                        };
                        spans.push(Span::styled(current.clone(), Style::default().fg(color)));
                        current.clear();
                        in_string = false;
                        in_key = false;
                    } else {
                        // Flush any accumulated text
                        if !current.is_empty() {
                            spans.push(Span::raw(current.clone()));
                            current.clear();
                        }
                        // Start of string - check if it's a key or value
                        in_string = true;
                        in_key = !after_colon;
                        current.push(ch);
                    }
                }
                ':' if !in_string => {
                    // Flush any accumulated text
                    if !current.is_empty() {
                        spans.push(Span::raw(current.clone()));
                        current.clear();
                    }
                    spans.push(Span::styled(
                        ch.to_string(),
                        Style::default().fg(self.config.colon_color),
                    ));
                    after_colon = true;
                }
                ',' if !in_string => {
                    // Flush any accumulated text (might be a number or boolean)
                    self.flush_value(&mut current, &mut spans, after_colon);
                    spans.push(Span::styled(
                        ch.to_string(),
                        Style::default().fg(self.config.comma_color),
                    ));
                    after_colon = false;
                }
                '{' | '}' if !in_string => {
                    // Flush any accumulated text
                    if !current.is_empty() {
                        spans.push(Span::raw(current.clone()));
                        current.clear();
                    }
                    spans.push(Span::styled(
                        ch.to_string(),
                        Style::default().fg(self.config.brace_color),
                    ));
                    after_colon = false;
                }
                '[' | ']' if !in_string => {
                    // Flush any accumulated text
                    if !current.is_empty() {
                        spans.push(Span::raw(current.clone()));
                        current.clear();
                    }
                    spans.push(Span::styled(
                        ch.to_string(),
                        Style::default().fg(self.config.bracket_color),
                    ));
                    after_colon = false;
                }
                _ => {
                    current.push(ch);
                }
            }
        }

        // Flush any remaining text
        self.flush_value(&mut current, &mut spans, after_colon);

        spans
    }

    /// Flush accumulated value text with appropriate coloring
    fn flush_value(&self, current: &mut String, spans: &mut Vec<Span<'static>>, after_colon: bool) {
        if current.is_empty() {
            return;
        }

        let trimmed = current.trim();
        if !trimmed.is_empty() {
            // Determine color based on content
            let color = if after_colon {
                if trimmed == "true" || trimmed == "false" {
                    self.config.boolean_color
                } else if trimmed == "null" {
                    self.config.null_color
                } else if trimmed.parse::<f64>().is_ok() {
                    self.config.number_color
                } else {
                    Color::White // Default for whitespace and other chars
                }
            } else {
                Color::White // Default
            };

            if color != Color::White {
                // Keep leading/trailing whitespace separate
                let leading_ws = current.len() - current.trim_start().len();
                let trailing_ws = current.len() - current.trim_end().len();

                if leading_ws > 0 {
                    spans.push(Span::raw(current[..leading_ws].to_string()));
                }
                spans.push(Span::styled(
                    trimmed.to_string(),
                    Style::default().fg(color),
                ));
                if trailing_ws > 0 {
                    spans.push(Span::raw(
                        current[current.len() - trailing_ws..].to_string(),
                    ));
                }
            } else {
                spans.push(Span::raw(current.clone()));
            }
        } else {
            spans.push(Span::raw(current.clone()));
        }

        current.clear();
    }
}

impl Formatter for JsonFormatter {
    fn name(&self) -> &str {
        "json"
    }

    fn can_format(&self, value: &Value) -> bool {
        is_json(&value.data)
    }

    fn format(&self, value: &Value) -> Result<String, FormatError> {
        let json_str = String::from_utf8(value.data.clone()).map_err(|_| FormatError {
            message: "Invalid UTF-8".to_string(),
        })?;

        // Parse and prettify JSON
        let parsed: serde_json::Value =
            serde_json::from_str(&json_str).map_err(|e| FormatError {
                message: format!("Invalid JSON: {}", e),
            })?;

        // Format with indentation (serde_json uses 2 spaces by default)
        let mut pretty = serde_json::to_string_pretty(&parsed).map_err(|e| FormatError {
            message: format!("Failed to format JSON: {}", e),
        })?;

        // Apply configured indent
        pretty = self.apply_indent(&pretty);

        Ok(pretty)
    }
}

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

    #[test]
    fn test_json_formatter_can_format() {
        use crate::types::ValueType;

        let formatter = JsonFormatter::new(JsonColorConfig::default());
        let json_value = Value {
            data: b"{\"key\": \"value\"}".to_vec(),
            value_type: ValueType::Json,
            encoding: None,
        };
        assert!(formatter.can_format(&json_value));

        let text_value = Value {
            data: b"plain text".to_vec(),
            value_type: ValueType::String,
            encoding: None,
        };
        assert!(!formatter.can_format(&text_value));
    }

    #[test]
    fn test_json_formatter_format() {
        use crate::types::ValueType;

        let formatter = JsonFormatter::new(JsonColorConfig::default());
        let json_value = Value {
            data: b"{\"key\":\"value\",\"num\":123}".to_vec(),
            value_type: ValueType::Json,
            encoding: None,
        };
        let result = formatter.format(&json_value);
        assert!(result.is_ok());
        let formatted = result.unwrap();
        assert!(formatted.contains("\"key\""));
        assert!(formatted.contains("\"value\""));
        assert!(formatted.contains("123"));
    }

    #[test]
    fn test_json_formatter_format_invalid_json() {
        use crate::types::ValueType;

        let formatter = JsonFormatter::new(JsonColorConfig::default());
        let invalid_value = Value {
            data: b"{invalid}".to_vec(),
            value_type: ValueType::Json,
            encoding: None,
        };
        let result = formatter.format(&invalid_value);
        assert!(result.is_err());
    }

    #[test]
    fn test_json_formatter_format_to_lines() {
        use crate::types::ValueType;

        let formatter = JsonFormatter::new(JsonColorConfig::default());
        let json_value = Value {
            data: br#"{"key":"value","num":123,"bool":true,"null":null}"#.to_vec(),
            value_type: ValueType::Json,
            encoding: None,
        };
        let result = formatter.format_to_lines(&json_value);
        assert!(result.is_ok());
        let lines = result.unwrap();
        assert!(!lines.is_empty());
    }

    #[test]
    fn test_json_formatter_indent() {
        use crate::types::ValueType;

        // Test with 4-space indent
        let config = JsonColorConfig {
            indent: 4,
            ..Default::default()
        };
        let formatter = JsonFormatter::new(config);

        let json_value = Value {
            data: br#"{"key":"value","nested":{"inner":123}}"#.to_vec(),
            value_type: ValueType::Json,
            encoding: None,
        };

        let result = formatter.format(&json_value).unwrap();
        let lines: Vec<&str> = result.lines().collect();

        // Check that nested objects use 4-space indent
        // First line should be "{"
        // Second line should have 4 spaces before "key"
        // Third line should have 4 spaces before "nested"
        // Fourth line should have 8 spaces before "inner" (2 levels * 4 spaces)

        assert!(lines.len() > 3);
        let key_line = lines.iter().find(|l| l.contains("\"key\"")).unwrap();
        let nested_line = lines.iter().find(|l| l.contains("\"nested\"")).unwrap();
        let inner_line = lines.iter().find(|l| l.contains("\"inner\"")).unwrap();

        // Check leading spaces
        let key_indent = key_line.chars().take_while(|c| *c == ' ').count();
        let nested_indent = nested_line.chars().take_while(|c| *c == ' ').count();
        let inner_indent = inner_line.chars().take_while(|c| *c == ' ').count();

        assert_eq!(
            key_indent, 4,
            "Key should have 4-space indent, got {}",
            key_indent
        );
        assert_eq!(
            nested_indent, 4,
            "Nested should have 4-space indent, got {}",
            nested_indent
        );
        assert_eq!(
            inner_indent, 8,
            "Inner should have 8-space indent (2 levels), got {}",
            inner_indent
        );
    }
}