Назначение префикса и placeholder через переменные оболочки

This commit is contained in:
2026-10-25 23:28:11 +03:00
parent ee9a1597a0
commit a5881619df
4 changed files with 81 additions and 26 deletions
+13 -9
View File
@@ -21,9 +21,9 @@ type model struct {
errText string // Храним ошибку, если команда не найдена errText string // Храним ошибку, если команда не найдена
} }
func initialModel() model { func initialModel(placeholderText string, prefixText string) model {
ti := textinput.New() ti := textinput.New()
ti.Placeholder = "Type a command..." ti.Placeholder = placeholderText
ti.Focus() ti.Focus()
ti.CharLimit = 250 ti.CharLimit = 250
ti.SetWidth(60) ti.SetWidth(60)
@@ -33,8 +33,7 @@ func initialModel() model {
Foreground(lipgloss.Color("#FF00FA")). Foreground(lipgloss.Color("#FF00FA")).
Bold(true) Bold(true)
ti.Prompt = prefixStyle.Render("noko> ") ti.Prompt = prefixStyle.Render(prefixText)
//prefixStyle=lipgloss.NewStyle().Foreground(lipgloss.Color("#00FFD1"))
return model{ return model{
textInput: ti, textInput: ti,
@@ -51,7 +50,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch msg.String() {
case "ctrl+c", "esc": case "ctrl+c":
m.textInput.SetValue("")
return m, nil
case "ctrl+d":
return m, tea.Quit return m, tea.Quit
case "enter": case "enter":
@@ -108,7 +110,6 @@ func (m model) View() tea.View {
errStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#FF0000")) errStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#FF0000"))
s.WriteString(errStyle.Render(m.errText)) s.WriteString(errStyle.Render(m.errText))
s.WriteString("\n") s.WriteString("\n")
m.errText = ""
} }
s.WriteString(m.textInput.View()) s.WriteString(m.textInput.View())
@@ -117,13 +118,16 @@ func (m model) View() tea.View {
return tea.NewView(s.String()) return tea.NewView(s.String())
} }
func main() { func main() {
// Применение setup скрипта
executor.ExecSetUpScript()
placeholderText := executor.GetVariable("NSHELL_NO_TEXT_PLACEHOLDER")
prefixText := executor.GetVariable("NSHELL_PREFIX")
// Запускаем программу // Запускаем программу
p := tea.NewProgram(initialModel()) p := tea.NewProgram(initialModel(placeholderText, prefixText))
if _, err := p.Run(); err != nil { if _, err := p.Run(); err != nil {
fmt.Printf("Error running noko-shell: %v\n", err) fmt.Printf("Error running noko-shell: %v\n", err)
os.Exit(1) os.Exit(1)
} }
} }
+10 -5
View File
@@ -3,13 +3,15 @@ package executor
import ( import (
"errors" "errors"
"os/exec"
"os" "os"
"os/exec"
"strings" "strings"
) )
var ErrBuiltInExit = errors.New("exit command") var (
var localVars = make(map[string]string) ErrBuiltInExit = errors.New("exit command")
localVars = make(map[string]string)
)
func PrepareCommand(in string) (*exec.Cmd, error) { func PrepareCommand(in string) (*exec.Cmd, error) {
in = strings.TrimSpace(in) in = strings.TrimSpace(in)
@@ -26,12 +28,16 @@ func PrepareCommand(in string) (*exec.Cmd, error) {
} }
in = os.Expand(in, GetVariable) in = os.Expand(in, GetVariable)
in = strings.TrimSpace(in)
parts := strings.Fields(in) parts := strings.Fields(in)
head := parts[0] head := parts[0]
args := parts[1:] args := parts[1:]
if len(parts) == 0 {
return nil, nil // Если после os.Expand строка стала пустой, безопасно выходим
}
switch head { switch head {
case "exit": case "exit":
return nil, ErrBuiltInExit return nil, ErrBuiltInExit
@@ -49,7 +55,6 @@ func PrepareCommand(in string) (*exec.Cmd, error) {
return exec.Command(fullPath, args...), nil return exec.Command(fullPath, args...), nil
} }
func GetVariable(key string) string { func GetVariable(key string) string {
if val, exists := localVars[key]; exists { if val, exists := localVars[key]; exists {
return val return val
+44
View File
@@ -0,0 +1,44 @@
package executor
import (
"bufio"
"fmt"
"os"
"strings"
)
const defaultStartScript string = `
echo meow meow
NSHELL_NO_TEXT_PLACEHOLDER=Type Noko Meow Meow
NSHELL_PREFIX=noko>
`
func ExecSetUpScript() {
scanner := bufio.NewScanner(strings.NewReader(defaultStartScript))
for scanner.Scan() {
line := scanner.Text()
cmd, err := PrepareCommand(line)
if err != nil {
fmt.Printf("Error %s\n", err.Error())
}
if err != nil {
fmt.Printf("Error preparing command: %s\n", err.Error())
continue
}
if cmd == nil {
continue
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
fmt.Printf("Ошибка при выполнении команды: %s\n", err.Error())
}
}
if err := scanner.Err(); err != nil {
fmt.Println("Ошибка чтения:", err)
}
}
+3 -1
View File
@@ -6,7 +6,9 @@ import "strings"
var defaultSuggestions = []string{"exit"} var defaultSuggestions = []string{"exit"}
func Completer(in string) []string { func Completer(in string) []string {
if in=="" {return nil} if in == "" {
return nil
}
var matched []string var matched []string
for _, cmd := range defaultSuggestions { for _, cmd := range defaultSuggestions {
if strings.HasPrefix(cmd, in) { if strings.HasPrefix(cmd, in) {