summaryrefslogtreecommitdiff
path: root/internal/repl/repl.go
blob: 0f690e937b12f720b8775016d841a1433365147d (plain)
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
package repl

import (
	"bufio"
	"fmt"
	"os"
	"os/signal"
	"path/filepath"
	"strings"
	"syscall"

	"codeberg.org/snonux/perc/internal/calculator"
	"github.com/mattn/go-isatty"

	"github.com/c-bata/go-prompt"
)

const historyFile = ".perc_history"

// executor runs a calculation command and returns the result
func executor(input string) {
	input = strings.TrimSpace(input)
	if input == "" {
		return
	}

	// Check if it's a built-in command
	if cmd, ok := isBuiltinCommand(input); ok {
		output, err := ExecuteCommand(cmd)
		if err != nil {
			fmt.Printf("Error: %v\n", err)
		}
		if output != "" {
			fmt.Println(output)
		}
		// Don't add built-in commands to history
		return
	}

	// Run the calculation
	result, err := calculator.Parse(input)
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		return
	}
	fmt.Println(result)
}

// isBuiltinCommand checks if input starts with a built-in command
func isBuiltinCommand(input string) (string, bool) {
	args := strings.Fields(input)
	if len(args) == 0 {
		return "", false
	}

	cmd := strings.ToLower(args[0])
	for _, builtin := range builtinCommands {
		if cmd == builtin {
			return input, true
		}
	}
	return "", false
}

// getHistoryPath returns the path to the history file
func getHistoryPath() string {
	home, err := os.UserHomeDir()
	if err != nil {
		return ""
	}
	return filepath.Join(home, historyFile)
}

// loadHistory loads history from file
func loadHistory() []string {
	historyPath := getHistoryPath()
	if historyPath == "" {
		return nil
	}

	file, err := os.Open(historyPath)
	if err != nil {
		return nil
	}
	defer file.Close()

	var history []string
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		history = append(history, scanner.Text())
	}
	return history
}

// saveHistory saves history to file
func saveHistory(history []string) error {
	historyPath := getHistoryPath()
	if historyPath == "" {
		return nil
	}

	// Keep only last 1000 entries to prevent unlimited growth
	if len(history) > 1000 {
		history = history[len(history)-1000:]
	}

	file, err := os.Create(historyPath)
	if err != nil {
		return err
	}
	defer file.Close()

	writer := bufio.NewWriter(file)
	for _, entry := range history {
		if _, err := writer.WriteString(entry + "\n"); err != nil {
			return err
		}
	}
	return writer.Flush()
}

// RunREPL starts the interactive REPL
func RunREPL() error {
	// Check if stdin is a TTY
	if !isatty.IsTerminal(os.Stdin.Fd()) {
		fmt.Fprintln(os.Stderr, "REPL mode requires a TTY. Use 'perc <calculation>' for non-interactive mode.")
		return fmt.Errorf("stdin is not a TTY")
	}

	history := loadHistory()

	p := prompt.New(
		executor,
		completer,
		prompt.OptionTitle("perc - Percentage Calculator"),
		prompt.OptionPrefix("perc> "),
		prompt.OptionLivePrefix(func() (string, bool) {
			return "perc> ", true
		}),
		prompt.OptionHistory(history),
	)

	// Handle SIGINT gracefully
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, syscall.SIGINT)

	go func() {
		<-sigChan
		fmt.Println("\nUse 'quit' or 'exit' to exit, or Ctrl+D")
	}()

	// Run the prompt
	p.Run()

	// Note: History is not saved automatically in this version
	// The prompt library stores it in memory but doesn't expose a getter

	return nil
}

// completer provides auto-completion for built-in commands
func completer(d prompt.Document) []prompt.Suggest {
	text := d.GetWordBeforeCursor()
	if text == "" {
		return nil
	}

	var suggestions []prompt.Suggest
	for _, cmd := range builtinCommands {
		if strings.HasPrefix(strings.ToLower(cmd), strings.ToLower(text)) {
			suggestions = append(suggestions, prompt.Suggest{Text: cmd, Description: getCommandDescription(cmd)})
		}
	}
	return suggestions
}

func getCommandDescription(cmd string) string {
	descriptions := map[string]string{
		"help":   "Show help information",
		"clear":  "Clear the screen",
		"quit":   "Exit the REPL",
		"exit":   "Exit the REPL",
	}
	return descriptions[cmd]
}