feat: agnoster short path style

resolves #241
This commit is contained in:
Jan De Dobbeleer 2020-12-17 12:10:01 +01:00 committed by Jan De Dobbeleer
parent 746a77ff02
commit adb205fe66
4 changed files with 77 additions and 0 deletions

View file

@ -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
@ -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

View file

@ -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
@ -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:
@ -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())
}

View file

@ -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{}{

View file

@ -856,6 +856,7 @@
"enum": [
"agnoster",
"agnoster_full",
"agnoster_short",
"short",
"full",
"folder"