Skip to content

Commit

Permalink
feat(template): add .AbsolutePWD
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Jun 13, 2024
1 parent 38e68f9 commit 821e45e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/platform/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/platform/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 += `\`
Expand Down
8 changes: 3 additions & 5 deletions src/segments/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/template/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
knownVariables = []string{
"Root",
"PWD",
"AbsolutePWD",
"Folder",
"Shell",
"ShellVersion",
Expand Down
5 changes: 3 additions & 2 deletions website/docs/configuration/templates.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down Expand Up @@ -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. |
Expand Down

0 comments on commit 821e45e

Please sign in to comment.