Skip to content

Commit 1dadd4c

Browse files
committed
Move StripColor() to util package.
1 parent 92ffd38 commit 1dadd4c

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

format/long.go

+2-12
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,14 @@ import (
44
"fmt"
55
"math"
66
"time"
7-
"regexp"
87
"strings"
98
"strconv"
109
"github.com/fatih/color"
10+
"github.com/drn/nerd-ls/util"
1111
"github.com/drn/nerd-ls/node"
1212
"github.com/drn/nerd-ls/humanize"
1313
)
1414

15-
const ansi = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]" +
16-
"*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=>" +
17-
"<~]))"
18-
var ansiRegex = regexp.MustCompile(ansi)
19-
2015
// Long - Format listing in long format.
2116
func Long(nodes []node.Node) {
2217
// populate values
@@ -31,7 +26,7 @@ func Long(nodes []node.Node) {
3126
for i := range values {
3227
lengths[i] = make([]int, len(values[i]))
3328
for j := range values[i] {
34-
length := len(strip(values[i][j]))
29+
length := len(util.StripColor(values[i][j]))
3530
lengths[i][j] = length
3631
if length > maxLengths[j] {
3732
maxLengths[j] = length
@@ -109,11 +104,6 @@ func formatTime(node node.Node) string {
109104
)
110105
}
111106

112-
// strips ANSI color codes from string
113-
func strip(str string) string {
114-
return ansiRegex.ReplaceAllString(str, "")
115-
}
116-
117107
func formatMode(mode string) string {
118108
runes := []rune(mode)
119109

util/root.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package util
2+
3+
import (
4+
"regexp"
5+
)
6+
7+
const ansi = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]" +
8+
"*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=>" +
9+
"<~]))"
10+
var ansiRegex = regexp.MustCompile(ansi)
11+
12+
// StripColor - strips ANSI color codes from string
13+
func StripColor(str string) string {
14+
return ansiRegex.ReplaceAllString(str, "")
15+
}

0 commit comments

Comments
 (0)