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
mod app;
mod input;
mod json_ops;
mod navigation;
mod rendering;
mod ui;
use anyhow::Result;
use clap::{Arg, Command};
use crossterm::{
cursor,
event::{DisableMouseCapture, EnableMouseCapture},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{backend::CrosstermBackend, Terminal};
use std::{fs, io::stdout, panic, path::PathBuf};
use app::{App, FormatMode};
fn main() -> Result<()> {
// Set up panic handler to properly clean up terminal on crash
let original_hook = panic::take_hook();
panic::set_hook(Box::new(move |panic_info| {
// Clean up terminal
let _ = disable_raw_mode();
let _ = execute!(stdout(), LeaveAlternateScreen, DisableMouseCapture);
let _ = execute!(stdout(), cursor::Show);
// Call the original panic handler
original_hook(panic_info);
}));
let matches = Command::new("revw")
.version(env!("BUILD_VERSION"))
.about("A vim-like TUI for managing notes and resources")
.arg(Arg::new("file").help("JSON file to view").index(1))
.arg(
Arg::new("json")
.long("json")
.help("Use JSON editing mode")
.action(clap::ArgAction::SetTrue),
)
.arg(
Arg::new("stdout")
.long("stdout")
.help("Output to stdout instead of interactive mode")
.action(clap::ArgAction::SetTrue),
)
.arg(
Arg::new("output")
.short('o')
.long("output")
.help("Output to file (use '-' for stdout)")
.value_name("FILE"),
)
.arg(
Arg::new("inside")
.long("inside")
.help("Output only INSIDE section")
.action(clap::ArgAction::SetTrue),
)
.arg(
Arg::new("outside")
.long("outside")
.help("Output only OUTSIDE section")
.action(clap::ArgAction::SetTrue),
)
.get_matches();
let format_mode = if matches.get_flag("json") {
FormatMode::Edit
} else {
FormatMode::View
};
let stdout_mode = matches.get_flag("stdout");
let output_file = matches.get_one::<String>("output");
let inside_only = matches.get_flag("inside");
let outside_only = matches.get_flag("outside");
// If stdout mode or output file specified, run in non-interactive mode
if stdout_mode || output_file.is_some() {
let mut app = App::new(format_mode);
// Load file if provided
if let Some(file_path) = matches.get_one::<String>("file") {
let path = PathBuf::from(file_path);
let content = fs::read_to_string(&path)
.map_err(|e| {
eprintln!("Error: Cannot read file '{}': {}", file_path, e);
std::process::exit(1);
})
.unwrap();
app.json_input = content;
app.convert_json();
let output = if format_mode == FormatMode::Edit {
// In Edit mode, output the JSON as-is
app.json_input.clone()
} else {
// In View mode, format the entries for text output
if app.relf_entries.is_empty() {
// No entries parsed, output raw content or rendered lines
if !app.rendered_content.is_empty() {
app.rendered_content.join("\n")
} else {
app.json_input.clone()
}
} else {
// Format entries as text
let mut output_lines = Vec::new();
let mut outside_entries: Vec<String> = Vec::new();
let mut inside_entries: Vec<String> = Vec::new();
// Parse JSON to determine which section each entry belongs to
if let Ok(json_value) = serde_json::from_str::<serde_json::Value>(&app.json_input) {
if let Some(obj) = json_value.as_object() {
if let Some(outside) = obj.get("outside").and_then(|v| v.as_array()) {
for item in outside {
if let Some(item_obj) = item.as_object() {
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());
let mut entry = String::new();
entry.push_str(name);
if !context.is_empty() {
entry.push_str(&format!("\n{}", context));
}
if !url.is_empty() {
entry.push_str(&format!("\n{}", url));
}
// Only add percentage if not null
if let Some(pct) = percentage {
entry.push_str(&format!("\n{}%", pct));
}
outside_entries.push(entry);
}
}
}
if let Some(inside) = obj.get("inside").and_then(|v| v.as_array()) {
for item in inside {
if let Some(item_obj) = item.as_object() {
let mut entry_parts = Vec::new();
for (_key, value) in item_obj {
let value_str = match value {
serde_json::Value::String(s) => s.clone(),
serde_json::Value::Number(n) => n.to_string(),
serde_json::Value::Bool(b) => b.to_string(),
_ => value.to_string(),
};
if !value_str.is_empty() {
entry_parts.push(value_str);
}
}
inside_entries.push(entry_parts.join("\n"));
}
}
}
}
}
// Filter based on --inside or --outside flags
if inside_only && !outside_only {
// Only INSIDE section
if !inside_entries.is_empty() {
output_lines.push("INSIDE".to_string());
output_lines.push("".to_string());
for entry in inside_entries {
output_lines.push(entry);
output_lines.push("".to_string());
}
}
} else if outside_only && !inside_only {
// Only OUTSIDE section
if !outside_entries.is_empty() {
output_lines.push("OUTSIDE".to_string());
output_lines.push("".to_string());
for entry in outside_entries {
output_lines.push(entry);
output_lines.push("".to_string());
}
}
} else {
// Both sections (default behavior)
if !outside_entries.is_empty() {
output_lines.push("OUTSIDE".to_string());
output_lines.push("".to_string());
for entry in outside_entries {
output_lines.push(entry);
output_lines.push("".to_string());
}
}
if !inside_entries.is_empty() {
output_lines.push("INSIDE".to_string());
output_lines.push("".to_string());
for entry in inside_entries {
output_lines.push(entry);
output_lines.push("".to_string());
}
}
}
output_lines.join("\n")
}
};
if let Some(output_path) = output_file {
if output_path == "-" {
// Output to stdout
println!("{}", output);
} else {
// Output to file
fs::write(output_path, output)?;
}
} else {
// stdout flag was used
println!("{}", output);
}
} else {
eprintln!("Error: No input file specified");
std::process::exit(1);
}
} else {
// Interactive mode with better error handling
let mut app = App::new(format_mode);
// Load file if provided - no existence check for quick loading
if let Some(file_path) = matches.get_one::<String>("file") {
let path = PathBuf::from(file_path);
app.load_file(path);
}
// Set up terminal with error handling
let setup_result = (|| -> Result<Terminal<CrosstermBackend<std::io::Stdout>>> {
enable_raw_mode()?;
let mut stdout = stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
execute!(stdout, cursor::Hide)?;
let backend = CrosstermBackend::new(stdout);
Ok(Terminal::new(backend)?)
})();
let mut terminal = match setup_result {
Ok(term) => term,
Err(e) => {
eprintln!("Failed to initialize terminal: {}", e);
return Err(e);
}
};
// Run the app with proper cleanup
let res = input::run_app(&mut terminal, app);
// Always clean up, even if there was an error
let _ = disable_raw_mode();
let _ = execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
);
let _ = execute!(terminal.backend_mut(), cursor::Show);
let _ = terminal.show_cursor();
if let Err(err) = res {
eprintln!("Application error: {}", err);
std::process::exit(1);
}
}
Ok(())
}