-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfiles.rs
More file actions
232 lines (198 loc) · 6.65 KB
/
files.rs
File metadata and controls
232 lines (198 loc) · 6.65 KB
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
//! Builders for file-related interactive selections.
use crate::interactive::state::{InteractiveAction, InteractiveItem, InteractiveState};
use std::path::{Path, PathBuf};
/// Build an interactive state showing current context files.
pub fn build_context_list(context_files: &[PathBuf]) -> InteractiveState {
let items: Vec<InteractiveItem> = context_files
.iter()
.enumerate()
.map(|(idx, path)| {
let label = path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("unknown")
.to_string();
let description = path
.parent()
.and_then(|p| p.to_str())
.map(|s| s.to_string());
let icon = get_file_icon(path);
let mut item = InteractiveItem::new(idx.to_string(), label)
.with_icon(icon)
.with_path(path.clone());
if let Some(desc) = description {
item = item.with_description(desc);
}
item
})
.collect();
if items.is_empty() {
let empty_item = InteractiveItem::new("empty", "No files in context")
.with_description("Use /add to add files")
.with_disabled(true);
InteractiveState::new(
"Context Files",
vec![empty_item],
InteractiveAction::Custom("context".into()),
)
} else {
InteractiveState::new(
format!("Context Files ({})", items.len()),
items,
InteractiveAction::Custom("context".into()),
)
}
}
/// Build an interactive state for removing context files (multi-select).
pub fn build_context_remove(context_files: &[PathBuf]) -> InteractiveState {
let items: Vec<InteractiveItem> = context_files
.iter()
.enumerate()
.map(|(idx, path)| {
let label = path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("unknown")
.to_string();
let description = path.to_str().map(|s| s.to_string());
let icon = get_file_icon(path);
let mut item = InteractiveItem::new(idx.to_string(), label)
.with_icon(icon)
.with_path(path.clone());
if let Some(desc) = description {
item = item.with_description(desc);
}
item
})
.collect();
if items.is_empty() {
let empty_item = InteractiveItem::new("empty", "No files to remove").with_disabled(true);
InteractiveState::new(
"Remove Files",
vec![empty_item],
InteractiveAction::RemoveContextFiles,
)
} else {
InteractiveState::new(
format!("Remove Files ({})", items.len()),
items,
InteractiveAction::RemoveContextFiles,
)
.with_multi_select()
}
}
/// Build a file browser for adding files.
pub fn build_file_browser(base_path: &Path) -> InteractiveState {
let mut items = Vec::new();
// Add parent directory option if not at root
if let Some(parent) = base_path.parent() {
items.push(
InteractiveItem::new("..", "..")
.with_icon('<')
.with_description("Parent directory")
.with_path(parent.to_path_buf()),
);
}
// Read directory contents
if let Ok(entries) = std::fs::read_dir(base_path) {
let mut dirs = Vec::new();
let mut files = Vec::new();
for entry in entries.flatten() {
let path = entry.path();
let name = entry.file_name().to_string_lossy().to_string();
// Skip hidden files
if name.starts_with('.') {
continue;
}
if path.is_dir() {
dirs.push((name, path));
} else {
files.push((name, path));
}
}
// Sort alphabetically
dirs.sort_by(|a, b| a.0.to_lowercase().cmp(&b.0.to_lowercase()));
files.sort_by(|a, b| a.0.to_lowercase().cmp(&b.0.to_lowercase()));
// Add directories first
for (name, path) in dirs {
items.push(
InteractiveItem::new(&name, format!("{}/", name))
.with_icon('/')
.with_path(path),
);
}
// Then files
for (name, path) in files {
let icon = get_file_icon(&path);
items.push(
InteractiveItem::new(&name, &name)
.with_icon(icon)
.with_path(path),
);
}
}
if items.is_empty() {
items.push(InteractiveItem::new("empty", "(empty directory)").with_disabled(true));
}
let title = format!("Browse: {}", base_path.display());
InteractiveState::new(
title,
items,
InteractiveAction::BrowseFiles {
base_path: base_path.to_path_buf(),
},
)
.with_search()
.with_multi_select()
.with_max_visible(15)
}
/// Get an icon for a file based on its extension.
fn get_file_icon(path: &Path) -> char {
let ext = path
.extension()
.and_then(|e| e.to_str())
.map(|e| e.to_lowercase());
match ext.as_deref() {
Some("rs") => 'R',
Some("py") => 'P',
Some("js" | "ts" | "jsx" | "tsx") => 'J',
Some("json" | "yaml" | "yml" | "toml") => 'C',
Some("md" | "txt" | "doc" | "docx") => 'T',
Some("png" | "jpg" | "jpeg" | "gif" | "svg" | "webp") => 'I',
Some("html" | "css" | "scss") => 'W',
Some("sh" | "bash" | "zsh" | "fish") => '$',
Some("sql") => 'D',
Some("xml") => 'X',
Some("go") => 'G',
Some("java" | "kt" | "kts") => 'J',
Some("c" | "cpp" | "h" | "hpp") => 'C',
Some("rb") => 'R',
Some("lock") => 'L',
_ => '-',
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_build_context_list_empty() {
let state = build_context_list(&[]);
assert_eq!(state.items.len(), 1);
assert!(state.items[0].disabled);
}
#[test]
fn test_build_context_list_with_files() {
let files = vec![
PathBuf::from("/tmp/test.rs"),
PathBuf::from("/tmp/other.py"),
];
let state = build_context_list(&files);
assert_eq!(state.items.len(), 2);
}
#[test]
fn test_get_file_icon() {
assert_eq!(get_file_icon(Path::new("test.rs")), 'R');
assert_eq!(get_file_icon(Path::new("test.py")), 'P');
assert_eq!(get_file_icon(Path::new("test.unknown")), '-');
}
}