Fix terminal restore state and add set themes via local vars

This commit is contained in:
2026-10-22 00:50:36 +03:00
parent b40fbc95f8
commit da0fd7ddaf
5 changed files with 91 additions and 37 deletions
+26 -3
View File
@@ -2,21 +2,44 @@ package main
import ( import (
"fmt" "fmt"
"os"
"strings"
"nshell/executor" "nshell/executor"
"nshell/suggestions" "nshell/suggestions"
"github.com/c-bata/go-prompt" "github.com/c-bata/go-prompt"
"golang.org/x/term"
) )
var oldStateTerminal *term.State
func getColor(color string) prompt.Color {
color = strings.ToUpper(color)
switch color {
case "CYAN":
return prompt.Cyan
case "RED":
return prompt.Red
default:
return prompt.Green
}
}
func main() { func main() {
fd := int(os.Stdin.Fd())
if state, err := term.GetState(fd); err == nil {
oldStateTerminal = state
}
fmt.Println("Wellcome to noko-shell(nshell) :3") fmt.Println("Wellcome to noko-shell(nshell) :3")
options := []prompt.Option{ options := []prompt.Option{
prompt.OptionPrefix("nshell ^^>"), prompt.OptionLivePrefix(func() (string, bool) {
prompt.OptionPrefixTextColor(prompt.Cyan), return executor.GetVariable("NSHELL_PREFIX_TEXT"), true
}),
prompt.OptionPrefixTextColor(getColor(executor.GetVariable("NSHELL_PREFIX_COLOR"))),
} }
p := prompt.New(executor.Exec, suggestions.Completer, options...) p := prompt.New(executor.BuildExecutor(oldStateTerminal, &fd), suggestions.Completer, options...)
p.Run() p.Run()
} }
-6
View File
@@ -1,6 +0,0 @@
// Package config current shell config
package config
type Config struct {
UserLine string
}
+32 -1
View File
@@ -6,10 +6,27 @@ import (
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
"golang.org/x/term"
) )
func Exec(line string) { var localVars = make(map[string]string)
func BuildExecutor(oldStateTerminal *term.State, fd *int) func(string) {
return func(line string) {
line = strings.TrimSpace(line) line = strings.TrimSpace(line)
if strings.Contains(line, "=") && !strings.HasPrefix(line, "export ") {
parts := strings.SplitN(line, "=", 2)
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
localVars[key] = value
return
}
line = os.Expand(line, GetVariable)
if line == "" { if line == "" {
return return
} }
@@ -20,6 +37,9 @@ func Exec(line string) {
if command == "exit" { if command == "exit" {
fmt.Println("Bye Bye my small deer :3") fmt.Println("Bye Bye my small deer :3")
if oldStateTerminal != nil {
_ = term.Restore(*fd, oldStateTerminal)
}
os.Exit(0) os.Exit(0)
} }
@@ -42,4 +62,15 @@ func Exec(line string) {
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Execution error: %v\n", err) fmt.Fprintf(os.Stderr, "Execution error: %v\n", err)
} }
}
}
func GetVariable(key string) string {
if val, exists := localVars[key]; exists {
return val
}
if val := os.Getenv(key); val != "" {
return val
}
return ""
} }
+5 -2
View File
@@ -2,7 +2,10 @@ module nshell
go 1.27.2 go 1.27.2
require github.com/c-bata/go-prompt v0.2.6 require (
github.com/c-bata/go-prompt v0.2.6
golang.org/x/term v0.47.0
)
require ( require (
github.com/mattn/go-colorable v0.1.7 // indirect github.com/mattn/go-colorable v0.1.7 // indirect
@@ -10,5 +13,5 @@ require (
github.com/mattn/go-runewidth v0.0.9 // indirect github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mattn/go-tty v0.0.3 // indirect github.com/mattn/go-tty v0.0.3 // indirect
github.com/pkg/term v1.2.0-beta.2 // indirect github.com/pkg/term v1.2.0-beta.2 // indirect
golang.org/x/sys v0.0.0-20200918174421-af09f7315aff // indirect golang.org/x/sys v0.49.0 // indirect
) )
+4 -1
View File
@@ -21,5 +21,8 @@ golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200918174421-af09f7315aff h1:1CPUrky56AcgSpxz/KfgzQWzfG09u5YOL8MvPYBlrL8=
golang.org/x/sys v0.0.0-20200918174421-af09f7315aff/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200918174421-af09f7315aff/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
golang.org/x/term v0.47.0 h1:cpyOwQkr2Nzxe2p6KnrTG7Ei2wtzjsuSjhpXXepZ/BM=
golang.org/x/term v0.47.0/go.mod h1:efAfAuG6RKABvg1ArGebRkvX6LXul14SM67o2e4mUCY=