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
use ratatui::style::Color;
use unicode_width::UnicodeWidthChar;
#[derive(Clone, Debug, Default)]
pub struct RelfLineStyle {
pub fg: Option<Color>,
pub bg: Option<Color>,
pub bold: bool,
}
#[derive(Clone, Debug)]
pub struct RelfEntry {
pub lines: Vec<String>, // For backward compatibility and inside entries
pub bg_color: Color,
pub original_index: usize, // Index in the original JSON (before filtering)
// Fields for corner layout (outside entries)
pub name: Option<String>,
pub url: Option<String>,
pub context: Option<String>,
pub percentage: Option<i64>,
// Fields for inside entries
pub date: Option<String>,
}
#[derive(Clone, Debug, Default)]
pub struct RelfRenderResult {
pub lines: Vec<String>,
pub styles: Vec<RelfLineStyle>,
pub entries: Vec<RelfEntry>,
}
pub struct Renderer;
impl Renderer {
pub fn display_width_str(s: &str) -> usize {
s.chars()
.map(|c| UnicodeWidthChar::width(c).unwrap_or(0))
.sum()
}
pub fn prefix_display_width(s: &str, char_pos: usize) -> usize {
s.chars()
.take(char_pos)
.map(|c| UnicodeWidthChar::width(c).unwrap_or(0))
.sum()
}
pub fn slice_columns(s: &str, start_cols: usize, width_cols: usize) -> String {
if width_cols == 0 {
return String::new();
}
let mut sum = 0usize;
let mut start_idx = 0usize;
for (i, c) in s.chars().enumerate() {
let w = UnicodeWidthChar::width(c).unwrap_or(0);
if sum >= start_cols {
start_idx = i;
break;
}
sum += w;
start_idx = i + 1;
}
let mut out = String::new();
let mut used = 0usize;
for c in s.chars().skip(start_idx) {
let w = UnicodeWidthChar::width(c).unwrap_or(0);
if used + w > width_cols {
break;
}
out.push(c);
used += w;
}
out
}
pub fn render_relf(json_input: &str, filter_pattern: &str) -> RelfRenderResult {
if let Ok(json_value) = serde_json::from_str::<serde_json::Value>(json_input) {
let mut result = RelfRenderResult::default();
if let Some(obj) = json_value.as_object() {
let card_bg = Color::Rgb(26, 28, 34);
let mut global_index = 0; // Track the original index across all entries
for (section_key, section_value) in obj {
if section_key == "outside" || section_key == "inside" {
if let Some(section_array) = section_value.as_array() {
for item in section_array {
let original_index = global_index;
global_index += 1;
if let Some(item_obj) = item.as_object() {
if section_key == "outside" {
let mut entry_lines = Vec::new();
let name = item_obj
.get("name")
.and_then(|v| v.as_str())
.unwrap_or("");
let context = item_obj
.get("context")
.and_then(|v| v.as_str())
.unwrap_or("");
let url = item_obj
.get("url")
.and_then(|v| v.as_str())
.unwrap_or("");
let percentage = item_obj
.get("percentage")
.and_then(|v| v.as_i64());
entry_lines.push(name.to_string());
if !context.is_empty() {
entry_lines.push(context.to_string());
}
if !url.is_empty() {
entry_lines.push(url.to_string());
}
// Only add percentage line if not null
if let Some(pct) = percentage {
entry_lines.push(format!("{}%", pct));
}
// Apply filter if pattern is provided
if !filter_pattern.is_empty() {
let matches = entry_lines.iter().any(|line| {
line.to_lowercase().contains(&filter_pattern.to_lowercase())
});
if !matches {
continue; // Skip this entry
}
}
result.entries.push(RelfEntry {
lines: entry_lines,
bg_color: card_bg,
original_index,
name: Some(name.to_string()),
url: if !url.is_empty() { Some(url.to_string()) } else { None },
context: if !context.is_empty() { Some(context.to_string()) } else { None },
percentage,
date: None,
});
} else if section_key == "inside" {
let date = item_obj
.get("date")
.and_then(|v| v.as_str())
.unwrap_or("");
let context = item_obj
.get("context")
.and_then(|v| v.as_str())
.unwrap_or("");
let mut entry_lines = Vec::new();
if !date.is_empty() {
entry_lines.push(date.to_string());
}
if !context.is_empty() {
entry_lines.push(context.to_string());
}
// Apply filter if pattern is provided
if !filter_pattern.is_empty() {
let matches = entry_lines.iter().any(|line| {
line.to_lowercase().contains(&filter_pattern.to_lowercase())
});
if !matches {
continue; // Skip this entry
}
}
result.entries.push(RelfEntry {
lines: entry_lines,
bg_color: card_bg,
original_index,
name: None,
url: None,
context: if !context.is_empty() { Some(context.to_string()) } else { None },
percentage: None,
date: if !date.is_empty() { Some(date.to_string()) } else { None },
});
}
}
}
}
}
}
}
return result;
}
let lines: Vec<String> = json_input
.lines()
.enumerate()
.take(50)
.map(|(i, line)| format!("{:4}: {}", i + 1, line))
.collect();
let mut result = RelfRenderResult::default();
result
.lines
.push("⚠ Not valid JSON - showing raw text file content".to_string());
result.styles.push(RelfLineStyle {
fg: Some(Color::Yellow),
bg: None,
bold: true,
});
result.lines.push("".to_string());
result.styles.push(RelfLineStyle::default());
for line in lines {
result.lines.push(line);
result.styles.push(RelfLineStyle::default());
}
result
}
pub fn render_json(json_input: &str) -> Vec<String> {
json_input.lines().map(|line| line.to_string()).collect()
}
}