Skip to content

Commit 67e87e7

Browse files
committed
Autocomplete /session subcommands
1 parent 181c8ba commit 67e87e7

1 file changed

Lines changed: 97 additions & 1 deletion

File tree

src/repl/repl.go

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"os/exec"
1414
"os/signal"
1515
"path/filepath"
16-
"regexp"
1716
"sort"
1817
"strconv"
1918
"strings"
@@ -653,6 +652,20 @@ func (r *REPL) handleTabCompletion(line *strings.Builder) {
653652
}
654653
}
655654

655+
// Handle tab completion for /session subcommands
656+
if strings.HasPrefix(input, "/session ") && len(parts) >= 2 {
657+
if len(parts) == 2 {
658+
// Complete /session subcommands
659+
subcmd := parts[1]
660+
r.handleSessionSubcommandCompletion(line, subcmd)
661+
return
662+
} else if len(parts) == 3 && (parts[1] == "use" || parts[1] == "del") {
663+
// Complete session names for use/del
664+
r.handleSessionNameCompletion(line, "/session "+parts[1], parts[2])
665+
return
666+
}
667+
}
668+
656669
// Only handle tab completion at the beginning of the line for commands
657670
if !(strings.HasPrefix(input, "/") || strings.HasPrefix(input, "#") || strings.HasPrefix(input, "$")) {
658671
return
@@ -1035,6 +1048,89 @@ func (r *REPL) handleSessionCommand(args []string) error {
10351048
return nil
10361049
}
10371050

1051+
// handleSessionSubcommandCompletion handles tab completion for /session subcommands
1052+
func (r *REPL) handleSessionSubcommandCompletion(line *strings.Builder, subcmd string) {
1053+
// Subcommands for /session
1054+
subcommands := []string{"new", "list", "use", "del", "purge"}
1055+
sort.Strings(subcommands)
1056+
1057+
// Check if we need fresh options
1058+
if r.completeState == 0 || len(r.completeOptions) == 0 || r.completePrefix != "/session " {
1059+
r.completePrefix = "/session "
1060+
r.completeOptions = nil
1061+
for _, sc := range subcommands {
1062+
if strings.HasPrefix(sc, subcmd) {
1063+
r.completeOptions = append(r.completeOptions, r.completePrefix+sc)
1064+
}
1065+
}
1066+
if len(r.completeOptions) == 0 {
1067+
return
1068+
}
1069+
r.completeState = 1
1070+
r.completeIdx = 0
1071+
}
1072+
1073+
// Cycle through options
1074+
if len(r.completeOptions) > 0 {
1075+
current := line.String()
1076+
next := r.completeOptions[r.completeIdx]
1077+
for i := 0; i < len(current); i++ {
1078+
fmt.Print("\b \b")
1079+
}
1080+
fmt.Print(next)
1081+
line.Reset()
1082+
line.WriteString(next)
1083+
r.cursorPos = line.Len()
1084+
r.completeIdx = (r.completeIdx + 1) % len(r.completeOptions)
1085+
}
1086+
}
1087+
1088+
// handleSessionNameCompletion handles tab completion for session names
1089+
func (r *REPL) handleSessionNameCompletion(line *strings.Builder, command, partialName string) {
1090+
homeDir, err := os.UserHomeDir()
1091+
if err != nil {
1092+
return
1093+
}
1094+
chatDir := filepath.Join(homeDir, ".mai", "chat")
1095+
1096+
if r.completeState == 0 || !strings.HasPrefix(line.String(), r.completePrefix) {
1097+
files, err := os.ReadDir(chatDir)
1098+
if err != nil {
1099+
return
1100+
}
1101+
1102+
r.completeOptions = nil
1103+
for _, file := range files {
1104+
if !file.IsDir() && strings.HasSuffix(file.Name(), ".json") {
1105+
sessionName := strings.TrimSuffix(file.Name(), ".json")
1106+
if strings.HasPrefix(sessionName, partialName) {
1107+
r.completeOptions = append(r.completeOptions, command+" "+sessionName)
1108+
}
1109+
}
1110+
}
1111+
if len(r.completeOptions) == 0 {
1112+
return
1113+
}
1114+
sort.Strings(r.completeOptions)
1115+
r.completeState = 1
1116+
r.completeIdx = 0
1117+
r.completePrefix = command + " "
1118+
}
1119+
1120+
if len(r.completeOptions) > 0 {
1121+
current := line.String()
1122+
next := r.completeOptions[r.completeIdx]
1123+
for i := 0; i < len(current); i++ {
1124+
fmt.Print("\b \b")
1125+
}
1126+
fmt.Print(next)
1127+
line.Reset()
1128+
line.WriteString(next)
1129+
r.cursorPos = line.Len()
1130+
r.completeIdx = (r.completeIdx + 1) % len(r.completeOptions)
1131+
}
1132+
}
1133+
10381134
func (r *REPL) saveSession(sessionName string) error {
10391135
homeDir, err := os.UserHomeDir()
10401136
if err != nil {

0 commit comments

Comments
 (0)