summaryrefslogtreecommitdiff
path: root/internal/repl/prompt.go
blob: 37bb23a19ca079047b00dd9bebaa4969fd197685 (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
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Paul Buetow

package repl

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

// PromptBuilder constructs a prompt instance with the given configuration.
// It uses the builder pattern to configure all aspects of the prompt before calling Build.
type PromptBuilder struct {
	prefix     string
	title      string
	history    []string
	executor   func(string)
	completer  func(prompt.Document) []prompt.Suggest
	livePrefix func() (string, bool)
}

// NewPromptBuilder creates a new prompt builder with default values.
// Default values:
//   - prefix: "> "
//   - title: "gt - Percentage Calculator"
//   - executor: empty function
//   - completer: function that returns nil
//   - livePrefix: function that returns ("> ", true)
//
// Returns a new PromptBuilder instance
func NewPromptBuilder() *PromptBuilder {
	return &PromptBuilder{
		prefix:     "> ",
		title:      "gt - Percentage Calculator",
		executor:   func(string) {},
		completer:  func(prompt.Document) []prompt.Suggest { return nil },
		livePrefix: func() (string, bool) { return "> ", true },
	}
}

// SetPrefix sets the prompt prefix string.
// This is the string displayed before each input line (default: "> ").
//
// prefix: the prefix string to display
// Returns the builder for method chaining
func (b *PromptBuilder) SetPrefix(prefix string) *PromptBuilder {
	b.prefix = prefix
	return b
}

// SetTitle sets the prompt title.
// This title is displayed in the terminal window/tab title.
//
// title: the title string to set
// Returns the builder for method chaining
func (b *PromptBuilder) SetTitle(title string) *PromptBuilder {
	b.title = title
	return b
}

// SetHistory sets the history for the prompt.
// The history is a slice of strings representing previously entered commands.
// This allows users to navigate through their command history using arrow keys.
//
// history: the slice of history entries
// Returns the builder for method chaining
func (b *PromptBuilder) SetHistory(history []string) *PromptBuilder {
	b.history = history
	return b
}

// SetExecutor sets the executor function for processing input.
// The executor is called for each non-empty input line after the user presses Enter.
//
// executor: the function to call with each input line
// Returns the builder for method chaining
func (b *PromptBuilder) SetExecutor(executor func(string)) *PromptBuilder {
	b.executor = executor
	return b
}

// SetCompleter sets the completer function for auto-completion.
// The completer is called when the user presses Tab to get suggestions.
//
// completer: the function to call for tab-completion suggestions
// Returns the builder for method chaining
func (b *PromptBuilder) SetCompleter(completer func(prompt.Document) []prompt.Suggest) *PromptBuilder {
	b.completer = completer
	return b
}

// SetLivePrefix sets the live prefix function.
// The live prefix is displayed on the left side of the current input line
// and can be used to show context-dependent information (e.g., multi-line input).
//
// livePrefix: the function that returns the current prefix string
// Returns the builder for method chaining
func (b *PromptBuilder) SetLivePrefix(livePrefix func() (string, bool)) *PromptBuilder {
	b.livePrefix = livePrefix
	return b
}

// Build creates and returns a new prompt instance with the configured options.
// After calling Build, the PromptBuilder should not be modified.
//
// Returns a new prompt.Prompt instance ready to use with prompt.Run()
func (b *PromptBuilder) Build() *prompt.Prompt {
	return prompt.New(
		b.executor,
		b.completer,
		prompt.OptionTitle(b.title),
		prompt.OptionPrefix(b.prefix),
		prompt.OptionLivePrefix(b.livePrefix),
		prompt.OptionHistory(b.history),
	)
}