Skip to content

Commit

Permalink
improve shorten logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Jan 8, 2025
1 parent 922d958 commit 14051a5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
11 changes: 9 additions & 2 deletions cmd/slackdump/internal/ui/bubbles/filemgr/filemgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ func (s specialDir) Sys() interface{} {
// shorten returns a shortened version of a path.
func (m Model) shorten(dirpath string) string {
dirpath = filepath.Clean(dirpath)
if len(dirpath) < Width {
if len(dirpath) < Width-1 {
return dirpath
}
dirpath = filepath.Clean(dirpath)
Expand All @@ -454,5 +454,12 @@ func (m Model) shorten(dirpath string) string {
s = append(s, string(parts[i][0]))
}
s = append(s, parts[len(parts)-1])
return filepath.Join(s...)
res := filepath.Join(s...)
if dirpath[0] == '/' {
res = "/" + res
}
if len(res) > Width-1 {
res = "…" + res[len(res)-Width+3:]
}
return res
}
7 changes: 7 additions & 0 deletions cmd/slackdump/internal/ui/bubbles/filemgr/filemgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,13 @@ func TestModel_shorten(t *testing.T) {
},
want: "/h/u/D/F/L/P/L/T/4/Characters",
},
{
name: "really long path",
args: args{
dirpath: "/home/user/Downloads/Funky/Long/Path/Longer/Than/40/Characters/And/Even/Longer/Than/That/And/Then/Some/More/And/Even/Longer/Than/That/And/Then/Some",
},
want: "…/A/E/L/T/T/A/T/S/M/A/E/L/T/T/A/T/Some",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion internal/chunk/dirproc/filetracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func Test_filetracker_create(t *testing.T) {
t.Parallel()
cd := makeTestDir(t)
tr := newFileTracker(cd)
defer tr.CloseAll()
id := chunk.FileID("test")
err := tr.create(id)
if err != nil {
Expand All @@ -43,6 +44,7 @@ func Test_filetracker_create(t *testing.T) {
// creating initial file.
cd := makeTestDir(t)
tr := newFileTracker(cd)
defer tr.CloseAll()
id := chunk.FileID("test")
if err := tr.create(id); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -162,7 +164,7 @@ func Test_filetracker_RefCount(t *testing.T) {
cd := makeTestDir(t)
tr := newFileTracker(cd)
id := chunk.FileID("test")
r, err := tr.Recorder(id);
r, err := tr.Recorder(id)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 14051a5

Please sign in to comment.