feat(template): add .AbsolutePWD

This commit is contained in:
Jan De Dobbeleer 2024-06-13 14:58:13 +02:00 committed by Jan De Dobbeleer
parent 38e68f9017
commit 821e45e968
5 changed files with 14 additions and 7 deletions

View file

@ -143,6 +143,7 @@ func (c *commandCache) get(command string) (string, bool) {
type TemplateCache struct {
Root bool
PWD string
AbsolutePWD string
Folder string
Shell string
ShellVersion string

View file

@ -790,6 +790,12 @@ func (env *Shell) TemplateCache() *TemplateCache {
pwd := env.Pwd()
tmplCache.PWD = ReplaceHomeDirPrefixWithTilde(env, pwd)
tmplCache.AbsolutePWD = pwd
if env.IsWsl() {
tmplCache.AbsolutePWD, _ = env.RunCommand("wslpath", "-m", pwd)
}
tmplCache.Folder = Base(env, pwd)
if env.GOOS() == WINDOWS && strings.HasSuffix(tmplCache.Folder, ":") {
tmplCache.Folder += `\`

View file

@ -99,13 +99,11 @@ func (pt *Path) Enabled() bool {
if len(pt.pwd) == 0 {
return false
}
pt.setStyle()
pwd := pt.env.Pwd()
if pt.env.IsWsl() {
pt.Location, _ = pt.env.RunCommand("wslpath", "-m", pwd)
} else {
pt.Location = pwd
}
pt.Location = pt.env.TemplateCache().AbsolutePWD
pt.StackCount = pt.env.StackCount()
pt.Writable = pt.env.DirIsWritable(pwd)
return true

View file

@ -23,6 +23,7 @@ var (
knownVariables = []string{
"Root",
"PWD",
"AbsolutePWD",
"Folder",
"Shell",
"ShellVersion",

View file

@ -20,7 +20,8 @@ it with `.$` to reference it directly.
| Name | Type | Description |
| --------------- | --------- | ----------------------------------------------------------------- |
| `.Root` | `boolean` | is the current user root/admin or not |
| `.PWD` | `string` | the current working directory |
| `.PWD` | `string` | the current working directory (`~` for `$HOME`) |
| `.AbsolutePWD` | `string` | the current working directory (unaltered) |
| `.Folder` | `string` | the current working folder |
| `.Shell` | `string` | the current shell name |
| `.ShellVersion` | `string` | the current shell version |
@ -140,7 +141,7 @@ end
| `{{.Var}}` | Variable in a struct, where Var is a variable. |
| `{{- .Var -}}` | Remove extra white space surrounding the .Var variable and remove the newline. Either side is fine too. |
| `{{ $planet := "Earth"}}` | `{{ $planet }}` Store a value in a custom variable to reference later. Note that .$ is used to reference the global/parent context, like in the full example below with $. |
| `Hi {{if .Name}} {{.Name}} {{else}} visitor {{end}}` | If-else statement. If will evaluate whether or not the argument is empty. Using the elseif conditional is also an option. The not negation is available too. |
| `Hi {{if .Name}} {{.Name}} {{else}} visitor {{end}}` | If-else statement. If will evaluate whether or not the argument is empty. Using the elseif conditional is also an option. The not negation is available too. |
| `{{if and .Arg1 .Arg2}} both complete. {{else}} incomplete. {{end}}` | The and function compares multiple arguments to return the boolean AND (if arg1 then arg2 else arg1). Both arguments are evaluated. The or function compares multiple arguments to return the boolean OR. Similar to if arg1 then arg1 else arg2, so arg2 will never be evaluated if arg1 is false (not empty). |
| `{{with .Var}} {{end}}` | With statement, where Var is a variable. It skips the block if the variable is absent. |
| `{{range .Array}} {{end}}` | Range statement, where Array is an array, slice, map, or channel. |