Skip to content

Commit

Permalink
feat: agnoster short path style
Browse files Browse the repository at this point in the history
resolves #241
  • Loading branch information
JanDeDobbeleer committed Dec 17, 2020
1 parent 746a77f commit adb205f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/docs/segment-path.md
Expand Up @@ -42,6 +42,7 @@ Style sets the way the path is displayed. Based on previous experience and popul

- agnoster
- agnoster_full
- agnoster_short
- short
- full
- folder
Expand All @@ -56,6 +57,10 @@ inside the `$HOME` location or one of its children.

Renders each folder name separated by the `folder_separator_icon`.

### Agnoster Short

When more than 1 level deep, it renders one `folder_icon` followed by the name of the current folder separated by the `folder_separator_icon`.

### Short

Display `$PWD` as a string, replace `$HOME` with the `home_icon` if you're inside the `$HOME` location or
Expand Down
17 changes: 17 additions & 0 deletions segment_path.go
Expand Up @@ -26,6 +26,8 @@ const (
Agnoster string = "agnoster"
// AgnosterFull displays all the folder names with the folder_separator_icon
AgnosterFull string = "agnoster_full"
// AgnosterShort displays the folder names with one folder_separator_icon, regardless of depth
AgnosterShort string = "agnoster_short"
// Short displays a shorter path
Short string = "short"
// Full displays the full path
Expand All @@ -46,6 +48,8 @@ func (pt *path) string() string {
return pt.getAgnosterPath()
case AgnosterFull:
return pt.getAgnosterFullPath()
case AgnosterShort:
return pt.getAgnosterShortPath()
case Short:
return pt.getShortPath()
case Full:
Expand Down Expand Up @@ -124,6 +128,19 @@ func (pt *path) getAgnosterFullPath() string {
return strings.ReplaceAll(pwd, pathSeparator, folderSeparator)
}

func (pt *path) getAgnosterShortPath() string {
pathSeparator := pt.env.getPathSeperator()
folderSeparator := pt.props.getString(FolderSeparatorIcon, pathSeparator)
folderIcon := pt.props.getString(FolderIcon, "..")
root := pt.rootLocation()
base := base(pt.env.getcwd(), pt.env)
pathDepth := pt.pathDepth(pt.getShortPath())
if pathDepth == 1 {
return fmt.Sprintf("%s%s%s", root, folderSeparator, base)
}
return fmt.Sprintf("%s%s%s%s%s", root, folderSeparator, folderIcon, folderSeparator, base)
}

func (pt *path) inHomeDir(pwd string) bool {
return strings.HasPrefix(pwd, pt.env.homeDir())
}
Expand Down
54 changes: 54 additions & 0 deletions segment_path_test.go
Expand Up @@ -394,6 +394,60 @@ func TestGetAgnosterFullPath(t *testing.T) {
assert.Equal(t, "usr > location > whatever", got)
}

func TestGetAgnosterShortPath(t *testing.T) {
pwd := "/usr/location/whatever/man"
env := new(MockedEnvironment)
env.On("getPathSeperator", nil).Return("/")
env.On("homeDir", nil).Return("/usr/home")
env.On("getcwd", nil).Return(pwd)
path := &path{
env: env,
props: &properties{
values: map[Property]interface{}{
FolderSeparatorIcon: " > ",
},
},
}
got := path.getAgnosterShortPath()
assert.Equal(t, "usr > .. > man", got)
}

func TestGetAgnosterShortPathInsideHome(t *testing.T) {
pwd := "/usr/home/whatever/man"
env := new(MockedEnvironment)
env.On("getPathSeperator", nil).Return("/")
env.On("homeDir", nil).Return("/usr/home")
env.On("getcwd", nil).Return(pwd)
path := &path{
env: env,
props: &properties{
values: map[Property]interface{}{
FolderSeparatorIcon: " > ",
},
},
}
got := path.getAgnosterShortPath()
assert.Equal(t, "~ > .. > man", got)
}

func TestGetAgnosterShortPathInsideHomeOneLevel(t *testing.T) {
pwd := "/usr/home/projects"
env := new(MockedEnvironment)
env.On("getPathSeperator", nil).Return("/")
env.On("homeDir", nil).Return("/usr/home")
env.On("getcwd", nil).Return(pwd)
path := &path{
env: env,
props: &properties{
values: map[Property]interface{}{
FolderSeparatorIcon: " > ",
},
},
}
got := path.getAgnosterShortPath()
assert.Equal(t, "~ > projects", got)
}

func testWritePathInfo(home, pwd, pathSeparator string) string {
props := &properties{
values: map[Property]interface{}{
Expand Down
1 change: 1 addition & 0 deletions themes/schema.json
Expand Up @@ -856,6 +856,7 @@
"enum": [
"agnoster",
"agnoster_full",
"agnoster_short",
"short",
"full",
"folder"
Expand Down

0 comments on commit adb205f

Please sign in to comment.