Initial commit
This commit is contained in:
commit
9d9d8ce7d5
19 changed files with 399 additions and 0 deletions
3
internal/prompt/doc.go
Normal file
3
internal/prompt/doc.go
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
// Package prompt expands shell prompt templates by resolving variables like
|
||||
// %u (user), %h (host), and %w (working directory).
|
||||
package prompt
|
||||
40
internal/prompt/parse.go
Normal file
40
internal/prompt/parse.go
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue