Skip to content

Commit 91c1880

Browse files
committed
fix: PR comments: Add unit tests for icon_utils
1 parent e654b25 commit 91c1880

File tree

8 files changed

+107
-22
lines changed

8 files changed

+107
-22
lines changed

src/internal/common/icon_utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77
"github.com/yorukot/superfile/src/config/icon"
88
)
99

10-
func GetElementIcon(file string, isDir bool) icon.Style {
10+
func GetElementIcon(file string, isDir bool, nerdFont bool) icon.Style {
1111
ext := strings.TrimPrefix(filepath.Ext(file), ".")
1212
name := file
1313

14-
if !Config.Nerdfont {
14+
if !nerdFont {
1515
return icon.Style{
1616
Icon: "",
1717
Color: Theme.FilePanelFG,
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package common
2+
3+
import (
4+
"testing"
5+
6+
"github.com/yorukot/superfile/src/config/icon"
7+
)
8+
9+
func TestGetElementIcon(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
file string
13+
isDir bool
14+
nerdFont bool
15+
expected icon.Style
16+
}{
17+
{
18+
name: "Non-nerdfont returns empty icon",
19+
file: "test.txt",
20+
isDir: false,
21+
nerdFont: false,
22+
expected: icon.Style{
23+
Icon: "",
24+
Color: Theme.FilePanelFG,
25+
},
26+
},
27+
{
28+
name: "Directory with nerd font",
29+
file: "folder",
30+
isDir: true,
31+
nerdFont: true,
32+
expected: icon.Folders["folder"],
33+
},
34+
{
35+
name: "File with known extension",
36+
file: "test.go",
37+
isDir: false,
38+
nerdFont: true,
39+
expected: icon.Icons["go"],
40+
},
41+
{
42+
name: ".git directory",
43+
file: ".git",
44+
isDir: true,
45+
nerdFont: true,
46+
expected: icon.Folders[".git"],
47+
},
48+
{
49+
name: "superfile directory",
50+
file: "superfile",
51+
isDir: true,
52+
nerdFont: true,
53+
expected: icon.Folders["superfile"],
54+
},
55+
{
56+
name: "package.json file",
57+
file: "package.json",
58+
isDir: false,
59+
nerdFont: true,
60+
expected: icon.Icons["package"],
61+
},
62+
{
63+
name: "File with unknown extension",
64+
file: "test.xyz",
65+
isDir: false,
66+
nerdFont: true,
67+
expected: icon.Style{
68+
Icon: icon.Icons["file"].Icon,
69+
// Theme is not defined here, so this will be blank
70+
Color: Theme.FilePanelFG,
71+
},
72+
},
73+
}
74+
75+
for _, tt := range tests {
76+
t.Run(tt.name, func(t *testing.T) {
77+
result := GetElementIcon(tt.file, tt.isDir, tt.nerdFont)
78+
if result.Icon != tt.expected.Icon || result.Color != tt.expected.Color {
79+
t.Errorf("GetElementIcon() = %v, want %v", result, tt.expected)
80+
}
81+
})
82+
}
83+
}

src/internal/common/string_function.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ import (
1515
"github.com/charmbracelet/x/exp/term/ansi"
1616
)
1717

18-
func TruncateText(text string, maxChars int, talis string) string {
19-
truncatedText := ansi.Truncate(text, maxChars-len(talis), "")
18+
func TruncateText(text string, maxChars int, tails string) string {
19+
truncatedText := ansi.Truncate(text, maxChars-len(tails), "")
2020
if text != truncatedText {
21-
return truncatedText + talis
21+
return truncatedText + tails
2222
}
2323

2424
return text
2525
}
2626

27-
func TruncateTextBeginning(text string, maxChars int, talis string) string {
27+
func TruncateTextBeginning(text string, maxChars int, tails string) string {
2828
if ansi.StringWidth(text) <= maxChars {
2929
return text
3030
}
@@ -38,27 +38,27 @@ func TruncateTextBeginning(text string, maxChars int, talis string) string {
3838
truncatedWidth = ansi.StringWidth(string(truncatedRunes))
3939
}
4040

41-
if len(truncatedRunes) > len(talis) {
42-
truncatedRunes = append([]rune(talis), truncatedRunes[len(talis):]...)
41+
if len(truncatedRunes) > len(tails) {
42+
truncatedRunes = append([]rune(tails), truncatedRunes[len(tails):]...)
4343
}
4444

4545
return string(truncatedRunes)
4646
}
4747

48-
func TruncateMiddleText(text string, maxChars int, talis string) string {
48+
func TruncateMiddleText(text string, maxChars int, tails string) string {
4949
if utf8.RuneCountInString(text) <= maxChars {
5050
return text
5151
}
5252

5353
halfEllipsisLength := (maxChars - 3) / 2
54-
55-
truncatedText := text[:halfEllipsisLength] + talis + text[utf8.RuneCountInString(text)-halfEllipsisLength:]
54+
// Todo : Use ansi.Substring to correctly handle ANSI escape codes
55+
truncatedText := text[:halfEllipsisLength] + tails + text[utf8.RuneCountInString(text)-halfEllipsisLength:]
5656

5757
return truncatedText
5858
}
5959

6060
func PrettierName(name string, width int, isDir bool, isSelected bool, bgColor lipgloss.Color) string {
61-
style := GetElementIcon(name, isDir)
61+
style := GetElementIcon(name, isDir, Config.Nerdfont)
6262
if isSelected {
6363
return StringColorRender(lipgloss.Color(style.Color), bgColor).
6464
Background(bgColor).
@@ -73,15 +73,15 @@ func PrettierName(name string, width int, isDir bool, isSelected bool, bgColor l
7373
}
7474

7575
func PrettierDirectoryPreviewName(name string, isDir bool, bgColor lipgloss.Color) string {
76-
style := GetElementIcon(name, isDir)
76+
style := GetElementIcon(name, isDir, Config.Nerdfont)
7777
return StringColorRender(lipgloss.Color(style.Color), bgColor).
7878
Background(bgColor).
7979
Render(style.Icon+" ") +
8080
FilePanelStyle.Render(name)
8181
}
8282

8383
func ClipboardPrettierName(name string, width int, isDir bool, isSelected bool) string {
84-
style := GetElementIcon(name, isDir)
84+
style := GetElementIcon(name, isDir, Config.Nerdfont)
8585
if isSelected {
8686
return StringColorRender(lipgloss.Color(style.Color), FooterBGColor).
8787
Background(FooterBGColor).
@@ -152,8 +152,8 @@ func IsBufferPrintable(buffer []byte) bool {
152152
return true
153153
}
154154

155-
// IsExensionExtractable checks if a string is a valid compressed archive file extension.
156-
func IsExensionExtractable(ext string) bool {
155+
// IsExtensionExtractable checks if a string is a valid compressed archive file extension.
156+
func IsExtensionExtractable(ext string) bool {
157157
// Extensions based on the types that package: `xtractr` `ExtractFile` function handles.
158158
validExtensions := map[string]struct{}{
159159
".zip": {},

src/internal/common/string_function_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func TestIsExtensionExtractable(t *testing.T) {
109109

110110
for _, tt := range inputs {
111111
t.Run(tt.ext, func(t *testing.T) {
112-
result := IsExensionExtractable(tt.ext)
112+
result := IsExtensionExtractable(tt.ext)
113113
if result != tt.expected {
114114
t.Errorf("IsExensionExtractable (%q) = %v; want %v", tt.ext, result, tt.expected)
115115
}

src/internal/function.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ func (m *model) returnMetaData() {
325325
if fileInfo.IsDir() {
326326
m.fileMetaData.metaData = append(m.fileMetaData.metaData, [2]string{"Name", fileInfo.Name()})
327327
if m.focusPanel == metadataFocus {
328+
// Todo : Calling dirSize() could be expensive for large directories, as it recursively
329+
// walks the entire tree. Consider lazy loading, caching, or an async approach to avoid UI lockups.
328330
m.fileMetaData.metaData = append(m.fileMetaData.metaData, [2]string{"Size", common.FormatFileSize(dirSize(filePath))})
329331
}
330332
m.fileMetaData.metaData = append(m.fileMetaData.metaData, [2]string{"Date Modified", fileInfo.ModTime().String()})

src/internal/handle_file_operations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ func (m *model) extractFile() {
502502
}
503503

504504
ext := strings.ToLower(filepath.Ext(panel.element[panel.cursor].location))
505-
if !common.IsExensionExtractable(ext) {
505+
if !common.IsExtensionExtractable(ext) {
506506
slog.Error(fmt.Sprintf("Error unexpected file extension type: %s", ext), "error", errors.ErrUnsupported)
507507
return
508508
}

src/internal/ui/sidebar/ReadMe.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ This is for the sidebar UI, and for fetching and updating sidebar directories
88
- Implementing a proper state transitioning for the sidebar's different modes (normal, search, rename)
99
- Some methods could be made more pure by reducing side effects
1010

11-
# Coverage
1211
# Coverage
1312

1413
```bash
15-
cd /path/to/ui/prompt
14+
cd /path/to/ui/sidebar
1615
go test -cover
1716
```
18-
Current coverage is 74.0%.
19-
Current coverage
17+
Current coverage is 29.3%.

src/internal/utils/fzf_utils.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import "github.com/reinhrst/fzf-lib"
66
func FzfSearch(query string, source []string) []fzf.MatchResult {
77
fzfSearcher := fzf.New(source, fzf.DefaultOptions())
88
fzfSearcher.Search(query)
9+
// Todo : This is a blocking call, which will cause the UI to freeze if the query is slow.
10+
// Need to put a timeout on this
911
fzfResults := <-fzfSearcher.GetResultChannel()
1012
fzfSearcher.End()
1113
return fzfResults.Matches

0 commit comments

Comments
 (0)