Skip to content

Commit

Permalink
fix: implement correct FilenameOverflow calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
70sh1 committed May 24, 2024
1 parent 3fa200f commit 6ff76c1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ retract v1.0.0
require (
github.com/cheggaaa/pb/v3 v3.1.5
github.com/fatih/color v1.17.0
github.com/rivo/uniseg v0.4.7
github.com/stretchr/testify v1.9.0
github.com/urfave/cli/v2 v2.27.2
golang.org/x/crypto v0.23.0
Expand All @@ -21,7 +22,6 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
golang.org/x/sys v0.20.0 // indirect
Expand Down
15 changes: 11 additions & 4 deletions pathutils/pathutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"os"
"path/filepath"

"github.com/rivo/uniseg"
)

// Checks whenever given slice of strings contains duplicates.
Expand Down Expand Up @@ -34,14 +36,19 @@ func hasDuplicateFilenames(s []string) bool {
}

func FilenameOverflow(s string, n int) string {
r := []rune(s)
if len(r) < n {
charCount := uniseg.GraphemeClusterCount(s)
if charCount < n {
return s
}
return string(r[:n]) + "..."
gr := uniseg.NewGraphemes(s)
for range n {
gr.Next()
}
_, to := gr.Positions()
return s[:to] + "..."
}

// Cleans given paths and ouputDir and checks for duplicates.
// Cleans given paths and ouputDir (which is also assumed to be a path) and checks for duplicates.
// Also checks for duplicate filenames if outputDir is not empty.
// Returns cleaned paths or error if any of the checks failed.
func CleanAndCheckPaths(paths []string, outputDir string) ([]string, string, error) {
Expand Down

0 comments on commit 6ff76c1

Please sign in to comment.