@@ -16,6 +16,17 @@ import (
1616 "github.com/charmbracelet/x/ansi"
1717)
1818
19+ // Size calculation constants
20+ const (
21+ KilobyteSize = 1000 // SI decimal unit
22+ KibibyteSize = 1024 // Binary unit
23+ TabWidth = 4 // Standard tab expansion width
24+ DefaultBufferSize = 1024 // Default buffer size for string operations
25+ NonBreakingSpace = 0xa0 // Unicode non-breaking space
26+ EscapeChar = 0x1b // ANSI escape character
27+ ASCIIMax = 0x7f // Maximum ASCII character value
28+ )
29+
1930func TruncateText (text string , maxChars int , tails string ) string {
2031 truncatedText := ansi .Truncate (text , maxChars - len (tails ), "" )
2132 if text != truncatedText {
@@ -51,6 +62,7 @@ func TruncateMiddleText(text string, maxChars int, tails string) string {
5162 return text
5263 }
5364
65+ //nolint:mnd // standard halving for center truncation
5466 halfEllipsisLength := (maxChars - 3 ) / 2
5567 // TODO : Use ansi.Substring to correctly handle ANSI escape codes
5668 truncatedText := text [:halfEllipsisLength ] + tails + text [utf8 .RuneCountInString (text )- halfEllipsisLength :]
@@ -116,12 +128,12 @@ func FormatFileSize(size int64) string {
116128
117129 // TODO : Remove duplication here
118130 if Config .FileSizeUseSI {
119- unitIndex := int (math .Floor (math .Log (float64 (size )) / math .Log (1000 )))
120- adjustedSize := float64 (size ) / math .Pow (1000 , float64 (unitIndex ))
131+ unitIndex := int (math .Floor (math .Log (float64 (size )) / math .Log (KilobyteSize )))
132+ adjustedSize := float64 (size ) / math .Pow (KilobyteSize , float64 (unitIndex ))
121133 return fmt .Sprintf ("%.2f %s" , adjustedSize , unitsDec [unitIndex ])
122134 }
123- unitIndex := int (math .Floor (math .Log (float64 (size )) / math .Log (1024 )))
124- adjustedSize := float64 (size ) / math .Pow (1024 , float64 (unitIndex ))
135+ unitIndex := int (math .Floor (math .Log (float64 (size )) / math .Log (KibibyteSize )))
136+ adjustedSize := float64 (size ) / math .Pow (KibibyteSize , float64 (unitIndex ))
125137 return fmt .Sprintf ("%.2f %s" , adjustedSize , unitsBin [unitIndex ])
126138}
127139
@@ -132,7 +144,7 @@ func CheckAndTruncateLineLengths(text string, maxLength int) string {
132144
133145 for _ , line := range lines {
134146 // Replace tabs with spaces
135- expandedLine := strings .ReplaceAll (line , "\t " , strings .Repeat (" " , 4 ))
147+ expandedLine := strings .ReplaceAll (line , "\t " , strings .Repeat (" " , TabWidth ))
136148 truncatedLine := ansi .Truncate (expandedLine , maxLength , "" )
137149 result .WriteString (truncatedLine + "\n " )
138150 }
@@ -180,7 +192,7 @@ func IsTextFile(filename string) (bool, error) {
180192 defer file .Close ()
181193
182194 reader := bufio .NewReader (file )
183- buffer := make ([]byte , 1024 )
195+ buffer := make ([]byte , DefaultBufferSize )
184196 cnt , err := reader .Read (buffer )
185197 if err != nil && ! errors .Is (err , io .EOF ) {
186198 return false , err
@@ -204,7 +216,7 @@ func MakePrintableWithEscCheck(line string, allowEsc bool) string { //nolint: go
204216 }
205217 // It needs to be handled separately since considered a space,
206218 // It is multi-byte in UTF-8, But it has zero display width
207- if r == 0xa0 {
219+ if r == NonBreakingSpace {
208220 sb .WriteRune (r )
209221 continue
210222 }
@@ -215,13 +227,13 @@ func MakePrintableWithEscCheck(line string, allowEsc bool) string { //nolint: go
215227 sb .WriteString (" " )
216228 continue
217229 }
218- if r == 0x1b {
230+ if r == EscapeChar {
219231 if allowEsc {
220232 sb .WriteRune (r )
221233 }
222234 continue
223235 }
224- if r > 0x7f {
236+ if r > ASCIIMax {
225237 if unicode .IsSpace (r ) && utf8 .RuneLen (r ) > 1 {
226238 // See https://github.com/charmbracelet/x/issues/466
227239 // Space chacters spanning more than one bytes are not handled well by
0 commit comments