-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
4636 lines (4194 loc) · 140 KB
/
Copy pathmain.rs
File metadata and controls
4636 lines (4194 loc) · 140 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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! # Regedited CLI
//!
//! Command-line interface for the fast plaintext parse-ment database.
//!
//! ## Commands
//!
//! ```bash
//! # Safetensors-style fast scan (header-only)
//! regedited scan myfile.md
//! regedited scan myfile.md --filter Config
//!
//! # Diff two files (metadata-only)
//! regedited diff base.md patched.md
//!
//! # Replace sections (safetensors-style patch)
//! regedited replace base.md patched.md --output result.md
//!
//! # Fast grep (ripgrep-style, memory-mapped)
//! regedited fgrep myfile.md "pattern"
//! regedited fgrep myfile.md "pattern" --section MySection
//! regedited fgrep-multi myfile.md pattern1 pattern2 pattern3
//!
//! # ZONE CONTENT MANIPULATION (Python-scriptable)
//! # Copy zone content from one section to another
//! regedited zone-copy myfile.md --from Alpha --from-zone 0 --to Beta --to-zone 1
//!
//! # Append content to a zone (from stdin or --text)
//! echo "new content" | regedited zone-append myfile.md MySection 0
//! regedited zone-append myfile.md MySection 0 --text "inline content"
//!
//! # Replace zone content (from stdin or --text)
//! cat new.md | regedited zone-replace myfile.md MySection 1
//!
//! # Extract raw zone content to stdout (for piping)
//! regedited zone-extract myfile.md MySection 1 > extracted.md
//!
//! # Zone info in machine-readable format (for Python scripts)
//! regedited zone-info myfile.md MySection 1
//!
//! # Show database table for a section
//! regedited db myfile.md MySection
//!
//! # Show the hex-word line for a section (`ascii` is the legacy alias)
//! regedited hexline myfile.md MySection
//!
//! # Extract a zone (grep by line range)
//! regedited grep myfile.md MySection 0
//!
//! # Copy a string to clipboard
//! regedited clip myfile.md MySection 2
//!
//! # Echo a string (safe for Windows CMD)
//! regedited echo myfile.md MySection 1
//!
//! # Convert line range to hex-words
//! regedited convert 50 80 --zone-type code
//!
//! # Update a numeric value
//! regedited set-num myfile.md MySection 0 42
//!
//! # Update a string
//! regedited set-str myfile.md MySection 0 "new value"
//!
//! # Update a hex-word line zone (with type)
//! regedited set-zone myfile.md MySection 0 10 100 --zone-type code
//!
//! # Show section content
//! regedited content myfile.md MySection
//!
//! # Create a new document
//! regedited new myfile.md "Document Title"
//!
//! # Add / remove sections
//! regedited add myfile.md NewSection
//! regedited rm myfile.md OldSection
//! ```
// SPDX-License-Identifier: AGPL-3.0
use clap::{error::ErrorKind, CommandFactory, Parser, Subcommand};
use owo_colors::OwoColorize;
use regedited::{
bool_ops::{bool_and, bool_nand, bool_or, bool_xor, count, if_contains},
echo::safe_echo,
encapsulate::{convert_mode, encapsulate, extract, EncapMode},
header::scan_content,
html_extract::{extract_attributes, format_as_set_vars, format_numbered},
store::{Store, StoreConfig},
};
use serde::{Deserialize, Serialize};
use std::{
ffi::OsString,
path::{Path, PathBuf},
};
#[derive(Parser)]
#[command(name = "regedited")]
#[command(about = "Fast plaintext parse-ment database")]
#[command(version = "0.2.0")]
struct Cli {
#[command(subcommand)]
command: Commands,
/// Verbose output
#[arg(long, global = true)]
verbose: bool,
/// Don't auto-save changes
#[arg(long, global = true)]
no_save: bool,
}
#[derive(Subcommand)]
enum Commands {
/// List all indexes in the document
List {
/// Path to the markdown file
file: PathBuf,
},
/// Show the database table for an index
Db {
/// Path to the markdown file
file: PathBuf,
/// Index reference: 64, i64, or index:64 (legacy name accepted)
#[arg(value_name = "INDEX")]
section: String,
},
/// Show the hex-word line for an index (`ascii` is the legacy command name)
#[command(aliases = ["ascii", "hex-word-line", "ranges"])]
Hexline {
/// Path to the markdown file
file: PathBuf,
/// Index reference
#[arg(value_name = "INDEX")]
section: String,
},
/// Scan all indexes
Scan {
/// Path to the markdown file
file: PathBuf,
/// Filter indexes by legacy key pattern
#[arg(short, long)]
filter: Option<String>,
/// Filter by database value index and range (e.g., "0:5:50")
#[arg(short, long)]
value: Option<String>,
},
/// Diff two Regedited files (metadata-only, like safetensors header diff)
Diff {
/// First file
file_a: PathBuf,
/// Second file
file_b: PathBuf,
},
/// Replace matching numeric indexes from source into target
Replace {
/// Target file (to be modified)
target: PathBuf,
/// Source file (donor indexes)
source: PathBuf,
/// Index references to replace (all matching if omitted)
#[arg(short, long = "indexes", visible_alias = "sections")]
sections: Option<Vec<String>>,
/// Output file (default: overwrite target)
#[arg(short, long)]
output: Option<PathBuf>,
},
/// Fast grep (ripgrep-style, memory-mapped)
Fgrep {
/// Path to the markdown file
file: PathBuf,
/// Search pattern
pattern: String,
/// Limit to an index reference
#[arg(short, long = "index", visible_alias = "section", value_name = "INDEX")]
section: Option<String>,
},
/// Multi-pattern grep (OR logic)
FgrepMulti {
/// Path to the markdown file
file: PathBuf,
/// Search patterns
patterns: Vec<String>,
},
/// Zone content copy: copy one zone's content to another zone
ZoneCopy {
/// Path to the markdown file
file: PathBuf,
/// Source index reference
#[arg(short = 'f', long, value_name = "INDEX")]
from: String,
/// Source zone index (0-2)
#[arg(short = 'm', long, default_value = "0")]
from_zone: usize,
/// Target index reference
#[arg(short = 't', long, value_name = "INDEX")]
to: String,
/// Target zone index (0-2)
#[arg(short = 'n', long, default_value = "0")]
to_zone: usize,
},
/// Zone content append: append content (from stdin or --text) to a zone
ZoneAppend {
/// Path to the markdown file
file: PathBuf,
/// Target index reference
#[arg(value_name = "INDEX")]
section: String,
/// Target zone index (0-2)
zone: usize,
/// Text to append (if not provided, reads from stdin)
#[arg(short, long)]
text: Option<String>,
},
/// Zone content replace: replace a zone's content (from stdin or --text)
ZoneReplace {
/// Path to the markdown file
file: PathBuf,
/// Target index reference
#[arg(value_name = "INDEX")]
section: String,
/// Target zone index (0-2)
zone: usize,
/// Replacement text (if not provided, reads from stdin)
#[arg(short, long)]
text: Option<String>,
},
/// Zone content extract: dump raw zone content to stdout (for piping)
ZoneExtract {
/// Path to the markdown file
file: PathBuf,
/// Index reference
#[arg(value_name = "INDEX")]
section: String,
/// Zone index (0-2)
zone: usize,
},
/// Zone info: machine-readable zone metadata (for Python scripts)
ZoneInfo {
/// Path to the markdown file
file: PathBuf,
/// Index reference
#[arg(value_name = "INDEX")]
section: String,
/// Zone index (0-2)
zone: usize,
},
/// Resolve a registry index number to its internal layout key
ResolveIndex {
/// Path to the markdown file
file: PathBuf,
/// Registry index value from the `index: N` line
registry_index: u64,
},
/// Extract a zone from a numeric registry index
IndexZoneExtract {
/// Path to the markdown file
file: PathBuf,
/// Registry index value from the `index: N` line
registry_index: u64,
/// Zone index (0-2)
zone: usize,
},
/// Replace a zone in a numeric registry index
IndexZoneReplace {
/// Path to the markdown file
file: PathBuf,
/// Registry index value from the `index: N` line
registry_index: u64,
/// Zone index (0-2)
zone: usize,
/// Replacement text (if not provided, reads from stdin)
#[arg(short, long)]
text: Option<String>,
},
/// Copy one zone to another within the same file, addressed by registry index
IndexZoneCopy {
/// Path to the markdown file
file: PathBuf,
/// Source registry index value
#[arg(long)]
from_index: u64,
/// Source zone index (0-2)
#[arg(long, default_value = "0")]
from_zone: usize,
/// Target registry index value
#[arg(long)]
to_index: u64,
/// Target zone index (0-2)
#[arg(long, default_value = "0")]
to_zone: usize,
},
/// Transfer a zone between two files, addressed by registry index
IndexZoneTransfer {
/// Source markdown file
#[arg(long)]
from_file: PathBuf,
/// Source registry index value
#[arg(long)]
from_index: u64,
/// Source zone index (0-2)
#[arg(long, default_value = "0")]
from_zone: usize,
/// Target markdown file
#[arg(long)]
to_file: PathBuf,
/// Target registry index value
#[arg(long)]
to_index: u64,
/// Target zone index (0-2)
#[arg(long, default_value = "0")]
to_zone: usize,
},
/// Extract an explicit hex-word line range
HexExtract {
/// Path to the markdown file
file: PathBuf,
/// Start hex-word, e.g. 1x0000032 or legacy 0x10000032
start: String,
/// End hex-word, e.g. 1x0000050 or legacy 0x10000050
end: String,
},
/// Replace an explicit hex-word line range and shift later hex-words
HexReplace {
/// Path to the markdown file
file: PathBuf,
/// Start hex-word, e.g. 1x0000032 or legacy 0x10000032
start: String,
/// End hex-word, e.g. 1x0000050 or legacy 0x10000050
end: String,
/// Replacement text (if not provided, reads from stdin)
#[arg(short, long)]
text: Option<String>,
},
/// Read any native ref spec: index string, DB value, DB line, defined zone, literal hex line/range
RefGet {
/// Path to the markdown file
file: PathBuf,
/// Ref spec, e.g. index:4:string:3, index:5:db:8, index:3:zone:2, hex:0x0000021..0x0000022
spec: String,
/// Copy the resolved value to the system clipboard
#[arg(long)]
clip: bool,
},
/// Write a literal or resolved ref value to any writable native ref spec
RefSet {
/// Path to the markdown file
file: PathBuf,
/// Target ref spec
target: String,
/// Source ref spec
#[arg(long)]
from: Option<String>,
/// Literal text source; stdin is used if neither --from nor --text is supplied
#[arg(short, long)]
text: Option<String>,
/// Append to the target instead of replacing it
#[arg(long)]
append: bool,
},
/// Copy or move a resolved ref into another writable ref
RefCopy {
/// Path to the markdown file
file: PathBuf,
/// Source ref spec
from: String,
/// Target ref spec
to: String,
/// Append to the target instead of replacing it
#[arg(long)]
append: bool,
/// Remove the source after writing the target
#[arg(long = "move")]
move_source: bool,
},
/// Diff any two native ref specs
RefDiff {
/// Path to the markdown file
file: PathBuf,
/// Left ref spec
left: String,
/// Right ref spec
right: String,
},
/// Boolean comparison over arbitrary ref specs and literals
RefBool {
/// Path to the markdown file
file: PathBuf,
/// Left ref spec or literal
left: String,
/// Operation: contains, eq, ne, gt, gte, lt, lte
op: String,
/// Right ref spec or literal
right: String,
/// Value printed when true
#[arg(long, default_value = "TRUE")]
then_val: String,
/// Value printed when false
#[arg(long, default_value = "FALSE")]
else_val: String,
},
/// List string 1, string 2, and string 3 for an index
IndexStrList {
/// Path to the markdown file
file: PathBuf,
/// Registry index value
registry_index: u64,
},
/// Set a defined index zone's stored hexword range without changing content
IndexZoneSetHex {
/// Path to the markdown file
file: PathBuf,
/// Registry index value
registry_index: u64,
/// Defined range slot, user-facing 1-3
zone: usize,
/// Start hex-word
start: String,
/// End hex-word
end: String,
},
/// Convert two line numbers and assign them to an index zone (zones are 1-3)
IndexZoneSetLines {
/// Path to the indexed document
file: PathBuf,
/// Registry index value
registry_index: u64,
/// Defined zone slot, user-facing 1-3
zone: usize,
/// Two line numbers plus an optional inline p/b/m/d type token and clip/c suffix
#[arg(value_name = "VALUE", num_args = 2..)]
values: Vec<String>,
/// Default zone type when no inline type token is supplied
#[arg(short = 't', long, default_value = "markdown")]
zone_type: String,
},
/// Show current native Regedited state as JSON
State {
/// Path to the markdown file
file: PathBuf,
},
/// Compare current native Regedited state with a prior state JSON
StateCompare {
/// Path to the markdown file
file: PathBuf,
/// State JSON path
state: PathBuf,
},
/// Check committed zone fingerprints and write a temporary relocation diff
Check {
/// Path to the indexed document
file: PathBuf,
},
/// Save one zone checkpoint, or check and optionally pull safe range relocations
Commit {
/// Path to the indexed document
file: PathBuf,
/// Pull safe relocations without an interactive prompt
#[arg(long)]
pull: bool,
},
/// Apply the latest guarded zone relocation diff
Pull {
/// Path to the indexed document
file: PathBuf,
},
/// Restore the last one-step undo copy
Undo {
/// Path to the markdown file
file: PathBuf,
},
/// Extract a zone by index (0-2)
Grep {
/// Path to the markdown file
file: PathBuf,
/// Index reference
#[arg(value_name = "INDEX")]
section: String,
/// Zone index (0-2)
#[arg(value_name = "ZONE")]
index: usize,
},
/// Copy a string to clipboard
Clip {
/// Path to the markdown file
file: PathBuf,
/// Index reference
#[arg(value_name = "INDEX")]
section: String,
/// String index (0-2)
#[arg(value_name = "STRING")]
index: usize,
},
/// Echo a string safely (handles Windows CMD special chars)
Echo {
/// Path to the markdown file
file: PathBuf,
/// Index reference
#[arg(value_name = "INDEX")]
section: String,
/// String index (0-2)
#[arg(value_name = "STRING")]
index: usize,
},
/// Echo any text safely (direct string mode)
EchoDirect {
/// Text to echo safely
text: String,
},
/// getutf — Convert a line number to UTF-16LE representation
Getutf {
/// Line number to encode
number: u32,
/// Decode mode (provide UTF-16LE hex to decode back)
#[arg(short, long)]
decode: Option<String>,
},
/// Update a numeric value
SetNum {
/// Path to the markdown file
file: PathBuf,
/// Index reference
#[arg(value_name = "INDEX")]
section: String,
/// Value index (0-8)
#[arg(value_name = "SLOT")]
index: usize,
/// New value
value: i64,
},
/// Update a string value
SetStr {
/// Path to the markdown file
file: PathBuf,
/// Index reference
#[arg(value_name = "INDEX")]
section: String,
/// String index (0-2)
#[arg(value_name = "STRING")]
index: usize,
/// New value
value: String,
},
/// Update Hex-word line zone (with type: markdown/code/media/database)
SetZone {
/// Path to the markdown file
file: PathBuf,
/// Index reference
#[arg(value_name = "INDEX")]
section: String,
/// Zone index (0-2)
#[arg(value_name = "ZONE")]
index: usize,
/// Start line
start: u32,
/// End line
end: u32,
/// Zone type: markdown (0), code (1), media (2), database (3)
#[arg(short, long, default_value = "markdown")]
zone_type: String,
},
/// Convert line numbers to hex-words or assign one pair to an index zone
Convert {
/// Line numbers plus optional inline p/b/m/d type tokens and clip/c suffix
#[arg(value_name = "VALUE", num_args = 1..)]
values: Vec<String>,
/// Default zone type: markdown, code, media, database (or 0-3)
#[arg(short = 't', long, default_value = "markdown")]
zone_type: String,
/// Legacy zone-converter marker (inline p/b/m/d tokens remain optional)
#[arg(short = 'z', long = "zone")]
zone: bool,
},
/// List all zone types and their hex nibble values
Types,
/// Show index content (between --- and the next index)
Content {
/// Path to the markdown file
file: PathBuf,
/// Index reference
#[arg(value_name = "INDEX")]
section: String,
},
/// Extract arbitrary line range (ad-hoc grep)
Lines {
/// Path to the markdown file
file: PathBuf,
/// Start line (0-indexed)
start: usize,
/// End line (inclusive, 0-indexed)
end: usize,
},
/// Create a new document
New {
/// Path for the new file
file: PathBuf,
/// Document title
title: String,
},
/// Add a new canonical index
Add {
/// Path to the markdown file
file: PathBuf,
/// New numeric registry index
registry_index: u64,
},
/// Remove an index
Rm {
/// Path to the markdown file
file: PathBuf,
/// Index reference
#[arg(value_name = "INDEX")]
section: String,
},
/// Show document summary
Summary {
/// Path to the markdown file
file: PathBuf,
},
/// Show full document info (all indexes with details)
Info {
/// Path to the markdown file
file: PathBuf,
},
// ==================== ENCAPSULATION (shel.sh/XML inspired) ====================
/// Encapsulate text in b/c/d modes — ["..."], ['...'], ["'...'"]
Encap {
/// Text to encapsulate (or extract/convert if --extract/--to provided)
text: String,
/// Mode: b/search (["..."]), c/delimit (['...']), d/store (["'...'"])
#[arg(short, long, default_value = "d")]
mode: String,
/// Extract inner text from an encapsulated string
#[arg(long)]
extract: bool,
/// Convert to a different mode (b/c/d)
#[arg(long)]
to: Option<String>,
/// Output as set variable (e.g., --set 0aaa)
#[arg(long)]
set: Option<String>,
},
// ==================== HTML EXTRACTION (GRAB B/C/D equivalent) ====================
/// Extract HTML attributes (GRAB B/C/D equivalent)
GrabHtml {
/// Path to HTML file
file: PathBuf,
/// Attribute name (HREF, SRC, etc.)
attr: String,
/// Encapsulation mode: b, c, or d
#[arg(short, long, default_value = "b")]
mode: String,
/// Filter by tag name (e.g., "a", "img")
#[arg(short, long)]
tag: Option<String>,
/// Output as set variables with base name (e.g., --set 0aaa)
#[arg(long)]
set: Option<String>,
/// Output with numbered indices (-0, -1, ...)
#[arg(long)]
numbered: bool,
},
// ==================== BOOLEAN OPERATIONS (if-then logic) ====================
/// Boolean AND: content must contain ALL patterns
BoolAnd {
/// Path to the markdown file
file: PathBuf,
/// Index reference (or "__all__" for the entire file)
#[arg(value_name = "INDEX")]
section: String,
/// Patterns to match (ALL must be found)
patterns: Vec<String>,
},
/// Boolean NAND: contains first pattern but NOT second
BoolNand {
/// Path to the markdown file
file: PathBuf,
/// Index reference (or "__all__" for the entire file)
#[arg(value_name = "INDEX")]
section: String,
/// Pattern that must be found
must_contain: String,
/// Pattern that must NOT be found
must_not: String,
},
/// Boolean OR: content contains ANY of the patterns
BoolOr {
/// Path to the markdown file
file: PathBuf,
/// Index reference (or "__all__" for the entire file)
#[arg(value_name = "INDEX")]
section: String,
/// Patterns to match (ANY must be found)
patterns: Vec<String>,
},
/// Boolean XOR: contains EXACTLY ONE of two patterns
BoolXor {
/// Path to the markdown file
file: PathBuf,
/// Index reference (or "__all__" for the entire file)
#[arg(value_name = "INDEX")]
section: String,
/// First pattern
pattern_a: String,
/// Second pattern
pattern_b: String,
},
/// Count occurrences of a pattern in content
Count {
/// Path to the markdown file
file: PathBuf,
/// Index reference (or "__all__" for the entire file)
#[arg(value_name = "INDEX")]
section: String,
/// Pattern to count
pattern: String,
},
/// If-contains-then: return value based on pattern presence
IfContains {
/// Path to the markdown file
file: PathBuf,
/// Index reference (or "__all__" for the entire file)
#[arg(value_name = "INDEX")]
section: String,
/// Pattern to check for
pattern: String,
/// Value to return if pattern is found
#[arg(long, default_value = "TRUE")]
then_val: String,
/// Value to return if pattern is NOT found
#[arg(long, default_value = "FALSE")]
else_val: String,
},
// ==================== WAL (Write-Ahead Log) ====================
/// Show WAL status for a document
Wal {
/// Path to the markdown file
file: PathBuf,
},
/// Replay WAL (crash recovery)
WalReplay {
/// Path to the markdown file
file: PathBuf,
/// Actually apply changes (without this, just shows what would be done)
#[arg(long)]
apply: bool,
},
// ==================== TRANSACTIONS ====================
/// Transaction: begin, commit, or rollback
Tx {
/// Transaction action: begin, commit, rollback, status
action: String,
/// Path to the markdown file
file: PathBuf,
},
// ==================== SCHEMA ====================
/// Show or validate schema for a document
Schema {
/// Path to the markdown file
file: PathBuf,
/// Validate document against schema (shows errors if any)
#[arg(long)]
validate: bool,
/// Create a starter schema from existing document
#[arg(long)]
init: bool,
},
// ==================== TYPED VALUES ====================
/// List all supported registry types
RegTypes,
/// Parse a value as a typed registry value
RegParse {
/// Value to parse
value: String,
/// Registry type: REG_SZ, REG_DWORD, REG_QWORD, REG_BINARY, REG_MULTI_SZ, REG_JSON, REG_BOOL, ...
#[arg(short, long, default_value = "REG_SZ")]
reg_type: String,
},
// ==================== SERVE (Registry Container) ====================
// ==================== ENHANCED CLIPBOARD ====================
/// Copy zone content (by index 0-2) to clipboard
ClipZone {
/// Path to the markdown file
file: PathBuf,
/// Index reference
#[arg(value_name = "INDEX")]
section: String,
/// Zone index (0, 1, or 2)
#[arg(value_name = "ZONE")]
zone: usize,
},
/// Copy database value (by index 0-8) to clipboard
ClipDb {
/// Path to the markdown file
file: PathBuf,
/// Index reference
#[arg(value_name = "INDEX")]
section: String,
/// Value index (0-8)
#[arg(value_name = "SLOT")]
index: usize,
},
/// Copy entire database line to clipboard
ClipDbline {
/// Path to the markdown file
file: PathBuf,
/// Index reference
#[arg(value_name = "INDEX")]
section: String,
},
/// Copy hex-word line to clipboard (`clip-ascii` is the legacy command name)
#[command(aliases = ["clip-ascii", "clip-hex-word-line", "clip-ranges"])]
ClipHexline {
/// Path to the markdown file
file: PathBuf,
/// Index reference
#[arg(value_name = "INDEX")]
section: String,
},
/// Copy manually-keyed hex-word range to clipboard
ClipHexword {
/// Start line
start: u32,
/// End line
end: u32,
/// Zone type: markdown, code, media, database
#[arg(short, long, default_value = "code")]
zone_type: String,
},
/// Start HTTP server (registry container mode)
Serve {
/// Path to the document file to serve
#[arg(short, long)]
file: PathBuf,
/// Port to listen on
#[arg(short, long, default_value = "5000")]
port: u16,
/// Read-only mode (disallow modifications)
#[arg(long, default_value = "true")]
read_only: bool,
},
}
const NO_LOADED_PATH: &str = "No path specified yet, specify path, else perform a load first, i.e. `rgd load ~/example/file/location.txt`";
fn main() {
let result = std::thread::Builder::new()
.name("regedited-cli".to_string())
.stack_size(8 * 1024 * 1024)
.spawn(run_main)
.unwrap_or_else(|error| {
eprintln!("Failed to start Regedited: {}", error);
std::process::exit(1);
})
.join();
if let Err(panic) = result {
std::panic::resume_unwind(panic);
}
}
fn run_main() {
let mut args: Vec<OsString> = std::env::args_os().collect();
let short_mode = args
.first()
.is_some_and(|argv0| regedited::qol::is_rgd_invocation(argv0));
regedited::qol::normalize_global_arguments(&mut args);
if short_mode {
if let Err(error) = regedited::qol::validate_aliases() {
clap::Error::raw(ErrorKind::InvalidValue, error).exit();
}
match handle_loaded_path_command(&args) {
Ok(true) => return,
Ok(false) => {}
Err(error) => clap::Error::raw(ErrorKind::InvalidValue, error.to_string()).exit(),
}
regedited::qol::normalize_short_command(&mut args);
regedited::qol::normalize_compact_refs(&mut args);
regedited::qol::normalize_short_clip_flag(&mut args);
if let Err(error) = regedited::qol::normalize_convert_destination(&mut args) {
clap::Error::raw(ErrorKind::InvalidValue, error).exit();
}
}
if handle_example_request(&args) {
return;
}
if handle_help_request(&args, short_mode) {
return;
}
let cli = parse_cli(args, short_mode);
if let Err(e) = run(cli) {
eprintln!("{} {}", "Error:".red().bold(), e);
std::process::exit(1);
}
}
fn handle_example_request(args: &[OsString]) -> bool {
let requested = args
.get(1)
.and_then(|value| value.to_str())
.is_some_and(|value| value == "-ex" || value == "--examples");
if !requested {
return false;
}
let values: Vec<&str> = args
.iter()
.skip(2)
.filter_map(|value| value.to_str())
.collect();
let (script, environment) = match values.as_slice() {
[environment] => (false, *environment),
["script", environment] => (true, *environment),
_ => clap::Error::raw(
ErrorKind::InvalidValue,
"Usage: regedited -ex [script] <powershell|repl|python|bash|bat>",
)
.exit(),
};
let content = match (script, environment.to_ascii_lowercase().as_str()) {
(false, "powershell") => include_str!("../docs/shell/POWERSHELL.txt"),
(false, "repl") => include_str!("../docs/shell/REPL.txt"),
(false, "python") => include_str!("../docs/shell/PYTHON.txt"),