Repository navigation
Expand file tree
/
Copy pathcli.vala
More file actions
572 lines (527 loc) · 25.8 KB
/
Copy pathcli.vala
File metadata and controls
572 lines (527 loc) · 25.8 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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
using GLib;
using Gee;
public class CliController : GLib.Object {
public File? work_dir { get; private set; }
public Gee.ArrayList<ItemData> items { get; private set; }
public bool use_absolute { get; private set; }
public bool show_header { get; private set; }
public Gee.HashSet<string> checked_paths { get; private set; }
public Gee.HashSet<string> checked_dirs { get; private set; }
public Gee.ArrayList<string> common_phrases { get; private set; }
public Gee.ArrayList<string> operation_messages { get; private set; }
private string? export_path = null;
private string? save_path = null;
private bool project_loaded = false;
public CliController () {
items = new Gee.ArrayList<ItemData> ();
checked_paths = new Gee.HashSet<string> ();
checked_dirs = new Gee.HashSet<string> ();
common_phrases = new Gee.ArrayList<string> ();
operation_messages = new Gee.ArrayList<string> ();
}
public void initialize_from_state (
File? wdir,
Gee.ArrayList<ItemData> existing_items,
Gee.HashSet<string> existing_checked_paths,
Gee.HashSet<string> existing_checked_dirs,
Gee.ArrayList<string> existing_common_phrases,
bool existing_use_absolute,
bool existing_show_header
) {
work_dir = wdir;
use_absolute = existing_use_absolute;
show_header = existing_show_header;
items.clear ();
for (int i = 0; i < existing_items.size; i++) {
var item = existing_items.get (i);
items.add (new ItemData (item.item_type, item.file_path, item.content, item.force_absolute, item.is_missing));
}
checked_paths.clear ();
foreach (var path in existing_checked_paths) {
checked_paths.add (path);
}
checked_dirs.clear ();
foreach (var path in existing_checked_dirs) {
checked_dirs.add (path);
}
common_phrases.clear ();
for (int i = 0; i < existing_common_phrases.size; i++) {
common_phrases.add (existing_common_phrases.get (i));
}
}
// 从 AppState 初始化 CLI 状态
public void initialize_from_app_state (AppState state) {
initialize_from_state (
state.work_dir,
state.items,
state.check_model.checked_files,
state.check_model.checked_dirs,
state.common_phrases,
state.use_absolute,
state.show_header
);
}
// 将 CLI 操作结果应用回 AppState,返回 work_dir 是否变更
public bool apply_to_state (AppState state) {
bool work_dir_changed = false;
if (work_dir != null) {
if (state.work_dir == null || work_dir.get_path () != state.work_dir.get_path ()) {
work_dir_changed = true;
}
}
state.items.clear ();
for (int i = 0; i < items.size; i++) state.items.add (items.get (i));
// 常用语是全局设置, 仅在 CLI 加载了项目文件时才覆盖
if (project_loaded) {
state.common_phrases.clear ();
for (int i = 0; i < common_phrases.size; i++) state.common_phrases.add (common_phrases.get (i));
}
state.check_model.replace_from (checked_paths, checked_dirs);
state.use_absolute = use_absolute;
state.show_header = show_header;
if (work_dir_changed) {
state.work_dir = work_dir;
}
state.items_changed ();
state.state_changed ();
return work_dir_changed;
}
public static bool is_cli_mode (string[] args) {
// args[0] 是程序名, 跳过; 仅检查实际参数
for (int i = 1; i < args.length; i++) {
var arg = args[i];
if (arg == "--work-dir" || arg == "--select-file" || arg == "--add-text" ||
arg == "--move" || arg == "--remove" || arg == "--clear" ||
arg == "--export" || arg == "--load" || arg == "--save" ||
arg == "--absolute" || arg == "--header" || arg == "--help" ||
arg == "-h" || arg == "--list-items") {
return true;
}
}
return false;
}
public bool parse_args (string[] args) {
// args[0] 通常是程序名, 跳过; 空数组或仅程序名时直接返回
if (args.length <= 1) return true;
int i = 1;
while (i < args.length) {
string arg = args[i];
if (arg == "--help" || arg == "-h") {
print_help ();
return true;
} else if (arg == "--work-dir") {
i++;
if (i >= args.length) { show_missing_arg (arg); return false; }
if (!apply_work_dir (args[i])) return false;
} else if (arg == "--select-file") {
i++;
if (i >= args.length) { show_missing_arg (arg); return false; }
if (!add_file (args[i])) return false;
} else if (arg == "--add-text") {
i++;
if (i >= args.length) { show_missing_arg (arg); return false; }
add_text (args[i]);
} else if (arg == "--move") {
i++;
if (i + 1 >= args.length) { show_missing_arg (arg); return false; }
// int.parse 对非法字符串静默返回 0, 会把"索引 0"误当作目标操作。
// 用 try_parse 显式校验, 避免用户输入 "abc" 时静默操作第一项。
int from;
int to;
if (!int.try_parse (args[i], out from)) {
stderr.printf (_("Error: Invalid index '%s' (not a number)\n"), args[i]);
return false;
}
if (!int.try_parse (args[i + 1], out to)) {
stderr.printf (_("Error: Invalid index '%s' (not a number)\n"), args[i + 1]);
return false;
}
if (!move_item (from, to)) return false;
i++;
} else if (arg == "--remove") {
i++;
if (i >= args.length) { show_missing_arg (arg); return false; }
int idx;
if (!int.try_parse (args[i], out idx)) {
stderr.printf (_("Error: Invalid index '%s' (not a number)\n"), args[i]);
return false;
}
if (!remove_item (idx)) return false;
} else if (arg == "--clear") {
clear_items ();
} else if (arg == "--absolute") {
use_absolute = true;
operation_messages.add (_("Absolute paths enabled"));
} else if (arg == "--header") {
show_header = true;
operation_messages.add (_("Header info enabled"));
} else if (arg == "--export") {
i++;
if (i >= args.length) { show_missing_arg (arg); return false; }
export_path = args[i];
} else if (arg == "--load") {
i++;
if (i >= args.length) { show_missing_arg (arg); return false; }
if (!load_project (args[i])) return false;
} else if (arg == "--save") {
i++;
if (i >= args.length) { show_missing_arg (arg); return false; }
save_path = args[i];
} else if (arg == "--list-items") {
list_items ();
} else {
stderr.printf (_("Error: Unknown argument: %s\n"), arg);
stderr.printf (_("Use --help to see usage information\n"));
return false;
}
i++;
}
return true;
}
public int run (string[] args) {
if (!parse_args (args)) return 1;
if (!execute_save_export ()) return 1;
return 0;
}
public bool execute_save_export () {
bool success = true;
if (save_path != null) {
try {
ProjectManager.write_project_file (
save_path, work_dir, use_absolute, show_header,
items, checked_paths, checked_dirs, common_phrases,
new Gee.ArrayList<WorkspaceSnapshot> ()
);
stdout.printf (_("Project saved to: %s\n"), save_path);
operation_messages.add (_("Project saved to: %s").printf (save_path));
} catch (Error e) {
stderr.printf (_("Failed to save project: %s\n"), e.message);
operation_messages.add (_("Failed to save project: %s").printf (e.message));
success = false;
}
}
if (export_path != null) {
if (items.size == 0) {
stderr.printf (_("Error: Queue is empty, cannot export\n"));
operation_messages.add (_("Export failed: Queue is empty"));
success = false;
} else {
try {
FileGenerator.generate_file (export_path, items, use_absolute, show_header, work_dir);
stdout.printf (_("Merged text exported to: %s\n"), export_path);
operation_messages.add (_("Merged text exported to: %s").printf (export_path));
} catch (Error e) {
stderr.printf (_("Export failed: %s\n"), e.message);
operation_messages.add (_("Export failed: %s").printf (e.message));
success = false;
}
}
}
return success;
}
private static void show_missing_arg (string arg) {
stderr.printf (_("Error: Argument '%s' requires a value\n"), arg);
}
private void print_help () {
stdout.printf (_("FileCollector %s — CLI Mode\n"), Config.VERSION);
stdout.printf ("\n");
stdout.printf (_("Usage: filecollector [options...] [--gui]"));
stdout.printf ("\n");
stdout.printf ("\n");
stdout.printf (_("Working Directory:")); stdout.printf ("\n");
stdout.printf (" --work-dir DIR "); stdout.printf (_("Set working directory")); stdout.printf ("\n");
stdout.printf ("\n");
stdout.printf (_("File Selection:")); stdout.printf ("\n");
stdout.printf (" --select-file PATH "); stdout.printf (_("Add file to queue (can be used multiple times)")); stdout.printf ("\n");
stdout.printf (" --add-text \"TEXT\" "); stdout.printf (_("Add custom text (can be used multiple times)")); stdout.printf ("\n");
stdout.printf ("\n");
stdout.printf (_("Queue Management:")); stdout.printf ("\n");
stdout.printf (" --move FROM TO "); stdout.printf (_("Move item at index FROM to index TO")); stdout.printf ("\n");
stdout.printf (" --remove INDEX "); stdout.printf (_("Remove item at INDEX")); stdout.printf ("\n");
stdout.printf (" --clear "); stdout.printf (_("Clear the queue")); stdout.printf ("\n");
stdout.printf (" --list-items "); stdout.printf (_("List current queue items")); stdout.printf ("\n");
stdout.printf ("\n");
stdout.printf (_("Export Settings:")); stdout.printf ("\n");
stdout.printf (" --export PATH "); stdout.printf (_("Export merged text to PATH")); stdout.printf ("\n");
stdout.printf (" --absolute "); stdout.printf (_("Use Absolute Paths")); stdout.printf ("\n");
stdout.printf (" --header "); stdout.printf (_("Add header (working directory path)")); stdout.printf ("\n");
stdout.printf ("\n");
stdout.printf (_("Project Files:")); stdout.printf ("\n");
stdout.printf (" --load FILE "); stdout.printf (_("Load state from project file")); stdout.printf ("\n");
stdout.printf (" --save FILE "); stdout.printf (_("Save current state to project file")); stdout.printf ("\n");
stdout.printf ("\n");
stdout.printf (_("GUI Mode:")); stdout.printf ("\n");
stdout.printf (" --gui "); stdout.printf (_("Initialize with CLI args then open the GUI")); stdout.printf ("\n");
stdout.printf ("\n");
stdout.printf (_("Other:")); stdout.printf ("\n");
stdout.printf (" --help, -h "); stdout.printf (_("Show this help message")); stdout.printf ("\n");
stdout.printf ("\n");
stdout.printf (_("Examples:"));
stdout.printf ("\n");
stdout.printf (" 1. "); stdout.printf (_("Build and export:")); stdout.printf ("\n");
stdout.printf (" filecollector --work-dir ./project \\\n");
stdout.printf (" --select-file src/main.vala \\\n");
stdout.printf (" --select-file src/utils/helper.vala \\\n");
stdout.printf (" --add-text \"=== "); stdout.printf (_("Configuration Files")); stdout.printf (" ===\" \\\n");
stdout.printf (" --select-file config.ini \\\n");
stdout.printf (" --move 3 2 \\\n");
stdout.printf (" --export output.txt\n");
stdout.printf ("\n");
stdout.printf (" 2. "); stdout.printf (_("Export from project file:")); stdout.printf ("\n");
stdout.printf (" filecollector --load my.project.fcol --export output.txt\n");
stdout.printf ("\n");
stdout.printf (" 3. "); stdout.printf (_("Build and save project:")); stdout.printf ("\n");
stdout.printf (" filecollector --work-dir ./project \\\n");
stdout.printf (" --select-file a.txt --select-file b.txt \\\n");
stdout.printf (" --save my.project.fcol\n");
stdout.printf ("\n");
stdout.printf (" 4. "); stdout.printf (_("View the queue:")); stdout.printf ("\n");
stdout.printf (" filecollector --load my.project.fcol --list-items\n");
stdout.printf ("\n");
stdout.printf (" 5. "); stdout.printf (_("Load project then open GUI for adjustments:")); stdout.printf ("\n");
stdout.printf (" filecollector --load my.project.fcol --gui\n");
stdout.printf ("\n");
stdout.printf (" 6. "); stdout.printf (_("Initialize state via CLI then open GUI:")); stdout.printf ("\n");
stdout.printf (" filecollector --work-dir ./project --select-file src/main.vala --gui\n");
stdout.printf ("\n");
}
private bool apply_work_dir (string path) {
var dir = File.new_for_path (path);
if (!dir.query_exists ()) {
stderr.printf (_("Error: Directory does not exist: %s\n"), path);
return false;
}
work_dir = dir;
stdout.printf (_("✓ Working directory set: %s\n"), path);
operation_messages.add (_("Working directory set: %s").printf (path));
return true;
}
// 输入路径解析 (--select-file / --load): 相对路径先按进程 cwd 查找
// (旧行为, 兼容既有脚本), 找不到且设置了 --work-dir 时再按 work_dir
// 解析 (README 完整工作流示例的约定)。输出路径 (--save / --export)
// 不在此列, 保持相对 cwd 不变。
private string resolve_input_path (string path) {
if (Path.is_absolute (path) || work_dir == null) return path;
if (FileUtils.test (path, FileTest.EXISTS)) return path;
try {
var resolved = work_dir.resolve_relative_path (path);
if (resolved.query_exists ()) return resolved.get_path ();
} catch (Error e) { }
return path;
}
private bool add_file (string path) {
var file = File.new_for_path (resolve_input_path (path));
if (!file.query_exists ()) {
stderr.printf (_("Error: File does not exist: %s\n"), path);
return false;
}
var abs_path = file.get_path ();
var item = new ItemData ("file", abs_path, null, false);
items.add (item);
checked_paths.add (abs_path);
stdout.printf (_("✓ File added [%d]: %s\n"), (int)items.size, abs_path);
operation_messages.add (_("File added: %s").printf (file.get_basename ()));
// 二进制文件: 同步触发预处理 (CLI 没有异步线程, 直接 block 调 VLM)
// 命中缓存则直接复用, miss 则调 VLM 写入缓存, 并把 Markdown 挂到 item 上,
// 后续 FileGenerator 导出时直接使用 preprocessed_content.
if (work_dir != null &&
item.is_allowed_binary_target (ConfigManager.get_allowed_binary_extensions ())) {
try {
bool from_cache;
string md = BinaryPreprocessor.preprocess_sync (
item, work_dir.get_path (), out from_cache
);
item.preprocessed_content = md;
item.preprocess_status = PreprocessStatus.COMPLETED;
item.from_cache = from_cache;
item.preprocess_error = null;
stdout.printf (_(" ↳ %s: %s\n"),
from_cache ? _("Reused from cache")
: (ConfigManager.get_binary_process_mode () == ConfigManager.BINARY_MODE_TEXT)
? _("Text extracted locally")
: _("VLM conversion called"),
file.get_basename ());
} catch (Error e) {
stderr.printf (_(" ⚠ Preprocessing %s failed: %s\n"),
file.get_basename (), e.message);
item.preprocess_status = PreprocessStatus.FAILED;
item.preprocess_error = e.message;
}
}
return true;
}
private void add_text (string text) {
items.add (new ItemData ("text", null, text, false));
stdout.printf (_("✓ Text added [%d]: %s\n"), (int)items.size, text);
var preview = text;
if (preview.length > 30) preview = preview.substring (0, 30) + "...";
operation_messages.add (_("Text added: %s").printf (preview));
}
private bool move_item (int from, int to) {
if (items.size == 0) {
stderr.printf (_("Error: Queue is empty\n"));
return false;
}
if (from < 0 || from >= items.size) {
stderr.printf (_("Error: Source index %d out of range (0-%d)\n"), from, items.size - 1);
return false;
}
if (to < 0 || to >= items.size) {
stderr.printf (_("Error: Target index %d out of range (0-%d)\n"), to, items.size - 1);
return false;
}
if (from == to) return true;
var tmp = items.remove_at (from);
items.insert (to, tmp);
stdout.printf (_("✓ Item moved from index %d to %d\n"), from, to);
operation_messages.add (_("Item moved %d → %d").printf (from, to));
return true;
}
private bool remove_item (int index) {
if (index < 0 || index >= items.size) {
stderr.printf (_("Error: Index %d out of range (0-%d)\n"), index, items.size - 1);
return false;
}
var data = items.get (index);
if (data.item_type == "file" && !data.force_absolute) {
checked_paths.remove (data.file_path);
}
items.remove_at (index);
stdout.printf (_("✓ Item at index %d deleted\n"), index);
operation_messages.add (_("Item %d deleted").printf (index));
return true;
}
private void clear_items () {
items.clear ();
checked_paths.clear ();
checked_dirs.clear ();
stdout.printf (_("✓ Queue cleared\n"));
operation_messages.add (_("Queue cleared"));
}
private void list_items () {
if (items.size == 0) {
stdout.printf (_("Queue is empty\n"));
return;
}
var dashes = new StringBuilder ();
for (int j = 0; j < 60; j++) dashes.append_c ('-');
var dash_str = dashes.str;
stdout.printf (_("Current queue (%d items):\n"), items.size);
stdout.printf ("%s\n", dash_str);
for (int i = 0; i < items.size; i++) {
var data = items.get (i);
if (data.item_type == "file") {
stdout.printf (" %d. [%s] %s\n", i, _("File"), data.file_path);
} else {
var preview = data.content;
if (preview.length > 60) preview = preview.substring (0, 60) + "...";
stdout.printf (" %d. [%s] %s\n", i, _("Text"), preview);
}
}
stdout.printf ("%s\n", dash_str);
stdout.printf (_("Settings: Absolute paths=%s Header=%s\n"),
use_absolute.to_string (), show_header.to_string ());
if (work_dir != null) {
stdout.printf (_("Working directory: %s\n"), work_dir.get_path ());
}
}
private bool load_project (string path) {
string resolved = resolve_input_path (path);
var file = File.new_for_path (resolved);
if (!file.query_exists ()) {
stderr.printf (_("Error: Project file does not exist: %s\n"), path);
return false;
}
try {
string content;
size_t len;
FileUtils.get_contents (resolved, out content, out len);
var parser = new Json.Parser ();
parser.load_from_data (content);
var root_node = parser.get_root ();
if (root_node == null || root_node.get_node_type () != Json.NodeType.OBJECT) {
printerr ("Invalid project file: root is not a JSON object\n");
return false;
}
var root = root_node.get_object ();
if (root == null) {
printerr ("Invalid project file: root object is null\n");
return false;
}
project_loaded = true;
var wd_str = root.get_string_member_with_default ("work_dir", "");
if (wd_str != "") {
var wd = File.new_for_path (wd_str);
if (wd.query_exists ()) {
work_dir = wd;
}
}
use_absolute = root.get_boolean_member_with_default ("use_absolute", false);
show_header = root.get_boolean_member_with_default ("show_header", false);
items.clear ();
checked_paths.clear ();
checked_dirs.clear ();
// 与 ProjectManager.load_project_file 共用字段兼容 / null 防御 / 路径
// 归一化规则, 并同样按存在性过滤, 保证 GUI 与 CLI 加载结果一致,
// 也不会把不存在的路径经 --save 写回项目文件。
foreach (var p in ProjectManager.read_string_array (root, "checked_files", "checked_entries")) {
var canonical = ProjectManager.canonicalize_checked_path (p, work_dir);
if (canonical != null && File.new_for_path (canonical).query_exists ()) {
checked_paths.add (canonical);
}
}
foreach (var d in ProjectManager.read_string_array (root, "checked_dirs", "")) {
checked_dirs.add (d);
}
var items_arr = root.has_member ("items") ? root.get_array_member ("items") : null;
if (items_arr != null) {
for (int i = 0; i < items_arr.get_length (); i++) {
var obj = items_arr.get_object_element (i);
if (obj == null) continue;
var type = obj.get_string_member_with_default ("type", "file");
if (type == "file") {
var p = obj.get_string_member_with_default ("path", "");
if (p == "") continue;
var fa = obj.get_boolean_member_with_default ("force_absolute", false);
// is_missing 由当前磁盘状态决定, 与 project_manager 加载逻辑保持一致.
// JSON 中的 missing 字段仅作为保存时的记录, 加载时不直接使用.
var miss = !File.new_for_path (p).query_exists ();
var item = new ItemData ("file", p, null, fa, miss);
items.add (item);
// 二进制文件: 尝试从 .filecollector_cache 复用已转换的 Markdown
// (不主动调 VLM, 避免 load 时产生 API 费用/等待)
if (work_dir != null &&
!miss &&
item.is_allowed_binary_target (ConfigManager.get_allowed_binary_extensions ())) {
try {
string? md = BinaryPreprocessor.try_cache_only (
item, work_dir.get_path ()
);
if (md != null) {
item.preprocessed_content = md;
item.preprocess_status = PreprocessStatus.COMPLETED;
item.from_cache = true;
}
} catch (Error e) {
// 缓存读取失败, 保持 NONE, 导出时按二进制处理
}
}
} else {
var c = obj.get_string_member_with_default ("content", "");
items.add (new ItemData ("text", null, c, false));
}
}
}
common_phrases.clear ();
foreach (var phrase in ProjectManager.read_string_array (root, "common_phrases", "")) {
common_phrases.add (phrase);
}
stdout.printf (_("✓ Loaded from project file: %s (%d items)\n"), path, items.size);
operation_messages.add (_("Project loaded: %s (%d items)").printf (File.new_for_path (path).get_basename (), items.size));
return true;
} catch (Error e) {
stderr.printf (_("Failed to load project: %s\n"), e.message);
return false;
}
}
}