From 74fd5d11194d866a261a8f64d77c78a19ff2b174 Mon Sep 17 00:00:00 2001
From: Bence Ferdinandy <bence@ferdinandy.com>
Date: Sat, 17 Sep 2022 23:26:35 +0200
Subject: [PATCH] statusline-format: add %p placeholder for current path

Allow showing the current working directory in the statusline via
[statusline] render-format=%p, which is useful if the user changes
directories often.

Signed-off-by: Bence Ferdinandy <bence@ferdinandy.com>
Tested-by: Tim Culverhouse <tim@timculverhouse.com>
Acked-by: Robin Jarry <robin@jarry.cc>
---
 commands/cd.go             |  1 +
 doc/aerc-config.5.scd      |  2 ++
 lib/statusline/renderer.go | 11 +++++++++++
 3 files changed, 14 insertions(+)

diff --git a/commands/cd.go b/commands/cd.go
index d856a06..dc05adb 100644
--- a/commands/cd.go
+++ b/commands/cd.go
@@ -60,6 +60,7 @@ func (ChangeDirectory) Execute(aerc *widgets.Aerc, args []string) error {
 	}
 	if err := os.Chdir(target); err == nil {
 		previousDir = cwd
+		aerc.UpdateStatus()
 	}
 	return err
 }
diff --git a/doc/aerc-config.5.scd b/doc/aerc-config.5.scd
index 943c6c8..55f82cc 100644
--- a/doc/aerc-config.5.scd
+++ b/doc/aerc-config.5.scd
@@ -376,6 +376,8 @@ These options are configured in the *[statusline]* section of aerc.conf.
 :  active directory name
 |  %c
 :  connection state
+|  %p
+:  current path
 |  %m
 :  mute statusline and show only push notifications
 |  %S
diff --git a/lib/statusline/renderer.go b/lib/statusline/renderer.go
index 26c95c2..f128e6a 100644
--- a/lib/statusline/renderer.go
+++ b/lib/statusline/renderer.go
@@ -3,6 +3,7 @@ package statusline
 import (
 	"errors"
 	"fmt"
+	"os"
 	"strings"
 	"unicode"
 
@@ -171,6 +172,16 @@ func parseStatuslineFormat(format string, texter Texter, r renderParams) (string
 			}
 			retval = append(retval, 's')
 			args = append(args, strings.Join(tray, r.sep))
+		case 'p':
+			path, err := os.Getwd()
+			if err == nil {
+				home, _ := os.UserHomeDir()
+				if strings.HasPrefix(path, home) {
+					path = strings.Replace(path, home, "~", 1)
+				}
+				retval = append(retval, 's')
+				args = append(args, path)
+			}
 		default:
 			// Just ignore it and print as is
 			// so %k in index format becomes %%k to Printf