Skip to content

Commit bb9652e

Browse files
committed
feat: add path display mode
- Add PathDisplayAbsolute and PathDisplayRelative constants - Prepare foundation for relative/absolute path display options
1 parent 98fd794 commit bb9652e

4 files changed

Lines changed: 64 additions & 18 deletions

File tree

cmd/wordcounter/main.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var (
1515
exportPath string
1616
excludePattern []string
1717
withTotal bool
18+
relativePath bool
1819
)
1920

2021
// rootCmd represents the base command when called without any subcommands
@@ -58,7 +59,12 @@ func runDirCounter(dirPath string) {
5859
ignores := wcg.DiscoverIgnoreFile()
5960
ignores = append(ignores, excludePattern...)
6061

61-
counter := wcg.NewDirCounter(dirPath, ignores...)
62+
pathDisplayMode := wcg.PathDisplayAbsolute
63+
if relativePath {
64+
pathDisplayMode = wcg.PathDisplayRelative
65+
}
66+
67+
counter := wcg.NewDirCounterWithPathMode(dirPath, pathDisplayMode, ignores...)
6268
if withTotal {
6369
counter.EnableTotal()
6470
}
@@ -89,7 +95,12 @@ func runFileCounter(filePath string) {
8995
log.Fatalf("Error: File does not exist: %s", filePath)
9096
}
9197

92-
counter := wcg.NewFileCounter(filePath)
98+
pathDisplayMode := wcg.PathDisplayAbsolute
99+
if relativePath {
100+
pathDisplayMode = wcg.PathDisplayRelative
101+
}
102+
103+
counter := wcg.NewFileCounterWithPathMode(filePath, pathDisplayMode)
93104
if err := counter.Count(); err != nil {
94105
log.Fatalf("Error counting characters in file: %v", err)
95106
}
@@ -144,6 +155,7 @@ func init() {
144155
countCmd.Flags().StringVarP(&exportPath, "exportPath", "", "counter.xlsx", "export path only for csv and excel")
145156
countCmd.Flags().StringArrayVarP(&excludePattern, "exclude", "", []string{}, "you can specify multiple patterns by call multiple times")
146157
countCmd.Flags().BoolVarP(&withTotal, "total", "", false, "enable total count only work for mode=dir")
158+
countCmd.Flags().BoolVarP(&relativePath, "relative", "r", false, "show relative paths instead of absolute paths")
147159

148160
serverCmd.Flags().StringVarP(&host, "host", "", "127.0.0.1", "host")
149161
serverCmd.Flags().IntVarP(&port, "port", "p", 8080, "port")

constants.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ const (
1313
ModeFile = "file"
1414
)
1515

16+
// Path display modes
17+
const (
18+
PathDisplayAbsolute = "absolute"
19+
PathDisplayRelative = "relative"
20+
)
21+
1622
// Default values
1723
const (
1824
DefaultExportPath = "counter.xlsx"
@@ -24,11 +30,11 @@ const (
2430

2531
// Server configuration
2632
const (
27-
ServerAppName = "WordCounter"
28-
APIVersion = "v1"
29-
APIBasePath = "/" + APIVersion + "/wordcounter"
30-
PingEndpoint = APIBasePath + "/ping"
31-
CountEndpoint = APIBasePath + "/count"
33+
ServerAppName = "WordCounter"
34+
APIVersion = "v1"
35+
APIBasePath = "/" + APIVersion + "/wordcounter"
36+
PingEndpoint = APIBasePath + "/ping"
37+
CountEndpoint = APIBasePath + "/count"
3238
)
3339

3440
// File patterns

dir.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,24 @@ import (
99
)
1010

1111
type DirCounter struct {
12-
dirname string
13-
ignoreList []string
14-
fcs []*FileCounter
15-
withTotal bool
12+
dirname string
13+
ignoreList []string
14+
fcs []*FileCounter
15+
withTotal bool
16+
pathDisplayMode string
1617
}
1718

1819
func NewDirCounter(dirname string, ignores ...string) *DirCounter {
20+
return NewDirCounterWithPathMode(dirname, PathDisplayAbsolute, ignores...)
21+
}
22+
23+
func NewDirCounterWithPathMode(dirname string, pathDisplayMode string, ignores ...string) *DirCounter {
1924
return &DirCounter{
20-
ignoreList: ignores,
21-
dirname: dirname,
22-
fcs: []*FileCounter{},
23-
withTotal: false,
25+
ignoreList: ignores,
26+
dirname: dirname,
27+
fcs: []*FileCounter{},
28+
withTotal: false,
29+
pathDisplayMode: pathDisplayMode,
2430
}
2531
}
2632

@@ -108,7 +114,25 @@ func (dc *DirCounter) processFilesConcurrently(filePaths []string) error {
108114
go func() {
109115
defer wg.Done()
110116
for j := range jobs {
111-
fc := NewFileCounter(j.filePath)
117+
var originalPath string
118+
if dc.pathDisplayMode == PathDisplayRelative {
119+
// Calculate relative path from the directory being scanned
120+
relPath, err := filepath.Rel(ToAbsolutePath(dc.dirname), j.filePath)
121+
if err != nil {
122+
originalPath = filepath.Base(j.filePath) // fallback to basename
123+
} else {
124+
originalPath = relPath
125+
}
126+
} else {
127+
originalPath = j.filePath
128+
}
129+
130+
fc := &FileCounter{
131+
FileName: j.filePath,
132+
originalPath: originalPath,
133+
pathDisplayMode: dc.pathDisplayMode,
134+
tc: NewCounter(),
135+
}
112136
err := fc.Count()
113137
results <- result{index: j.index, fc: fc, err: err}
114138
}

dir_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ func TestDirCounter_GetHeaderAndRows(t *testing.T) {
174174
dc: wcg.NewDirCounter(testDir),
175175
want: []wcg.Row{
176176
{"File", "Lines", "ChineseChars", "NonChineseChars", "TotalChars"},
177+
{filepath.Join(testDir, "empty.md"), 0, 0, 0, 0},
177178
{filepath.Join(testDir, "foo.md"), 1, 12, 1, 13},
178179
{filepath.Join(testDir, "test.md"), 2, 5, 0, 5},
179180
{filepath.Join(testDir, "test.txt"), 1, 5, 14, 19},
@@ -194,7 +195,8 @@ func TestDirCounter_GetHeaderAndRows(t *testing.T) {
194195

195196
func TestDirCounter_ExportCSV(t *testing.T) {
196197
testDir := filepath.Join(wd, "testdata")
197-
expectedCSV := fmt.Sprintf("File,Lines,ChineseChars,NonChineseChars,TotalChars\n%s,1,12,1,13\n%s,2,5,0,5\n%s,1,5,14,19",
198+
expectedCSV := fmt.Sprintf("File,Lines,ChineseChars,NonChineseChars,TotalChars\n%s,0,0,0,0\n%s,1,12,1,13\n%s,2,5,0,5\n%s,1,5,14,19",
199+
filepath.Join(testDir, "empty.md"),
198200
filepath.Join(testDir, "foo.md"),
199201
filepath.Join(testDir, "test.md"),
200202
filepath.Join(testDir, "test.txt"),
@@ -227,7 +229,8 @@ func TestDirCounter_ExportCSV(t *testing.T) {
227229

228230
func TestDirCounter_ExportCSVWithFileName(t *testing.T) {
229231
testDir := filepath.Join(wd, "testdata")
230-
expectedCSV := fmt.Sprintf("File,Lines,ChineseChars,NonChineseChars,TotalChars\n%s,1,12,1,13\n%s,2,5,0,5\n%s,1,5,14,19",
232+
expectedCSV := fmt.Sprintf("File,Lines,ChineseChars,NonChineseChars,TotalChars\n%s,0,0,0,0\n%s,1,12,1,13\n%s,2,5,0,5\n%s,1,5,14,19",
233+
filepath.Join(testDir, "empty.md"),
231234
filepath.Join(testDir, "foo.md"),
232235
filepath.Join(testDir, "test.md"),
233236
filepath.Join(testDir, "test.txt"),
@@ -269,6 +272,7 @@ func TestDirCounter_ExportTable(t *testing.T) {
269272
expectedTbl := table.NewWriter()
270273
expectedTbl.AppendHeader(wcg.Row{"File", "Lines", "ChineseChars", "NonChineseChars", "TotalChars"})
271274
rows := []table.Row{
275+
{filepath.Join(testDir, "empty.md"), 0, 0, 0, 0},
272276
{filepath.Join(testDir, "foo.md"), 1, 12, 1, 13},
273277
{filepath.Join(testDir, "test.md"), 2, 5, 0, 5},
274278
{filepath.Join(testDir, "test.txt"), 1, 5, 14, 19},

0 commit comments

Comments
 (0)