From d52f917782ea6a73cf045643deea21fa343a1fde Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sat, 11 Dec 2021 20:37:14 +0100 Subject: [PATCH] feat(path): agnoster left resolves #1411 --- docs/docs/segment-path.md | 9 +++++-- src/segment_path.go | 27 +++++++++++++++++++++ src/segment_path_test.go | 50 ++++++++++++++++++++++++++++++++++++++- themes/schema.json | 1 + 4 files changed, 84 insertions(+), 3 deletions(-) diff --git a/docs/docs/segment-path.md b/docs/docs/segment-path.md index 5b3faa36..bd172016 100644 --- a/docs/docs/segment-path.md +++ b/docs/docs/segment-path.md @@ -76,6 +76,7 @@ Style sets the way the path is displayed. Based on previous experience and popul - agnoster - agnoster_full - agnoster_short +- agnoster_left - full - folder - mixed @@ -84,8 +85,7 @@ Style sets the way the path is displayed. Based on previous experience and popul ### Agnoster Renders each folder as the `folder_icon` separated by the `folder_separator_icon`. -Only the current folder name is displayed at the end, `$HOME` is replaced by the `home_icon` if you're -inside the `$HOME` location or one of its children. +Only the current folder name is displayed at the end. ### Agnoster Full @@ -96,6 +96,11 @@ Renders each folder name separated by the `folder_separator_icon`. When more than `max_depth` levels deep, it renders one `folder_icon` followed by the names of the last `max_depth` folders, separated by the `folder_separator_icon`. +### Agnoster Left + +Renders each folder as the `folder_icon` separated by the `folder_separator_icon`. +Only the root folder name and it's child are displayed in full. + ### Full Display `$PWD` as a string. diff --git a/src/segment_path.go b/src/segment_path.go index 9a848c46..7e68c81b 100644 --- a/src/segment_path.go +++ b/src/segment_path.go @@ -37,6 +37,8 @@ const ( Mixed string = "mixed" // Letter like agnoster, but with the first letter of each folder name Letter string = "letter" + // AgnosterLeft like agnoster, but keeps the left side of the path + AgnosterLeft string = "agnoster_left" // MixedThreshold the threshold of the length of the path Mixed will display MixedThreshold Property = "mixed_threshold" // MappedLocations allows overriding certain location with an icon @@ -67,6 +69,8 @@ func (pt *path) string() string { formattedPath = pt.getMixedPath() case Letter: formattedPath = pt.getLetterPath() + case AgnosterLeft: + formattedPath = pt.getAgnosterLeftPath() case Short: // "short" is a duplicate of "full", just here for backwards compatibility fallthrough @@ -145,6 +149,29 @@ func (pt *path) getAgnosterPath() string { return buffer.String() } +func (pt *path) getAgnosterLeftPath() string { + pwd := pt.getPwd() + separator := pt.env.getPathSeperator() + pwd = strings.Trim(pwd, separator) + splitted := strings.Split(pwd, separator) + folderIcon := pt.props.getString(FolderIcon, "..") + separator = pt.props.getString(FolderSeparatorIcon, separator) + switch len(splitted) { + case 0: + return "" + case 1: + return splitted[0] + case 2: + return fmt.Sprintf("%s%s%s", splitted[0], separator, splitted[1]) + } + var buffer strings.Builder + buffer.WriteString(fmt.Sprintf("%s%s%s", splitted[0], separator, splitted[1])) + for i := 2; i < len(splitted); i++ { + buffer.WriteString(fmt.Sprintf("%s%s", separator, folderIcon)) + } + return buffer.String() +} + func (pt *path) getLetterPath() string { var buffer strings.Builder pwd := pt.getPwd() diff --git a/src/segment_path_test.go b/src/segment_path_test.go index f4c263d3..6b0af224 100644 --- a/src/segment_path_test.go +++ b/src/segment_path_test.go @@ -617,7 +617,7 @@ func TestGetFolderPathCustomMappedLocations(t *testing.T) { assert.Equal(t, "#", got) } -func TestAgnosterPath(t *testing.T) { +func TestAgnosterPath(t *testing.T) { // nolint:dupl cases := []struct { Case string Expected string @@ -665,6 +665,54 @@ func TestAgnosterPath(t *testing.T) { } } +func TestAgnosterLeftPath(t *testing.T) { // nolint:dupl + cases := []struct { + Case string + Expected string + Home string + PWD string + PathSeparator string + }{ + {Case: "Windows outside home", Expected: "C: > Program Files > f > f", Home: homeBillWindows, PWD: "C:\\Program Files\\Go\\location", PathSeparator: "\\"}, + {Case: "Windows inside home", Expected: "~ > Documents > f > f", Home: homeBillWindows, PWD: homeBillWindows + "\\Documents\\Bill\\location", PathSeparator: "\\"}, + {Case: "Windows inside home zero levels", Expected: "C: > location", Home: homeBillWindows, PWD: "C:\\location", PathSeparator: "\\"}, + {Case: "Windows inside home one level", Expected: "C: > Program Files > f", Home: homeBillWindows, PWD: "C:\\Program Files\\location", PathSeparator: "\\"}, + {Case: "Windows lower case drive letter", Expected: "C: > Windows", Home: homeBillWindows, PWD: "C:\\Windows\\", PathSeparator: "\\"}, + {Case: "Windows lower case drive letter (other)", Expected: "P: > Other", Home: homeBillWindows, PWD: "P:\\Other\\", PathSeparator: "\\"}, + {Case: "Windows lower word drive", Expected: "some: > some", Home: homeBillWindows, PWD: "some:\\some\\", PathSeparator: "\\"}, + {Case: "Windows lower word drive (ending with c)", Expected: "src: > source", Home: homeBillWindows, PWD: "src:\\source\\", PathSeparator: "\\"}, + {Case: "Windows lower word drive (arbitrary cases)", Expected: "sRc: > source", Home: homeBillWindows, PWD: "sRc:\\source\\", PathSeparator: "\\"}, + {Case: "Windows registry drive", Expected: "\uf013 > SOFTWARE > f", Home: homeBillWindows, PWD: "HKLM:\\SOFTWARE\\magnetic:test\\", PathSeparator: "\\"}, + {Case: "Windows registry drive case sensitive", Expected: "\uf013 > SOFTWARE > f", Home: homeBillWindows, PWD: "HKLM:\\SOFTWARE\\magnetic:TOAST\\", PathSeparator: "\\"}, + {Case: "Unix outside home", Expected: "mnt > go > f > f", Home: homeJan, PWD: "/mnt/go/test/location", PathSeparator: "/"}, + {Case: "Unix inside home", Expected: "~ > docs > f > f", Home: homeJan, PWD: homeJan + "/docs/jan/location", PathSeparator: "/"}, + {Case: "Unix outside home zero levels", Expected: "mnt > location", Home: homeJan, PWD: "/mnt/location", PathSeparator: "/"}, + {Case: "Unix outside home one level", Expected: "mnt > folder > f", Home: homeJan, PWD: "/mnt/folder/location", PathSeparator: "/"}, + } + + for _, tc := range cases { + env := new(MockedEnvironment) + env.On("homeDir", nil).Return(tc.Home) + env.On("getPathSeperator", nil).Return(tc.PathSeparator) + env.On("getcwd", nil).Return(tc.PWD) + env.On("getRuntimeGOOS", nil).Return("") + args := &args{ + PSWD: &tc.PWD, + } + env.On("getArgs", nil).Return(args) + path := &path{ + env: env, + props: map[Property]interface{}{ + FolderSeparatorIcon: " > ", + FolderIcon: "f", + HomeIcon: "~", + }, + } + got := path.getAgnosterLeftPath() + assert.Equal(t, tc.Expected, got, tc.Case) + } +} + func TestGetPwd(t *testing.T) { cases := []struct { MappedLocationsEnabled bool diff --git a/themes/schema.json b/themes/schema.json index bc08243d..56bcfa7f 100644 --- a/themes/schema.json +++ b/themes/schema.json @@ -1191,6 +1191,7 @@ "agnoster", "agnoster_full", "agnoster_short", + "agnoster_left", "short", "full", "folder",