feat(template): add .PSWD

This commit is contained in:
L. Yeung 2024-08-06 17:54:56 +08:00 committed by Jan De Dobbeleer
parent 69556d6fb6
commit 485f45c496
5 changed files with 12 additions and 3 deletions

View file

@ -10,6 +10,7 @@ type Template struct {
Root bool Root bool
PWD string PWD string
AbsolutePWD string AbsolutePWD string
PSWD string
Folder string Folder string
Shell string Shell string
ShellVersion string ShellVersion string

View file

@ -62,7 +62,7 @@ func (term *Terminal) Init() {
if term.CmdFlags.Plain { if term.CmdFlags.Plain {
log.Plain() log.Plain()
log.Debug("dlain mode enabled") log.Debug("plain mode enabled")
} }
initCache := func(fileName string) *cache.File { initCache := func(fileName string) *cache.File {
@ -674,6 +674,8 @@ func (term *Terminal) TemplateCache() *cache.Template {
tmplCache.AbsolutePWD, _ = term.RunCommand("wslpath", "-m", pwd) tmplCache.AbsolutePWD, _ = term.RunCommand("wslpath", "-m", pwd)
} }
tmplCache.PSWD = term.CmdFlags.PSWD
tmplCache.Folder = Base(term, pwd) tmplCache.Folder = Base(term, pwd)
if term.GOOS() == WINDOWS && strings.HasSuffix(tmplCache.Folder, ":") { if term.GOOS() == WINDOWS && strings.HasSuffix(tmplCache.Folder, ":") {
tmplCache.Folder += `\` tmplCache.Folder += `\`

View file

@ -109,7 +109,11 @@ func (pt *Path) Enabled() bool {
pt.setStyle() pt.setStyle()
pwd := pt.env.Pwd() pwd := pt.env.Pwd()
pt.Location = strings.ReplaceAll(pt.env.TemplateCache().AbsolutePWD, `\`, `/`) pt.Location = pt.env.TemplateCache().AbsolutePWD
if pt.env.GOOS() == runtime.WINDOWS {
pt.Location = strings.ReplaceAll(pt.Location, `\`, `/`)
}
pt.StackCount = pt.env.StackCount() pt.StackCount = pt.env.StackCount()
pt.Writable = pt.env.DirIsWritable(pwd) pt.Writable = pt.env.DirIsWritable(pwd)
return true return true
@ -609,7 +613,7 @@ func (pt *Path) normalizePath(path string) string {
lastChar = clean[len(clean)-1:][0] lastChar = clean[len(clean)-1:][0]
} }
if char == '/' && lastChar != 60 { // 60 == <, this is done to ovoid replacing color codes if char == '/' && lastChar != 60 { // 60 == <, this is done to avoid replacing color codes
clean = append(clean, 92) // 92 == \ clean = append(clean, 92) // 92 == \
continue continue
} }

View file

@ -28,6 +28,7 @@ var (
"Root", "Root",
"PWD", "PWD",
"AbsolutePWD", "AbsolutePWD",
"PSWD",
"Folder", "Folder",
"Shell", "Shell",
"ShellVersion", "ShellVersion",

View file

@ -22,6 +22,7 @@ it with `.$` to reference it directly.
| `.Root` | `boolean` | is the current user root/admin or not | | `.Root` | `boolean` | is the current user root/admin or not |
| `.PWD` | `string` | the current working directory (`~` for `$HOME`) | | `.PWD` | `string` | the current working directory (`~` for `$HOME`) |
| `.AbsolutePWD` | `string` | the current working directory (unaltered) | | `.AbsolutePWD` | `string` | the current working directory (unaltered) |
| `.PSWD ` | `string` | the current non-filesystem working directory in PowerShell |
| `.Folder` | `string` | the current working folder | | `.Folder` | `string` | the current working folder |
| `.Shell` | `string` | the current shell name | | `.Shell` | `string` | the current shell name |
| `.ShellVersion` | `string` | the current shell version | | `.ShellVersion` | `string` | the current shell version |