-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompletion.go
More file actions
1032 lines (892 loc) · 32.8 KB
/
completion.go
File metadata and controls
1032 lines (892 loc) · 32.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
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
/*
Copyright (C) 2025 Note CLI Contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// SetupCompletion handles the interactive completion setup prompt
func SetupCompletion(reader *bufio.Reader) {
// Check if completion is already set up
if IsCompletionAlreadySetup() {
return
}
fmt.Println()
fmt.Print("Would you like to set up command line completion for note? (y/N): ")
response, _ := reader.ReadString('\n')
response = strings.ToLower(strings.TrimSpace(response))
if response != "y" && response != "yes" {
fmt.Println("Skipping completion setup. You can run 'note --config' later to set it up.")
return
}
shell := detectShell()
if shell == "" {
fmt.Println("Could not detect shell type. Skipping completion setup.")
return
}
switch shell {
case "bash":
SetupBashCompletion()
case "zsh":
SetupZshCompletion()
case "fish":
SetupFishCompletion()
default:
fmt.Printf("Shell '%s' not supported for completion. Supported shells: bash, zsh, fish\n", shell)
}
}
// IsCompletionAlreadySetup checks if command line completion is already configured
func IsCompletionAlreadySetup() bool {
shell := detectShell()
if shell == "" {
return false
}
// First check centralized config
_, hasCompletion := GetCentralizedConfigStatus(shell)
if hasCompletion {
return true
}
// Fall back to checking legacy locations for backward compatibility
homeDir, err := os.UserHomeDir()
if err != nil {
return false
}
switch shell {
case "bash":
// Check if ~/.note.bash exists and is sourced in shell config
bashCompletionFile := filepath.Join(homeDir, ".note.bash")
if _, err := os.Stat(bashCompletionFile); err == nil {
// Check .bashrc or .bash_profile for note completion
bashFiles := []string{".bashrc", ".bash_profile", ".profile"}
for _, file := range bashFiles {
if CheckFileForCompletionSource(filepath.Join(homeDir, file)) {
return true
}
}
}
case "zsh":
zshCompletionFile := filepath.Join(homeDir, ".note.zsh")
if _, err := os.Stat(zshCompletionFile); err == nil {
if CheckFileForCompletionSource(filepath.Join(homeDir, ".zshrc")) {
return true
}
}
case "fish":
// Check fish completion directory
fishCompletionDir := filepath.Join(homeDir, ".config", "fish", "completions")
fishCompletionFile := filepath.Join(fishCompletionDir, "note.fish")
_, err := os.Stat(fishCompletionFile)
return err == nil
}
return false
}
// CheckFileForCompletionSource checks if a file sources note completion
func CheckFileForCompletionSource(filePath string) bool {
file, err := os.Open(filePath)
if err != nil {
return false
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// Check for centralized config
if strings.Contains(line, BashCentralizedConfig) || strings.Contains(line, ZshCentralizedConfig) ||
strings.Contains(line, FishCentralizedConfig) {
return true
}
// Check for legacy config
if (strings.Contains(line, "~/.note.bash") || strings.Contains(line, "~/.note.zsh")) &&
(strings.Contains(line, "source") || strings.Contains(line, ".")) ||
(strings.Contains(line, "note") && (strings.Contains(line, "complete") || strings.Contains(line, "completion"))) {
return true
}
}
return false
}
// SetupBashCompletion sets up bash command completion
func SetupBashCompletion() {
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting home directory: %v\n", err)
return
}
// Get current alias status to preserve it
hasAliases, _ := GetCentralizedConfigStatus("bash")
// Write centralized config with completion enabled
if err := WriteCentralizedConfig("bash", hasAliases, true); err != nil {
fmt.Fprintf(os.Stderr, "Error writing centralized config: %v\n", err)
return
}
// Ensure source line exists in .bashrc
if err := EnsureSourceLine("bash"); err != nil {
fmt.Fprintf(os.Stderr, "Error adding source line: %v\n", err)
return
}
// Clean up legacy config
CleanupLegacyConfig("bash")
configPath := filepath.Join(homeDir, BashCentralizedConfig)
fmt.Printf("✓ Bash completion setup complete!\n")
fmt.Printf(" Created centralized config at %s\n", configPath)
fmt.Printf(" Run 'source ~/.bashrc' or restart your shell to activate completions\n")
}
// SetupZshCompletion sets up zsh command completion
func SetupZshCompletion() {
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting home directory: %v\n", err)
return
}
// Get current alias status to preserve it
hasAliases, _ := GetCentralizedConfigStatus("zsh")
// Write centralized config with completion enabled
if err := WriteCentralizedConfig("zsh", hasAliases, true); err != nil {
fmt.Fprintf(os.Stderr, "Error writing centralized config: %v\n", err)
return
}
// Ensure source line exists in .zshrc
if err := EnsureSourceLine("zsh"); err != nil {
fmt.Fprintf(os.Stderr, "Error adding source line: %v\n", err)
return
}
// Clean up legacy config
CleanupLegacyConfig("zsh")
configPath := filepath.Join(homeDir, ZshCentralizedConfig)
fmt.Printf("✓ Zsh completion setup complete!\n")
fmt.Printf(" Created centralized config at %s\n", configPath)
fmt.Printf(" Restart your shell or run: source ~/.zshrc\n")
}
// SetupFishCompletion sets up fish command completion
func SetupFishCompletion() {
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting home directory: %v\n", err)
return
}
// Create fish completion directory if it doesn't exist
fishCompletionDir := filepath.Join(homeDir, ".config", "fish", "completions")
if err := os.MkdirAll(fishCompletionDir, 0755); err != nil {
fmt.Fprintf(os.Stderr, "Error creating fish completion directory: %v\n", err)
return
}
// Create a simple fish completion script
fishCompletionScript := `# note command completion for fish
# Helper function to get notes (includes archived if -a flag is present)
function __note_get_notes
if test -f ~/.note
set notesdir (grep "^notesdir=" ~/.note | cut -d= -f2 | sed "s|~|$HOME|")
if test -d "$notesdir"
# Get main notes
find "$notesdir" -maxdepth 1 -name "*.md" -type f -exec basename {} .md \; 2>/dev/null
# Check if -a flag is in the command line
if contains -- -a (commandline -opc); or contains -- -al (commandline -opc); or contains -- -la (commandline -opc)
# Include archived notes
if test -d "$notesdir/Archive"
for f in (find "$notesdir/Archive" -maxdepth 1 -name "*.md" -type f -exec basename {} .md \; 2>/dev/null)
echo "Archive/$f"
end
end
if test -d "$notesdir/archive"
for f in (find "$notesdir/archive" -maxdepth 1 -name "*.md" -type f -exec basename {} .md \; 2>/dev/null)
echo "archive/$f"
end
end
end
end
end | sort
end
# Main command
complete -c note -f
complete -c note -s l -d "List notes"
complete -c note -s s -d "Search notes" -r
complete -c note -s a -d "Include archived notes"
complete -c note -s d -d "Archive notes" -r
complete -c note -l config -d "Run setup/reconfigure"
complete -c note -l configure -d "Run setup/reconfigure"
complete -c note -l autocomplete -d "Setup/update command line autocompletion"
complete -c note -l alias -d "Setup shell aliases"
complete -c note -s v -l version -d "Show version"
complete -c note -s h -l help -d "Show help"
# Complete with existing note names for main argument
complete -c note -n '__fish_is_first_token' -a '(__note_get_notes)'
# Complete note names after flags that take note arguments
complete -c note -n '__fish_seen_argument -s l -s a -s d' -a '(__note_get_notes)'
# Alias: n (same as note)
complete -c n -f
complete -c n -s l -d "List notes"
complete -c n -s s -d "Search notes" -r
complete -c n -s a -d "Include archived notes"
complete -c n -s d -d "Archive notes" -r
complete -c n -l config -d "Run setup/reconfigure"
complete -c n -l configure -d "Run setup/reconfigure"
complete -c n -l autocomplete -d "Setup/update command line autocompletion"
complete -c n -l alias -d "Setup shell aliases"
complete -c n -s v -l version -d "Show version"
complete -c n -s h -l help -d "Show help"
complete -c n -n '__fish_is_first_token' -a '(__note_get_notes)'
complete -c n -n '__fish_seen_argument -s l -s a -s d' -a '(__note_get_notes)'
# Alias: nls (note -l)
complete -c nls -f
complete -c nls -a '(__note_get_notes)'
# Alias: nrm (note -d)
complete -c nrm -f
complete -c nrm -a '(__note_get_notes)'
`
noteCompletionFile := filepath.Join(fishCompletionDir, "note.fish")
if err := os.WriteFile(noteCompletionFile, []byte(fishCompletionScript), 0644); err != nil {
fmt.Fprintf(os.Stderr, "Error writing fish completion script: %v\n", err)
return
}
fmt.Printf("✓ Fish completion setup complete!\n")
fmt.Printf(" Created completion file at %s\n", noteCompletionFile)
fmt.Printf(" Restart your shell to activate completions\n")
}
// RunAutocompleteSetup handles the main autocomplete setup flow
func RunAutocompleteSetup() {
reader := bufio.NewReader(os.Stdin)
fmt.Println("note - Command Line Autocompletion Setup")
fmt.Println()
fmt.Println("This will set up tab completion for the note command, allowing you to:")
fmt.Println("• Tab-complete note names")
fmt.Println("• Tab-complete command flags")
fmt.Println("• Get context-aware completions")
fmt.Println()
fmt.Print("Would you like to set up autocompletion? (y/N): ")
response, _ := reader.ReadString('\n')
response = strings.ToLower(strings.TrimSpace(response))
if response != "y" && response != "yes" {
fmt.Println("Autocompletion setup cancelled.")
return
}
shell := detectShell()
if shell == "" {
fmt.Println("Could not detect shell type. Skipping completion setup.")
fmt.Println("Supported shells: bash, zsh, fish")
return
}
fmt.Printf("Detected shell: %s\n", shell)
fmt.Println()
// Set up completion for the detected shell
fmt.Printf("Setting up %s completion...\n", shell)
switch shell {
case "bash":
SetupBashCompletion()
case "zsh":
SetupZshCompletion()
case "fish":
SetupFishCompletion()
default:
fmt.Printf("Shell '%s' not supported for completion. Supported shells: bash, zsh, fish\n", shell)
return
}
fmt.Println()
fmt.Println("✓ Autocompletion setup complete!")
fmt.Println(" To activate, run one of:")
homeDir, _ := os.UserHomeDir()
switch shell {
case "bash":
fmt.Printf(" source ~/.bashrc\n")
fmt.Printf(" source ~/%s\n", BashCentralizedConfig)
case "zsh":
fmt.Printf(" source ~/.zshrc\n")
fmt.Printf(" source ~/%s\n", ZshCentralizedConfig)
case "fish":
fmt.Println(" (restart your shell)")
fmt.Printf(" source %s\n", filepath.Join(homeDir, ".config", "fish", "completions", "note.fish"))
}
fmt.Println(" Or simply restart your shell")
}
// CleanupExistingCompletion removes existing completion setup for the specified shell
func CleanupExistingCompletion(shell string) {
homeDir, err := os.UserHomeDir()
if err != nil {
return
}
switch shell {
case "bash":
// Remove centralized config file
centralizedFile := filepath.Join(homeDir, BashCentralizedConfig)
os.Remove(centralizedFile)
// Remove legacy .note.bash file
legacyFile := filepath.Join(homeDir, ".note.bash")
os.Remove(legacyFile)
// Clean up shell config files
cleanupShellConfig(filepath.Join(homeDir, ".bashrc"))
cleanupShellConfig(filepath.Join(homeDir, ".bash_profile"))
cleanupShellConfig(filepath.Join(homeDir, ".profile"))
case "zsh":
// Remove centralized config file
centralizedFile := filepath.Join(homeDir, ZshCentralizedConfig)
os.Remove(centralizedFile)
// Remove legacy .note.zsh file
legacyFile := filepath.Join(homeDir, ".note.zsh")
os.Remove(legacyFile)
// Clean up .zshrc
cleanupShellConfig(filepath.Join(homeDir, ".zshrc"))
case "fish":
// Remove centralized config file
centralizedFile := filepath.Join(homeDir, FishCentralizedConfig)
os.Remove(centralizedFile)
// Remove existing fish completion file
fishCompletionDir := filepath.Join(homeDir, ".config", "fish", "completions")
noteCompletionFile := filepath.Join(fishCompletionDir, "note.fish")
os.Remove(noteCompletionFile)
// Clean up fish config
fishConfig := filepath.Join(homeDir, ".config", "fish", "config.fish")
cleanupShellConfig(fishConfig)
}
}
// cleanupShellConfig removes note completion lines from shell config files
func cleanupShellConfig(configFile string) {
// Read the file
file, err := os.Open(configFile)
if err != nil {
return
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
skipNext := false
for scanner.Scan() {
line := scanner.Text()
// Skip lines that contain note completion references
if strings.Contains(line, "# note command completion") {
skipNext = true
continue
}
// Skip Note CLI integration header and following source line
if strings.Contains(line, "# Note CLI integration") {
skipNext = true
continue
}
// Skip centralized config source lines
if strings.Contains(line, BashCentralizedConfig) ||
strings.Contains(line, ZshCentralizedConfig) ||
strings.Contains(line, FishCentralizedConfig) {
continue
}
if skipNext && (strings.Contains(line, ".note.bash") ||
strings.Contains(line, ".note.zsh") ||
strings.Contains(line, "completions/bash/note") ||
(strings.Contains(line, "note") && strings.Contains(line, "source"))) {
skipNext = false
continue
}
if skipNext && strings.TrimSpace(line) == "" {
continue
}
skipNext = false
lines = append(lines, line)
}
// Write the cleaned file back
outFile, err := os.Create(configFile)
if err != nil {
return
}
defer outFile.Close()
for _, line := range lines {
fmt.Fprintln(outFile, line)
}
}
// detectShell detects the current shell from environment variables
func detectShell() string {
shell := os.Getenv("SHELL")
if shell == "" {
return ""
}
// Extract shell name from path
shellName := filepath.Base(shell)
// Map common shell variants
switch shellName {
case "bash":
return "bash"
case "zsh":
return "zsh"
case "fish":
return "fish"
default:
return shellName
}
}
// Centralized config file paths
const (
BashCentralizedConfig = ".note_bash_rc"
ZshCentralizedConfig = ".note_zsh_rc"
FishCentralizedConfig = ".note_fish_rc"
)
// generateBashConfig generates the complete bash config content
func generateBashConfig(aliasesEnabled, completionEnabled bool, notePath string) string {
var content strings.Builder
content.WriteString("# Note CLI Shell Integration\n")
content.WriteString("# Generated by note CLI - Do not edit manually\n")
content.WriteString("# Regenerate with: note --configure\n\n")
if aliasesEnabled {
content.WriteString("# ============= ALIASES =============\n")
content.WriteString(fmt.Sprintf("alias n='%s'\n", notePath))
content.WriteString(fmt.Sprintf("alias nls='%s -l'\n", notePath))
content.WriteString(fmt.Sprintf("alias nrm='%s -d'\n", notePath))
content.WriteString("\n")
}
if completionEnabled {
content.WriteString("# ============= COMPLETION =============\n")
content.WriteString(`_note_complete() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
# Check if -a flag is present in the command line
local include_archive=false
for word in "${COMP_WORDS[@]}"; do
if [[ "$word" == "-a" || "$word" == "-al" || "$word" == "-la" || "$word" == "-as" || "$word" == "-sa" ]]; then
include_archive=true
break
fi
done
# Helper function to get notes
_get_notes() {
if [[ -f ~/.note ]]; then
local notesdir=$(grep "^notesdir=" ~/.note | cut -d= -f2 | sed "s|~|$HOME|")
if [[ -d "$notesdir" ]]; then
# Get notes from main directory
local notes=$(find "$notesdir" -maxdepth 1 -name "*.md" -type f -exec basename {} .md \; 2>/dev/null)
# If -a flag is present, also include archived notes
if [[ "$include_archive" == true ]]; then
local archivedir="$notesdir/Archive"
if [[ -d "$archivedir" ]]; then
local archived=$(find "$archivedir" -maxdepth 1 -name "*.md" -type f -exec basename {} .md \; 2>/dev/null | sed 's/^/Archive\//')
notes="$notes"$'\n'"$archived"
fi
# Also check lowercase archive
archivedir="$notesdir/archive"
if [[ -d "$archivedir" ]]; then
local archived=$(find "$archivedir" -maxdepth 1 -name "*.md" -type f -exec basename {} .md \; 2>/dev/null | sed 's/^/archive\//')
notes="$notes"$'\n'"$archived"
fi
fi
echo "$notes" | sort | tr '\n' ' '
fi
fi
}
# If we're on the first argument
if [[ ${COMP_CWORD} -eq 1 ]]; then
# If user starts typing a dash, offer flags
if [[ "$cur" == -* ]]; then
local flags="-l -s -a -d -v --config --configure --autocomplete --alias --help --version -h"
COMPREPLY=($(compgen -W "$flags" -- "${cur}"))
else
# Otherwise, prioritize note names
local notes=$(_get_notes)
# Use case-insensitive matching by converting both to lowercase
local cur_lower=$(echo "$cur" | tr '[:upper:]' '[:lower:]')
COMPREPLY=()
for note in $notes; do
local note_lower=$(echo "$note" | tr '[:upper:]' '[:lower:]')
if [[ "$note_lower" == "$cur_lower"* ]]; then
COMPREPLY+=("$note")
fi
done
fi
# If previous was -l, -a, or -d, offer note names
elif [[ "$prev" == "-l" || "$prev" == "-a" || "$prev" == "-d" || "$prev" == "-al" || "$prev" == "-la" ]]; then
local notes=$(_get_notes)
# Use case-insensitive matching by converting both to lowercase
local cur_lower=$(echo "$cur" | tr '[:upper:]' '[:lower:]')
COMPREPLY=()
for note in $notes; do
local note_lower=$(echo "$note" | tr '[:upper:]' '[:lower:]')
if [[ "$note_lower" == "$cur_lower"* ]]; then
COMPREPLY+=("$note")
fi
done
fi
}
# Register completion for note and its aliases
complete -F _note_complete note
complete -F _note_complete n
complete -F _note_complete nls
complete -F _note_complete nrm
`)
}
return content.String()
}
// generateZshConfig generates the complete zsh config content
func generateZshConfig(aliasesEnabled, completionEnabled bool, notePath string) string {
var content strings.Builder
content.WriteString("# Note CLI Shell Integration\n")
content.WriteString("# Generated by note CLI - Do not edit manually\n")
content.WriteString("# Regenerate with: note --configure\n\n")
if aliasesEnabled {
content.WriteString("# ============= ALIASES =============\n")
content.WriteString(fmt.Sprintf("alias n='%s'\n", notePath))
content.WriteString(fmt.Sprintf("alias nls='%s -l'\n", notePath))
content.WriteString(fmt.Sprintf("alias nrm='%s -d'\n", notePath))
content.WriteString("\n")
}
if completionEnabled {
content.WriteString("# ============= COMPLETION =============\n")
content.WriteString("autoload -U +X compinit && compinit\n\n")
content.WriteString(`_note_complete() {
local cur="${words[CURRENT]}"
local prev="${words[CURRENT-1]}"
# Check if -a flag is present in the command line
local include_archive=false
for word in "${words[@]}"; do
if [[ "$word" == "-a" || "$word" == "-al" || "$word" == "-la" || "$word" == "-as" || "$word" == "-sa" ]]; then
include_archive=true
break
fi
done
# Helper function to get notes
_get_notes() {
local notes=()
if [[ -f ~/.note ]]; then
local notesdir=$(grep "^notesdir=" ~/.note | cut -d= -f2 | sed "s|~|$HOME|")
if [[ -d "$notesdir" ]]; then
# Get notes from main directory
notes+=(${(f)"$(find "$notesdir" -maxdepth 1 -name "*.md" -type f -exec basename {} .md \; 2>/dev/null)"})
# If -a flag is present, also include archived notes
if [[ "$include_archive" == true ]]; then
local archivedir="$notesdir/Archive"
if [[ -d "$archivedir" ]]; then
local archived=(${(f)"$(find "$archivedir" -maxdepth 1 -name "*.md" -type f -exec basename {} .md \; 2>/dev/null)"})
for a in $archived; do
notes+=("Archive/$a")
done
fi
# Also check lowercase archive
archivedir="$notesdir/archive"
if [[ -d "$archivedir" ]]; then
local archived=(${(f)"$(find "$archivedir" -maxdepth 1 -name "*.md" -type f -exec basename {} .md \; 2>/dev/null)"})
for a in $archived; do
notes+=("archive/$a")
done
fi
fi
fi
fi
echo "${(F)notes}" | sort
}
# If we're on the first argument
if [[ $CURRENT -eq 2 ]]; then
# If user starts typing a dash, offer flags
if [[ "$cur" == -* ]]; then
local flags=("-l" "-s" "-a" "-d" "-v" "--config" "--configure" "--autocomplete" "--alias" "--help" "--version" "-h")
compadd -a flags
else
# Otherwise, prioritize note names
local all_notes=(${(f)"$(_get_notes)"})
local notes=()
# Filter case-insensitively
local cur_lower="${cur:l}"
for note in $all_notes; do
if [[ "${note:l}" == ${cur_lower}* ]]; then
notes+=("$note")
fi
done
compadd -a notes
fi
# If previous was -l, -a, -d, or combined flags, offer note names
elif [[ "$prev" == "-l" || "$prev" == "-a" || "$prev" == "-d" || "$prev" == "-al" || "$prev" == "-la" ]]; then
local all_notes=(${(f)"$(_get_notes)"})
# Filter case-insensitively
local notes=()
local cur_lower="${cur:l}"
for note in $all_notes; do
if [[ "${note:l}" == ${cur_lower}* ]]; then
notes+=("$note")
fi
done
compadd -a notes
fi
}
# Register completion for note and its aliases
compdef _note_complete note
compdef _note_complete n
compdef _note_complete nls
compdef _note_complete nrm
`)
}
return content.String()
}
// generateFishConfig generates the fish config content (aliases only, completion stays in standard location)
func generateFishConfig(aliasesEnabled bool, notePath string) string {
var content strings.Builder
content.WriteString("# Note CLI Shell Integration\n")
content.WriteString("# Generated by note CLI - Do not edit manually\n")
content.WriteString("# Regenerate with: note --configure\n\n")
if aliasesEnabled {
content.WriteString("# ============= ALIASES =============\n")
content.WriteString(fmt.Sprintf("alias n '%s'\n", notePath))
content.WriteString(fmt.Sprintf("alias nls '%s -l'\n", notePath))
content.WriteString(fmt.Sprintf("alias nrm '%s -d'\n", notePath))
}
return content.String()
}
// WriteCentralizedConfig writes the centralized config file for the specified shell
func WriteCentralizedConfig(shell string, aliasesEnabled, completionEnabled bool) error {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("error getting home directory: %w", err)
}
// Get the path to the note binary
notePath, err := os.Executable()
if err != nil {
// Fallback to checking PATH
notePath, err = exec.LookPath("note")
if err != nil {
return fmt.Errorf("could not determine note command path: %w", err)
}
}
var configPath string
var content string
switch shell {
case "bash":
configPath = filepath.Join(homeDir, BashCentralizedConfig)
content = generateBashConfig(aliasesEnabled, completionEnabled, notePath)
case "zsh":
configPath = filepath.Join(homeDir, ZshCentralizedConfig)
content = generateZshConfig(aliasesEnabled, completionEnabled, notePath)
case "fish":
configPath = filepath.Join(homeDir, FishCentralizedConfig)
content = generateFishConfig(aliasesEnabled, notePath)
default:
return fmt.Errorf("unsupported shell: %s", shell)
}
if err := os.WriteFile(configPath, []byte(content), 0644); err != nil {
return fmt.Errorf("error writing config file: %w", err)
}
return nil
}
// EnsureSourceLine adds the source line to the shell's RC file if not already present
func EnsureSourceLine(shell string) error {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("error getting home directory: %w", err)
}
var rcPath string
var sourceLine string
var configFile string
switch shell {
case "bash":
rcPath = filepath.Join(homeDir, ".bashrc")
configFile = BashCentralizedConfig
sourceLine = fmt.Sprintf("\n# Note CLI integration\n[ -f ~/%s ] && source ~/%s\n", configFile, configFile)
case "zsh":
rcPath = filepath.Join(homeDir, ".zshrc")
configFile = ZshCentralizedConfig
sourceLine = fmt.Sprintf("\n# Note CLI integration\n[ -f ~/%s ] && source ~/%s\n", configFile, configFile)
case "fish":
// Create fish config directory if it doesn't exist
fishConfigDir := filepath.Join(homeDir, ".config", "fish")
if err := os.MkdirAll(fishConfigDir, 0755); err != nil {
return fmt.Errorf("error creating fish config directory: %w", err)
}
rcPath = filepath.Join(fishConfigDir, "config.fish")
configFile = FishCentralizedConfig
sourceLine = fmt.Sprintf("\n# Note CLI integration\ntest -f ~/%s; and source ~/%s\n", configFile, configFile)
default:
return fmt.Errorf("unsupported shell: %s", shell)
}
// Check if source line already exists
if content, err := os.ReadFile(rcPath); err == nil {
contentStr := string(content)
// Check for either the config file name or the full source pattern
if strings.Contains(contentStr, configFile) ||
strings.Contains(contentStr, "# Note CLI integration") {
// Source line already exists
return nil
}
}
// Append source line to RC file
file, err := os.OpenFile(rcPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return fmt.Errorf("error opening %s: %w", rcPath, err)
}
defer file.Close()
if _, err := file.WriteString(sourceLine); err != nil {
return fmt.Errorf("error writing to %s: %w", rcPath, err)
}
return nil
}
// GetCentralizedConfigStatus checks what features are enabled in the centralized config
func GetCentralizedConfigStatus(shell string) (hasAliases, hasCompletion bool) {
homeDir, err := os.UserHomeDir()
if err != nil {
return false, false
}
var configPath string
switch shell {
case "bash":
configPath = filepath.Join(homeDir, BashCentralizedConfig)
case "zsh":
configPath = filepath.Join(homeDir, ZshCentralizedConfig)
case "fish":
configPath = filepath.Join(homeDir, FishCentralizedConfig)
// For fish, completion is stored separately in the standard location
fishCompletionDir := filepath.Join(homeDir, ".config", "fish", "completions")
fishCompletionFile := filepath.Join(fishCompletionDir, "note.fish")
if _, err := os.Stat(fishCompletionFile); err == nil {
hasCompletion = true
}
default:
return false, false
}
content, err := os.ReadFile(configPath)
if err != nil {
// For fish, we may have completion but no config file (aliases)
// Return what we've already detected
return hasAliases, hasCompletion
}
contentStr := string(content)
hasAliases = strings.Contains(contentStr, "# ============= ALIASES =============")
if shell != "fish" {
hasCompletion = strings.Contains(contentStr, "# ============= COMPLETION =============")
}
return hasAliases, hasCompletion
}
// CleanupLegacyConfig removes old-style configuration files and inline entries
func CleanupLegacyConfig(shell string) error {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("error getting home directory: %w", err)
}
switch shell {
case "bash":
// Remove old .note.bash file
legacyBashFile := filepath.Join(homeDir, ".note.bash")
os.Remove(legacyBashFile)
// Clean up legacy entries from .bashrc
bashrc := filepath.Join(homeDir, ".bashrc")
cleanupLegacyShellConfig(bashrc)
cleanupLegacyShellConfig(filepath.Join(homeDir, ".bash_profile"))
cleanupLegacyShellConfig(filepath.Join(homeDir, ".profile"))
case "zsh":
// Remove old .note.zsh file
legacyZshFile := filepath.Join(homeDir, ".note.zsh")
os.Remove(legacyZshFile)
// Clean up legacy entries from .zshrc
zshrc := filepath.Join(homeDir, ".zshrc")
cleanupLegacyShellConfig(zshrc)
case "fish":
// Clean up legacy entries from config.fish
fishConfig := filepath.Join(homeDir, ".config", "fish", "config.fish")
cleanupLegacyFishConfig(fishConfig)
}
return nil
}
// cleanupLegacyShellConfig removes old note command aliases and completion source lines from shell config
func cleanupLegacyShellConfig(configFile string) {
content, err := os.ReadFile(configFile)
if err != nil {
return
}
lines := strings.Split(string(content), "\n")
var newLines []string
inNoteSection := false
for _, line := range lines {
trimmedLine := strings.TrimSpace(line)
// Detect start of note sections
if trimmedLine == "# note command aliases" || trimmedLine == "# note command completion" {
inNoteSection = true
continue
}
// Skip old source lines for .note.bash/.note.zsh
if strings.Contains(line, ".note.bash") || strings.Contains(line, ".note.zsh") {
continue
}
// Skip inline alias definitions that are part of note
if inNoteSection {
// Check if this line is a note-related alias
if (strings.Contains(line, "alias n=") || strings.Contains(line, "alias nls=") || strings.Contains(line, "alias nrm=")) &&
strings.Contains(line, "note") {
continue
}
// Check if this line is a note-related source/completion line
if strings.Contains(line, "source") && strings.Contains(line, "note") {
continue
}
if strings.Contains(line, "autoload") && strings.Contains(line, "compinit") {
continue
}
// If we hit a non-empty, non-note line, we're out of the section
if trimmedLine != "" && !strings.HasPrefix(trimmedLine, "#") {
inNoteSection = false
}
// Skip empty lines within the note section
if trimmedLine == "" {
continue
}
}
// Skip standalone note alias lines (not in a section)
if (strings.Contains(line, "alias n='") || strings.Contains(line, "alias nls='") || strings.Contains(line, "alias nrm='")) &&
strings.Contains(line, "note") {
continue
}
newLines = append(newLines, line)
}
// Remove consecutive empty lines at the end
for len(newLines) > 1 && strings.TrimSpace(newLines[len(newLines)-1]) == "" && strings.TrimSpace(newLines[len(newLines)-2]) == "" {
newLines = newLines[:len(newLines)-1]
}
// Write cleaned content back
newContent := strings.Join(newLines, "\n")
if len(newContent) > 0 && !strings.HasSuffix(newContent, "\n") {
newContent += "\n"
}
os.WriteFile(configFile, []byte(newContent), 0644)
}
// cleanupLegacyFishConfig removes old note command aliases from fish config
func cleanupLegacyFishConfig(configFile string) {
content, err := os.ReadFile(configFile)
if err != nil {
return
}
lines := strings.Split(string(content), "\n")
var newLines []string
skipNext := false
skipCount := 0
for _, line := range lines {
// Skip "# note command aliases" header and following alias lines
if strings.Contains(line, "# note command aliases") {
skipNext = true
skipCount = 3 // Skip the next 3 lines (the alias lines)