mirror of
https://gitlab.com/pnx/gosh
synced 2026-06-16 07:04:57 +02:00
40 lines
674 B
Go
40 lines
674 B
Go
package prompt
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
|
|
"gosh/internal/paths"
|
|
)
|
|
|
|
func cwd(abbr bool) string {
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
return "?"
|
|
}
|
|
|
|
if abbr {
|
|
cwd = paths.AbbreviateHome(cwd)
|
|
}
|
|
return cwd
|
|
}
|
|
|
|
func hostname() string {
|
|
hostname, err := os.Hostname()
|
|
if err != nil {
|
|
return "?"
|
|
}
|
|
return hostname
|
|
}
|
|
|
|
func resolve(input, variable, value string) string {
|
|
return strings.ReplaceAll(input, variable, value)
|
|
}
|
|
|
|
func Parse(prompt string) string {
|
|
prompt = resolve(prompt, "%w", cwd(true))
|
|
prompt = resolve(prompt, "%W", cwd(false))
|
|
prompt = resolve(prompt, "%h", hostname())
|
|
prompt = resolve(prompt, "%u", os.ExpandEnv("$USER"))
|
|
return prompt
|
|
}
|