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
use super::App;
use std::{{fs, path::PathBuf, time::Instant}};
use serde_json::json;
impl App {
pub fn load_file(&mut self, path: PathBuf) {
// Path cleaning - remove all kinds of quotes and whitespace
let path_display = path.display().to_string();
let cleaned_path_str = path_display
.trim()
.trim_matches('"')
.trim_matches('\'')
.trim_matches('`')
.trim();
let fixed_path = PathBuf::from(cleaned_path_str);
let final_path_display = fixed_path.display().to_string();
match fs::read_to_string(&fixed_path) {
Ok(content) => {
self.json_input = content;
let path_changed = self.file_path.as_ref() != Some(&fixed_path);
self.file_path = Some(fixed_path.clone());
if path_changed {
self.file_path_changed = true;
}
self.set_status(&format!("Loaded: {}", final_path_display));
self.convert_json();
// Reset card selection and cursor position when opening a new file
if path_changed {
self.selected_entry_index = 0;
self.hscroll = 0;
self.content_cursor_line = 0;
self.content_cursor_col = 0;
self.scroll = 0;
}
}
Err(e) => {
// If file doesn't exist, create it with default entries
if e.kind() == std::io::ErrorKind::NotFound {
// Get current timestamp
let now = chrono::Local::now();
let timestamp = now.format("%Y-%m-%d %H:%M:%S").to_string();
// Create formatted JSON with proper indentation
let default_value = json!({
"outside": [
{
"name": "",
"context": "",
"url": "",
"percentage": null
}
],
"inside": [
{
"date": timestamp,
"context": ""
}
]
});
let default_json = serde_json::to_string_pretty(&default_value)
.unwrap_or_else(|_| String::from(r#"{"outside":[],"inside":[]}"#));
match fs::write(&fixed_path, &default_json) {
Ok(()) => {
self.json_input = default_json;
let path_changed = self.file_path.as_ref() != Some(&fixed_path);
self.file_path = Some(fixed_path.clone());
if path_changed {
self.file_path_changed = true;
}
self.set_status(&format!("Created new file: {}", final_path_display));
self.convert_json();
// Reset card selection and cursor position when creating a new file
if path_changed {
self.selected_entry_index = 0;
self.hscroll = 0;
self.content_cursor_line = 0;
self.content_cursor_col = 0;
self.scroll = 0;
}
// Reload explorer if open
if self.explorer_open {
self.load_explorer_entries();
}
}
Err(create_err) => {
self.set_status(&format!("Error creating '{}': {}", final_path_display, create_err));
}
}
} else {
self.set_status(&format!("Error loading '{}': {}", final_path_display, e));
}
}
}
}
pub fn save_file(&mut self) {
if let Some(ref path) = self.file_path {
match fs::write(path, &self.json_input) {
Ok(()) => {
self.is_modified = false;
self.last_save_time = Some(Instant::now());
self.set_status(&format!("Saved: {}", path.display()));
// Reload explorer if open (without resetting cursor position)
if self.explorer_open {
self.reload_explorer_entries();
}
}
Err(e) => {
self.set_status(&format!("Error saving: {}", e));
}
}
} else {
self.set_status("No filename. Use :w filename.json");
}
}
pub fn save_file_as(&mut self, filename: &str) {
let path = PathBuf::from(filename);
match fs::write(&path, &self.json_input) {
Ok(()) => {
let path_changed = self.file_path.as_ref() != Some(&path);
self.file_path = Some(path.clone());
self.is_modified = false;
self.last_save_time = Some(Instant::now());
if path_changed {
self.file_path_changed = true;
}
self.set_status(&format!("Saved: {}", path.display()));
// Reload explorer if open
if self.explorer_open {
self.load_explorer_entries();
}
}
Err(e) => {
self.set_status(&format!("Error saving: {}", e));
}
}
}
pub fn reload_file(&mut self) {
if let Some(path) = self.file_path.clone() {
match fs::read_to_string(&path) {
Ok(content) => {
self.json_input = content;
self.is_modified = false;
self.convert_json();
self.content_cursor_line = 0;
self.content_cursor_col = 0;
self.scroll = 0;
self.set_status(&format!("Reloaded: {}", path.display()));
}
Err(e) => {
self.set_status(&format!("Error reloading: {}", e));
}
}
} else {
self.set_status("No file to reload");
}
}
}