From 3c9bf857e2899bddf807c7597f1905976e5d8390 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Sun, 5 Mar 2023 13:56:38 -0800 Subject: [PATCH] xmain: Add AbsPath and HumanPath --- xmain/xmain.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/xmain/xmain.go b/xmain/xmain.go index 783bb8b..08f5368 100644 --- a/xmain/xmain.go +++ b/xmain/xmain.go @@ -10,6 +10,7 @@ import ( "os" "os/signal" "path/filepath" + "strings" "syscall" "time" @@ -181,8 +182,26 @@ func (ms *State) WritePath(fp string, p []byte) error { return os.WriteFile(fp, p, 0644) } -func (ms *State) RelPath(fp string) string { - rel, err := filepath.Rel(ms.PWD, fp) +// AbsPath joins the PWD with fp to give the absolute path to fp. +func (ms *State) AbsPath(fp string) string { + if filepath.IsAbs(fp) { + return fp + } + return filepath.Join(ms.PWD, fp) +} + +// HumanPath makes absolute path fp more suitable for human consumption +// by replacing $HOME in fp with ~ and making it relative to the current PWD. +func (ms *State) HumanPath(fp string) string { + if strings.HasPrefix(fp, os.Getenv("HOME")) { + fp = filepath.Join("~", strings.TrimPrefix(fp, os.Getenv("HOME"))) + } + pwd := ms.PWD + if strings.HasPrefix(pwd, os.Getenv("HOME")) { + pwd = filepath.Join("~", strings.TrimPrefix(pwd, os.Getenv("HOME"))) + } + + rel, err := filepath.Rel(pwd, fp) if err != nil { return fp }