Small optimizations and cleanups (#1830)

This commit is contained in:
David Pedersen authored and GitHub committed 2024-11-04 00:25:05 +11:00
1 parent f5a0e655a1
commit 0f48f36b0f
6 files changed
+33 -31

No files matched your search

+7 -6
View File
@@ -98,7 +98,7 @@ func parseEscapeSequence(s string) tcell.Style {
func applyAnsiCodes(s string, st tcell.Style) tcell.Style { func applyAnsiCodes(s string, st tcell.Style) tcell.Style {
toks := strings.Split(s, ";") toks := strings.Split(s, ";")
var nums []int nums := make([]int, 0, len(toks))
for _, tok := range toks { for _, tok := range toks {
if tok == "" { if tok == "" {
nums = append(nums, 0) nums = append(nums, 0)
@@ -115,7 +115,8 @@ func applyAnsiCodes(s string, st tcell.Style) tcell.Style {
// ECMA-48 details the standard // ECMA-48 details the standard
// TODO: should we support turning off attributes? // TODO: should we support turning off attributes?
// Probably because this is used for previewers too // Probably because this is used for previewers too
for i := 0; i < len(nums); i++ { numslen := len(nums)
for i := 0; i < numslen; i++ {
n := nums[i] n := nums[i]
switch { switch {
case n == 0: case n == 0:
@@ -143,10 +144,10 @@ func applyAnsiCodes(s string, st tcell.Style) tcell.Style {
case n >= 90 && n <= 97: case n >= 90 && n <= 97:
st = st.Foreground(tcell.PaletteColor(n - 82)) st = st.Foreground(tcell.PaletteColor(n - 82))
case n == 38: case n == 38:
if i+3 <= len(nums) && nums[i+1] == 5 { if i+3 <= numslen && nums[i+1] == 5 {
st = st.Foreground(tcell.PaletteColor(nums[i+2])) st = st.Foreground(tcell.PaletteColor(nums[i+2]))
i += 2 i += 2
} else if i+5 <= len(nums) && nums[i+1] == 2 { } else if i+5 <= numslen && nums[i+1] == 2 {
st = st.Foreground(tcell.NewRGBColor( st = st.Foreground(tcell.NewRGBColor(
int32(nums[i+2]), int32(nums[i+2]),
int32(nums[i+3]), int32(nums[i+3]),
@@ -160,10 +161,10 @@ func applyAnsiCodes(s string, st tcell.Style) tcell.Style {
case n >= 100 && n <= 107: case n >= 100 && n <= 107:
st = st.Background(tcell.PaletteColor(n - 92)) st = st.Background(tcell.PaletteColor(n - 92))
case n == 48: case n == 48:
if i+3 <= len(nums) && nums[i+1] == 5 { if i+3 <= numslen && nums[i+1] == 5 {
st = st.Background(tcell.PaletteColor(nums[i+2])) st = st.Background(tcell.PaletteColor(nums[i+2]))
i += 2 i += 2
} else if i+5 <= len(nums) && nums[i+1] == 2 { } else if i+5 <= numslen && nums[i+1] == 2 {
st = st.Background(tcell.NewRGBColor( st = st.Background(tcell.NewRGBColor(
int32(nums[i+2]), int32(nums[i+2]),
int32(nums[i+3]), int32(nums[i+3]),
+4 -3
View File
@@ -215,9 +215,9 @@ func matchExec(s string) (matches []string, longest []rune) {
} }
} }
sort.Strings(words)
if len(words) > 0 { if len(words) > 0 {
sort.Strings(words)
uniq := words[:1] uniq := words[:1]
for i := 1; i < len(words); i++ { for i := 1; i < len(words); i++ {
if words[i] != words[i-1] { if words[i] != words[i-1] {
@@ -294,7 +294,8 @@ func matchFile(s string) (matches []string, longest []rune) {
} }
func matchCmd(s string) (matches []string, longest []rune) { func matchCmd(s string) (matches []string, longest []rune) {
words := gCmdWords words := make([]string, 0, len(gCmdWords)+len(gOpts.cmds))
words = append(words, gCmdWords...)
for c := range gOpts.cmds { for c := range gOpts.cmds {
words = append(words, c) words = append(words, c)
} }
+1 -1
View File
@@ -326,7 +326,7 @@ func (e *setExpr) eval(app *app, args []string) {
gOpts.promptfmt = e.val gOpts.promptfmt = e.val
case "ratios": case "ratios":
toks := strings.Split(e.val, ":") toks := strings.Split(e.val, ":")
var rats []int rats := make([]int, 0, len(toks))
for _, s := range toks { for _, s := range toks {
n, err := strconv.Atoi(s) n, err := strconv.Atoi(s)
if err != nil { if err != nil {
+1 -3
View File
@@ -105,9 +105,7 @@ func (im *iconMap) parseEnv(env string) {
} }
func (im *iconMap) parseArray(arr []string) { func (im *iconMap) parseArray(arr []string) {
key := arr[0] key := replaceTilde(arr[0])
key = replaceTilde(key)
if filepath.IsAbs(key) { if filepath.IsAbs(key) {
key = filepath.Clean(key) key = filepath.Clean(key)
+18 -13
View File
@@ -26,7 +26,7 @@ func isRoot(name string) bool { return filepath.Dir(name) == name }
func replaceTilde(s string) string { func replaceTilde(s string) string {
if strings.HasPrefix(s, "~") { if strings.HasPrefix(s, "~") {
s = strings.Replace(s, "~", gUser.HomeDir, 1) return gUser.HomeDir + s[1:]
} }
return s return s
} }
@@ -76,7 +76,7 @@ func runeSliceWidthLastRange(rs []rune, maxWidth int) []rune {
} }
// This function is used to escape whitespaces and special characters with // This function is used to escape whitespaces and special characters with
// backlashes in a given string. // backslashes in a given string.
func escape(s string) string { func escape(s string) string {
buf := make([]rune, 0, len(s)) buf := make([]rune, 0, len(s))
for _, r := range s { for _, r := range s {
@@ -88,7 +88,7 @@ func escape(s string) string {
return string(buf) return string(buf)
} }
// This function is used to remove backlashes that are used to escape // This function is used to remove backslashes that are used to escape
// whitespaces and special characters in a given string. // whitespaces and special characters in a given string.
func unescape(s string) string { func unescape(s string) string {
esc := false esc := false
@@ -139,8 +139,7 @@ func tokenize(s string) []string {
buf = nil buf = nil
} }
} }
toks = append(toks, string(buf)) return append(toks, string(buf))
return toks
} }
// This function splits the first word of a string delimited by whitespace from // This function splits the first word of a string delimited by whitespace from
@@ -198,15 +197,16 @@ func readArrays(r io.Reader, min_cols, max_cols int) ([][]string, error) {
} }
return !squote && !dquote && unicode.IsSpace(r) return !squote && !dquote && unicode.IsSpace(r)
}) })
arrlen := len(arr)
if len(arr) < min_cols || len(arr) > max_cols { if arrlen < min_cols || arrlen > max_cols {
if min_cols == max_cols { if min_cols == max_cols {
return nil, fmt.Errorf("expected %d columns but found: %s", min_cols, s.Text()) return nil, fmt.Errorf("expected %d columns but found: %s", min_cols, s.Text())
} }
return nil, fmt.Errorf("expected %d~%d columns but found: %s", min_cols, max_cols, s.Text()) return nil, fmt.Errorf("expected %d~%d columns but found: %s", min_cols, max_cols, s.Text())
} }
for i := 0; i < len(arr); i++ { for i := 0; i < arrlen; i++ {
squote, dquote = false, false squote, dquote = false, false
buf := make([]rune, 0, len(arr[i])) buf := make([]rune, 0, len(arr[i]))
for _, r := range arr[i] { for _, r := range arr[i] {
@@ -257,7 +257,8 @@ func humanize(size int64) string {
for _, s := range suffix { for _, s := range suffix {
if curr < 10 { if curr < 10 {
return fmt.Sprintf("%.1f%s", curr-0.0499, s) return fmt.Sprintf("%.1f%s", curr-0.0499, s)
} else if curr < 1000 { }
if curr < 1000 {
return fmt.Sprintf("%d%s", int(curr), s) return fmt.Sprintf("%d%s", int(curr), s)
} }
curr /= 1000 curr /= 1000
@@ -272,22 +273,24 @@ func humanize(size int64) string {
// than 'foo10bar'. // than 'foo10bar'.
func naturalLess(s1, s2 string) bool { func naturalLess(s1, s2 string) bool {
lo1, lo2, hi1, hi2 := 0, 0, 0, 0 lo1, lo2, hi1, hi2 := 0, 0, 0, 0
s1len := len(s1)
s2len := len(s2)
for { for {
if hi1 >= len(s1) { if hi1 >= s1len {
return hi2 != len(s2) return hi2 != s2len
} }
if hi2 >= len(s2) { if hi2 >= s2len {
return false return false
} }
isDigit1 := isDigit(s1[hi1]) isDigit1 := isDigit(s1[hi1])
isDigit2 := isDigit(s2[hi2]) isDigit2 := isDigit(s2[hi2])
for lo1 = hi1; hi1 < len(s1) && isDigit(s1[hi1]) == isDigit1; hi1++ { for lo1 = hi1; hi1 < s1len && isDigit(s1[hi1]) == isDigit1; hi1++ {
} }
for lo2 = hi2; hi2 < len(s2) && isDigit(s2[hi2]) == isDigit2; hi2++ { for lo2 = hi2; hi2 < s2len && isDigit(s2[hi2]) == isDigit2; hi2++ {
} }
if s1[lo1:hi1] == s2[lo2:hi2] { if s1[lo1:hi1] == s2[lo2:hi2] {
@@ -332,6 +335,7 @@ var (
reWordEnd = regexp.MustCompile(`(\pL|\pN)([^\pL\pN]|$)`) reWordEnd = regexp.MustCompile(`(\pL|\pN)([^\pL\pN]|$)`)
) )
// TODO: Remove when bumping to Go 1.21+
func min(a, b int) int { func min(a, b int) int {
if a < b { if a < b {
return a return a
@@ -339,6 +343,7 @@ func min(a, b int) int {
return b return b
} }
// TODO: Remove when bumping to Go 1.21+
func max(a, b int) int { func max(a, b int) int {
if a > b { if a > b {
return a return a
+2 -5
View File
@@ -324,9 +324,6 @@ func (p *parser) parseExpr() expr {
} }
func (p *parser) parse() bool { func (p *parser) parse() bool {
if p.expr = p.parseExpr(); p.expr == nil { p.expr = p.parseExpr()
return false return p.expr != nil
}
return true
} }