feat(wrapper): continue last mode
This commit is contained in:
+45
-3
@@ -3,11 +3,13 @@ package root
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
@@ -18,6 +20,47 @@ import (
|
|||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type State struct {
|
||||||
|
Mode string `json:"mode"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func getStateFile() string {
|
||||||
|
home, err := os.UserHomeDir()
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
dir := filepath.Join(home, ".iris")
|
||||||
|
os.MkdirAll(dir, 0755)
|
||||||
|
return filepath.Join(dir, "state.json")
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadMode() string {
|
||||||
|
file := getStateFile()
|
||||||
|
if file != "" {
|
||||||
|
data, err := os.ReadFile(file)
|
||||||
|
if err == nil {
|
||||||
|
var state State
|
||||||
|
if err := json.Unmarshal(data, &state); err == nil {
|
||||||
|
if state.Mode == "history" || state.Mode == "spec" {
|
||||||
|
return state.Mode
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "spec"
|
||||||
|
}
|
||||||
|
|
||||||
|
func saveMode(mode string) {
|
||||||
|
file := getStateFile()
|
||||||
|
if file != "" {
|
||||||
|
state := State{Mode: mode}
|
||||||
|
data, err := json.MarshalIndent(state, "", " ")
|
||||||
|
if err == nil {
|
||||||
|
os.WriteFile(file, data, 0644)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// runWrapper sets up the pty environment, launches the shell,
|
// runWrapper sets up the pty environment, launches the shell,
|
||||||
// and manages the main input loop to provide real-time suggestions
|
// and manages the main input loop to provide real-time suggestions
|
||||||
// it handles raw terminal mode to intercept keystrokes and
|
// it handles raw terminal mode to intercept keystrokes and
|
||||||
@@ -148,7 +191,7 @@ func runWrapper() {
|
|||||||
|
|
||||||
var naiveBuffer string
|
var naiveBuffer string
|
||||||
suggestionsEnabled := true
|
suggestionsEnabled := true
|
||||||
mode := "spec"
|
mode := loadMode()
|
||||||
|
|
||||||
// renderOverlay decides whether to draw the suggestion menu based on current state
|
// renderOverlay decides whether to draw the suggestion menu based on current state
|
||||||
renderOverlay := func() {
|
renderOverlay := func() {
|
||||||
@@ -253,6 +296,7 @@ func runWrapper() {
|
|||||||
} else {
|
} else {
|
||||||
mode = "spec"
|
mode = "spec"
|
||||||
}
|
}
|
||||||
|
saveMode(mode)
|
||||||
shouldOverlayDraw = true
|
shouldOverlayDraw = true
|
||||||
// enter: enter behavior is a bit different from tab suggestions in code editor
|
// enter: enter behavior is a bit different from tab suggestions in code editor
|
||||||
// I want it to execute the command anyway and ignore the suggestions
|
// I want it to execute the command anyway and ignore the suggestions
|
||||||
@@ -278,7 +322,6 @@ func runWrapper() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
naiveBuffer = selected
|
naiveBuffer = selected
|
||||||
mode = "spec"
|
|
||||||
|
|
||||||
ptmx.Write([]byte{0x15}) // ctrl+u to clear line
|
ptmx.Write([]byte{0x15}) // ctrl+u to clear line
|
||||||
ptmx.Write([]byte(selected))
|
ptmx.Write([]byte(selected))
|
||||||
@@ -309,7 +352,6 @@ func runWrapper() {
|
|||||||
shouldOverlayDraw = true
|
shouldOverlayDraw = true
|
||||||
case '\r', 0x03, 0x15, 0x0C: // enter, ctrl+c, ctrl+u, ctrl+l: clear buffer on line reset
|
case '\r', 0x03, 0x15, 0x0C: // enter, ctrl+c, ctrl+u, ctrl+l: clear buffer on line reset
|
||||||
naiveBuffer = ""
|
naiveBuffer = ""
|
||||||
mode = "spec"
|
|
||||||
os.Stdout.Write([]byte(overlay.ClearAndDisable()))
|
os.Stdout.Write([]byte(overlay.ClearAndDisable()))
|
||||||
default:
|
default:
|
||||||
// track normal printable characters in the buffer for matching
|
// track normal printable characters in the buffer for matching
|
||||||
|
|||||||
Reference in New Issue
Block a user