From 2a3292560d5963192c502dd2d6129340d2591590 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Sun, 23 Feb 2025 13:05:20 +0700 Subject: [PATCH] Apply modernization fixes (#1921) --- complete.go | 4 ++-- eval.go | 16 ++++++++-------- main.go | 4 ++-- misc.go | 4 ++-- nav.go | 17 ++++++++--------- server.go | 2 +- sixel.go | 2 +- ui.go | 16 ++++++++-------- 8 files changed, 32 insertions(+), 33 deletions(-) diff --git a/complete.go b/complete.go index 1e8a4a17..a4af8b28 100644 --- a/complete.go +++ b/complete.go @@ -116,7 +116,7 @@ var ( func getOptWords(opts any) (optWords []string) { t := reflect.TypeOf(opts) - for i := 0; i < t.NumField(); i++ { + for i := range t.NumField() { field := t.Field(i) switch field.Type.Kind() { case reflect.Map: @@ -134,7 +134,7 @@ func getOptWords(opts any) (optWords []string) { func getLocalOptWords(localOpts any) (localOptWords []string) { t := reflect.TypeOf(localOpts) - for i := 0; i < t.NumField(); i++ { + for i := range t.NumField() { field := t.Field(i) name := field.Name if field.Type.Kind() != reflect.Map { diff --git a/eval.go b/eval.go index b266975b..a1ebd495 100644 --- a/eval.go +++ b/eval.go @@ -1075,7 +1075,7 @@ func (e *callExpr) eval(app *app, args []string) { } resetIncCmd(app) preChdir(app) - for i := 0; i < e.count; i++ { + for range e.count { if err := app.nav.updir(); err != nil { app.ui.echoerrf("%s", err) return @@ -1124,7 +1124,7 @@ func (e *callExpr) eval(app *app, args []string) { case "jump-prev": resetIncCmd(app) preChdir(app) - for i := 0; i < e.count; i++ { + for range e.count { app.nav.cdJumpListPrev() } app.ui.loadFile(app, true) @@ -1134,7 +1134,7 @@ func (e *callExpr) eval(app *app, args []string) { case "jump-next": resetIncCmd(app) preChdir(app) - for i := 0; i < e.count; i++ { + for range e.count { app.nav.cdJumpListNext() } app.ui.loadFile(app, true) @@ -1499,7 +1499,7 @@ func (e *callExpr) eval(app *app, args []string) { } dir := app.nav.currDir() old := dir.ind - for i := 0; i < e.count; i++ { + for range e.count { if app.nav.findBack { app.nav.findPrev() } else { @@ -1516,7 +1516,7 @@ func (e *callExpr) eval(app *app, args []string) { } dir := app.nav.currDir() old := dir.ind - for i := 0; i < e.count; i++ { + for range e.count { if app.nav.findBack { app.nav.findNext() } else { @@ -1559,7 +1559,7 @@ func (e *callExpr) eval(app *app, args []string) { if !app.nav.init { return } - for i := 0; i < e.count; i++ { + for range e.count { if app.nav.searchBack { if moved, err := app.nav.searchPrev(); err != nil { app.ui.echoerrf("search-back: %s: %s", err, app.nav.search) @@ -1580,7 +1580,7 @@ func (e *callExpr) eval(app *app, args []string) { if !app.nav.init { return } - for i := 0; i < e.count; i++ { + for range e.count { if app.nav.searchBack { if moved, err := app.nav.searchNext(); err != nil { app.ui.echoerrf("search-back: %s: %s", err, app.nav.search) @@ -2254,7 +2254,7 @@ func (e *execExpr) eval(app *app, args []string) { } func (e *listExpr) eval(app *app, args []string) { - for i := 0; i < e.count; i++ { + for range e.count { for _, expr := range e.exprs { expr.eval(app, nil) } diff --git a/main.go b/main.go index 985d842b..febd5350 100644 --- a/main.go +++ b/main.go @@ -107,7 +107,7 @@ func fieldToString(field reflect.Value) string { case reflect.Bool: value = strconv.FormatBool(field.Bool()) case reflect.Slice: - for i := 0; i < field.Len(); i++ { + for i := range field.Len() { element := field.Index(i) if i == 0 { @@ -128,7 +128,7 @@ func getOptsMap() map[string]string { v := reflect.ValueOf(gOpts) t := v.Type() - for i := 0; i < v.NumField(); i++ { + for i := range v.NumField() { // Get field name and prefix it with lf_ name := "lf_" + t.Field(i).Name diff --git a/misc.go b/misc.go index be6e2e4a..7dcc74c8 100644 --- a/misc.go +++ b/misc.go @@ -170,7 +170,7 @@ func readArrays(r io.Reader, min_cols, max_cols int) ([][]string, error) { line := s.Text() squote, dquote := false, false - for i := 0; i < len(line); i++ { + for i := range len(line) { if line[i] == '\'' && !dquote { squote = !squote } else if line[i] == '"' && !squote { @@ -206,7 +206,7 @@ func readArrays(r io.Reader, min_cols, max_cols int) ([][]string, error) { return nil, fmt.Errorf("expected %d~%d columns but found: %s", min_cols, max_cols, s.Text()) } - for i := 0; i < arrlen; i++ { + for i := range arrlen { squote, dquote = false, false buf := make([]rune, 0, len(arr[i])) for _, r := range arr[i] { diff --git a/nav.go b/nav.go index 08f1f9ae..31b45484 100644 --- a/nav.go +++ b/nav.go @@ -17,6 +17,7 @@ import ( "github.com/djherbis/times" "golang.org/x/text/collate" + "slices" ) type linkState byte @@ -915,11 +916,9 @@ func (nav *nav) preview(path string, win *win) { break } - for _, byte := range bytes { - if byte == 0 { - reg.lines = []string{"\033[7mbinary\033[0m"} - return - } + if slices.Contains(bytes, 0) { + reg.lines = []string{"\033[7mbinary\033[0m"} + return } line = append(line, bytes...) @@ -1674,7 +1673,7 @@ func (nav *nav) globSel(pattern string, invert bool) error { dir := nav.currDir() anyMatched := false - for i := 0; i < len(dir.files); i++ { + for i := range dir.files { matched, err := filepath.Match(pattern, dir.files[i].Name()) if err != nil { return fmt.Errorf("glob-select: %s", err) @@ -1720,7 +1719,7 @@ func (nav *nav) findSingle() int { count := 0 index := 0 dir := nav.currDir() - for i := 0; i < len(dir.files); i++ { + for i := range dir.files { if findMatch(dir.files[i].Name(), nav.find) { count++ if count > 1 { @@ -1747,7 +1746,7 @@ func (nav *nav) findNext() (bool, bool) { } } if gOpts.wrapscan { - for i := 0; i < dir.ind; i++ { + for i := range dir.ind { if findMatch(dir.files[i].Name(), nav.find) { return nav.up(dir.ind - i), true } @@ -1804,7 +1803,7 @@ func (nav *nav) searchNext() (bool, error) { } } if gOpts.wrapscan { - for i := 0; i < dir.ind; i++ { + for i := range dir.ind { if matched, err := searchMatch(dir.files[i].Name(), nav.search, gOpts.globsearch); err != nil { return false, err } else if matched { diff --git a/server.go b/server.go index 7bb6e73f..6a9d3ae9 100644 --- a/server.go +++ b/server.go @@ -64,7 +64,7 @@ func echoerr(c net.Conn, msg string) { log.Print(msg) } -func echoerrf(c net.Conn, format string, a ...interface{}) { +func echoerrf(c net.Conn, format string, a ...any) { echoerr(c, fmt.Sprintf(format, a...)) } diff --git a/sixel.go b/sixel.go index 36ca8c88..d559cccd 100644 --- a/sixel.go +++ b/sixel.go @@ -60,7 +60,7 @@ func (sxs *sixelScreen) printSixel(win *win, screen tcell.Screen, reg *reg) { // HACK: fillers are used to control when tcell redraws the region where a sixel image is drawn. // alternating between bold and regular is to clear the image before drawing a new one. st := sxs.fillerStyle(reg.path) - for y := 0; y < win.h; y++ { + for y := range win.h { st = win.print(screen, 0, y, st, strings.Repeat(string(gSixelFiller), win.w)) } diff --git a/ui.go b/ui.go index 0d678583..54dd1037 100644 --- a/ui.go +++ b/ui.go @@ -242,7 +242,7 @@ func (win *win) print(screen tcell.Screen, x, y int, st tcell.Style, s string) t return st } -func (win *win) printf(screen tcell.Screen, x, y int, st tcell.Style, format string, a ...interface{}) { +func (win *win) printf(screen tcell.Screen, x, y int, st tcell.Style, format string, a ...any) { win.print(screen, x, y, st, fmt.Sprintf(format, a...)) } @@ -583,7 +583,7 @@ func getWidths(wtot int) []int { } wsum := 0 - for i := 0; i < wlen-1; i++ { + for i := range wlen - 1 { widths[i] = gOpts.ratios[i] * wtot / rsum wsum += widths[i] } @@ -600,7 +600,7 @@ func getWins(screen tcell.Screen) []*win { wacc := 0 wlen := len(widths) wins := make([]*win, 0, wlen) - for i := 0; i < wlen; i++ { + for i := range wlen { if gOpts.drawbox { wacc++ wins = append(wins, newWin(widths[i], htot-4, wacc, 2)) @@ -731,7 +731,7 @@ func (ui *ui) echoerr(msg string) { log.Printf("error: %s", msg) } -func (ui *ui) echoerrf(format string, a ...interface{}) { +func (ui *ui) echoerrf(format string, a ...any) { ui.echoerr(fmt.Sprintf(format, a...)) } @@ -1004,7 +1004,7 @@ func (ui *ui) drawBox() { } wacc := 0 - for wind := 0; wind < len(ui.wins)-1; wind++ { + for wind := range len(ui.wins) - 1 { wacc += ui.wins[wind].w + 1 ui.screen.SetContent(wacc, 1, tcell.RuneTTee, nil, st) for i := 2; i < h-2; i++ { @@ -1032,8 +1032,8 @@ func (ui *ui) draw(nav *nav) { // XXX: manual clean without flush to avoid flicker on Windows wtot, htot := ui.screen.Size() - for i := 0; i < wtot; i++ { - for j := 0; j < htot; j++ { + for i := range wtot { + for j := range htot { ui.screen.SetContent(i, j, ' ', nil, st) } } @@ -1045,7 +1045,7 @@ func (ui *ui) draw(nav *nav) { if gOpts.preview { wins-- } - for i := 0; i < wins; i++ { + for i := range wins { role := Parent if i == wins-1 { role = Active