From 6ff76c12bb575e7cd80b83772902c31ec95c4f58 Mon Sep 17 00:00:00 2001 From: 70sh1 <70sh1@proton.me> Date: Fri, 24 May 2024 05:41:53 +0300 Subject: [PATCH] fix: implement correct FilenameOverflow calculation --- go.mod | 2 +- pathutils/pathutils.go | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 26173b1..36e0e7d 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/pathutils/pathutils.go b/pathutils/pathutils.go index 1af23f6..8eb4b9d 100644 --- a/pathutils/pathutils.go +++ b/pathutils/pathutils.go @@ -5,6 +5,8 @@ import ( "fmt" "os" "path/filepath" + + "github.com/rivo/uniseg" ) // Checks whenever given slice of strings contains duplicates. @@ -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) {