1
1
mirror of https://github.com/walles/moar.git synced 2024-07-07 08:36:29 +03:00

Enable revive for unused parameters check

This commit is contained in:
Johan Walles 2024-01-01 13:30:25 +01:00
parent 4475ce5c63
commit f0bc8f2258
13 changed files with 76 additions and 61 deletions

View File

@ -1,9 +1,5 @@
linters:
enable:
- gofmt
# I'd really want to use Revive for this but that doesn't work:
# https://github.com/golangci/golangci-lint/issues/3653
- unparam
- revive
- usestdlibvars

View File

@ -41,6 +41,7 @@ func TestTokenize(t *testing.T) {
}()
myReader := NewReaderFromStream(fileName, file)
//revive:disable-next-line:empty-block
for !myReader.done.Load() {
}

View File

@ -12,6 +12,8 @@ import (
)
// Files larger than this won't be highlighted
//
//revive:disable-next-line:var-naming
const MAX_HIGHLIGHT_SIZE int64 = 1024 * 1024
// Read and highlight a file using Chroma: https://github.com/alecthomas/chroma

View File

@ -8,6 +8,8 @@ import (
)
// From: https://www.compart.com/en/unicode/U+00A0
//
//revive:disable-next-line:var-naming
const NO_BREAK_SPACE = '\xa0'
func getWrapWidth(line []twin.Cell, maxWrapWidth int) int {

View File

@ -24,8 +24,11 @@ const (
type StatusBarOption int
const (
//revive:disable-next-line:var-naming
STATUSBAR_STYLE_INVERSE StatusBarOption = iota
//revive:disable-next-line:var-naming
STATUSBAR_STYLE_PLAIN
//revive:disable-next-line:var-naming
STATUSBAR_STYLE_BOLD
)
@ -33,7 +36,9 @@ const (
type UnprintableStyle int
const (
//revive:disable-next-line:var-naming
UNPRINTABLE_STYLE_HIGHLIGHT UnprintableStyle = iota
//revive:disable-next-line:var-naming
UNPRINTABLE_STYLE_WHITESPACE
)
@ -475,7 +480,7 @@ func (p *Pager) StartPaging(screen twin.Screen, chromaStyle *chroma.Style, chrom
unprintableStyle = p.UnprintableStyle
consumeLessTermcapEnvs(chromaStyle, chromaFormatter)
styleUi(chromaStyle, chromaFormatter, p.StatusBarStyle)
styleUI(chromaStyle, chromaFormatter, p.StatusBarStyle)
p.screen = screen
p.linePrefix = getLineColorPrefix(chromaStyle, chromaFormatter)

View File

@ -15,6 +15,8 @@ import (
"gotest.tools/v3/assert"
)
//revive:disable:empty-block
const blueBackgroundClearToEol0 = "\x1b[44m\x1b[0K" // With 0 before the K, should clear to EOL
const blueBackgroundClearToEol = "\x1b[44m\x1b[K" // No 0 before the K, should also clear to EOL

View File

@ -465,58 +465,58 @@ func NewReaderFromFilename(filename string, style chroma.Style, formatter chroma
}
// createStatusUnlocked() assumes that its caller is holding the lock
func (r *Reader) createStatusUnlocked(lastLineOneBased int) string {
func (reader *Reader) createStatusUnlocked(lastLineOneBased int) string {
prefix := ""
if r.name != nil {
prefix = path.Base(*r.name) + ": "
if reader.name != nil {
prefix = path.Base(*reader.name) + ": "
}
if len(r.lines) == 0 {
if len(reader.lines) == 0 {
return prefix + "<empty>"
}
if len(r.lines) == 1 {
if len(reader.lines) == 1 {
return prefix + "1 line 100%"
}
percent := int(100 * float64(lastLineOneBased) / float64(len(r.lines)))
percent := int(100 * float64(lastLineOneBased) / float64(len(reader.lines)))
return fmt.Sprintf("%s%s lines %d%%",
prefix,
formatNumber(uint(len(r.lines))),
formatNumber(uint(len(reader.lines))),
percent)
}
// GetLineCount returns the number of lines available for viewing
func (r *Reader) GetLineCount() int {
r.Lock()
defer r.Unlock()
func (reader *Reader) GetLineCount() int {
reader.Lock()
defer reader.Unlock()
return len(r.lines)
return len(reader.lines)
}
// GetLine gets a line. If the requested line number is out of bounds, nil is returned.
func (r *Reader) GetLine(lineNumberOneBased int) *Line {
r.Lock()
defer r.Unlock()
func (reader *Reader) GetLine(lineNumberOneBased int) *Line {
reader.Lock()
defer reader.Unlock()
if lineNumberOneBased < 1 {
return nil
}
if lineNumberOneBased > len(r.lines) {
if lineNumberOneBased > len(reader.lines) {
return nil
}
return r.lines[lineNumberOneBased-1]
return reader.lines[lineNumberOneBased-1]
}
// GetLines gets the indicated lines from the input
//
// Overflow state will be didFit if we returned all lines we currently have, or
// didOverflow otherwise.
func (r *Reader) GetLines(firstLineOneBased int, wantedLineCount int) (*InputLines, overflowState) {
r.Lock()
defer r.Unlock()
return r.getLinesUnlocked(firstLineOneBased, wantedLineCount)
func (reader *Reader) GetLines(firstLineOneBased int, wantedLineCount int) (*InputLines, overflowState) {
reader.Lock()
defer reader.Unlock()
return reader.getLinesUnlocked(firstLineOneBased, wantedLineCount)
}
func nonWrappingAdd(a int, b int) int {
@ -531,16 +531,16 @@ func nonWrappingAdd(a int, b int) int {
return a + b
}
func (r *Reader) getLinesUnlocked(firstLineOneBased int, wantedLineCount int) (*InputLines, overflowState) {
func (reader *Reader) getLinesUnlocked(firstLineOneBased int, wantedLineCount int) (*InputLines, overflowState) {
if firstLineOneBased < 1 {
firstLineOneBased = 1
}
if len(r.lines) == 0 || wantedLineCount == 0 {
if len(reader.lines) == 0 || wantedLineCount == 0 {
return &InputLines{
lines: nil,
firstLineOneBased: firstLineOneBased,
statusText: r.createStatusUnlocked(firstLineOneBased),
statusText: reader.createStatusUnlocked(firstLineOneBased),
},
didFit // Empty files always fit
}
@ -548,8 +548,8 @@ func (r *Reader) getLinesUnlocked(firstLineOneBased int, wantedLineCount int) (*
firstLineZeroBased := firstLineOneBased - 1
lastLineZeroBased := nonWrappingAdd(firstLineZeroBased, wantedLineCount-1)
if lastLineZeroBased >= len(r.lines) {
lastLineZeroBased = len(r.lines) - 1
if lastLineZeroBased >= len(reader.lines) {
lastLineZeroBased = len(reader.lines) - 1
}
// Prevent reading past the end of the available lines
@ -561,19 +561,19 @@ func (r *Reader) getLinesUnlocked(firstLineOneBased int, wantedLineCount int) (*
firstLineOneBased = 1
}
return r.getLinesUnlocked(firstLineOneBased, wantedLineCount)
return reader.getLinesUnlocked(firstLineOneBased, wantedLineCount)
}
returnLines := r.lines[firstLineZeroBased : lastLineZeroBased+1]
returnLines := reader.lines[firstLineZeroBased : lastLineZeroBased+1]
overflow := didFit
if len(returnLines) != len(r.lines) {
if len(returnLines) != len(reader.lines) {
overflow = didOverflow // We're not returning all available lines
}
return &InputLines{
lines: returnLines,
firstLineOneBased: firstLineOneBased,
statusText: r.createStatusUnlocked(lastLineZeroBased + 1),
statusText: reader.createStatusUnlocked(lastLineZeroBased + 1),
},
overflow
}

View File

@ -15,6 +15,8 @@ import (
"gotest.tools/v3/assert"
)
//revive:disable:empty-block
func testGetLineCount(t *testing.T, reader *Reader) {
if strings.Contains(*reader.name, "compressed") {
// We are no good at counting lines of compressed files, never mind
@ -139,8 +141,10 @@ func getTestFiles() []string {
// Wait for reader to finish reading and highlighting. Used by tests.
func (r *Reader) _wait() error {
// Wait for our goroutine to finish
//revive:disable-next-line:empty-block
for !r.done.Load() {
}
//revive:disable-next-line:empty-block
for !r.highlightingDone.Load() {
}
@ -213,7 +217,7 @@ func testHighlightingLineCount(t *testing.T, filenameWithPath string) {
}
rawLinesCount := rawLinefeedsCount
if !rawFileEndsWithNewline {
rawLinesCount += 1
rawLinesCount++
}
// Then load the same file using one of our Readers
@ -329,15 +333,15 @@ func TestCompressedFiles(t *testing.T) {
}
func TestFilterNotInstalled(t *testing.T) {
// FIXME: Test what happens if we try to use a filter that is not installed
t.Skip("FIXME: Test what happens if we try to use a filter that is not installed")
}
func TestFilterFailure(t *testing.T) {
// FIXME: Test what happens if the filter command fails because of bad command line options
t.Skip("FIXME: Test what happens if the filter command fails because of bad command line options")
}
func TestFilterPermissionDenied(t *testing.T) {
// FIXME: Test what happens if the filter command fails because it can't access the requested file
t.Skip("FIXME: Test what happens if the filter command fails because it can't access the requested file")
}
func TestFilterFileNotFound(t *testing.T) {
@ -357,7 +361,7 @@ func TestFilterFileNotFound(t *testing.T) {
}
func TestFilterNotAFile(t *testing.T) {
// FIXME: Test what happens if the filter command fails because the target is not a file
t.Skip("FIXME: Test what happens if the filter command fails because the target is not a file")
}
// How long does it take to read a file?
@ -376,6 +380,7 @@ func BenchmarkReaderDone(b *testing.B) {
}
// Wait for the reader to finish
//revive:disable-next-line:empty-block
for !readMe.done.Load() {
}
if readMe.err != nil {
@ -390,8 +395,8 @@ func BenchmarkReadLargeFile(b *testing.B) {
const largeSizeBytes = 35_000_000
// First, create it from something...
input_filename := getSamplesDir() + "/../m/pager.go"
contents, err := os.ReadFile(input_filename)
inputFilename := getSamplesDir() + "/../m/pager.go"
contents, err := os.ReadFile(inputFilename)
if err != nil {
panic(err)
}
@ -436,8 +441,8 @@ func BenchmarkReadLargeFile(b *testing.B) {
// Count lines in pager.go
func BenchmarkCountLines(b *testing.B) {
// First, get some sample lines...
input_filename := getSamplesDir() + "/../m/pager.go"
contents, err := os.ReadFile(input_filename)
inputFilename := getSamplesDir() + "/../m/pager.go"
contents, err := os.ReadFile(inputFilename)
if err != nil {
panic(err)
}

View File

@ -10,6 +10,8 @@ import (
"gotest.tools/v3/assert"
)
//revive:disable:empty-block
func testHorizontalCropping(t *testing.T, contents string, firstIndex int, lastIndex int, expected string, expectedOverflow overflowState) {
pager := NewPager(nil)
pager.ShowLineNumbers = false

View File

@ -60,23 +60,23 @@ func canonicalFromPager(pager *Pager) scrollPositionCanonical {
}
// Create a new position, scrolled towards the end of the file
func (s scrollPosition) PreviousLine(scrollDistance int) scrollPosition {
func (sp scrollPosition) PreviousLine(scrollDistance int) scrollPosition {
return scrollPosition{
internalDontTouch: scrollPositionInternal{
name: s.internalDontTouch.name,
lineNumberOneBased: s.internalDontTouch.lineNumberOneBased,
deltaScreenLines: s.internalDontTouch.deltaScreenLines - scrollDistance,
name: sp.internalDontTouch.name,
lineNumberOneBased: sp.internalDontTouch.lineNumberOneBased,
deltaScreenLines: sp.internalDontTouch.deltaScreenLines - scrollDistance,
},
}
}
// Create a new position, scrolled towards the end of the file
func (s scrollPosition) NextLine(scrollDistance int) scrollPosition {
func (sp scrollPosition) NextLine(scrollDistance int) scrollPosition {
return scrollPosition{
internalDontTouch: scrollPositionInternal{
name: s.internalDontTouch.name,
lineNumberOneBased: s.internalDontTouch.lineNumberOneBased,
deltaScreenLines: s.internalDontTouch.deltaScreenLines + scrollDistance,
name: sp.internalDontTouch.name,
lineNumberOneBased: sp.internalDontTouch.lineNumberOneBased,
deltaScreenLines: sp.internalDontTouch.deltaScreenLines + scrollDistance,
},
}
}
@ -168,7 +168,7 @@ func (si *scrollPositionInternal) emptyBottomLinesCount(pager *Pager) int {
}
// Move to the next line
lineNumberOneBased += 1
lineNumberOneBased++
}
return unclaimedViewportLines

View File

@ -70,9 +70,9 @@ func (p *Pager) findFirstHit(startPosition scrollPosition, backwards bool) *scro
}
if backwards {
searchPosition -= 1
searchPosition--
} else {
searchPosition += 1
searchPosition++
}
}
}

View File

@ -138,7 +138,7 @@ func (s *styledStringSplitter) consumeControlSequence(charAfterEsc rune) error {
if charAfterEsc == ']' && s.input[startIndex:s.nextByteIndex] == "8;;" {
// Special case, here comes the URL
return s.handleUrl()
return s.handleURL()
}
continue
@ -196,9 +196,9 @@ func (s *styledStringSplitter) handleOsc(sequence string) error {
if endMarker == esc {
if s.nextChar() == '\\' {
return nil
} else {
return fmt.Errorf("Expected ESC \\ after ESC]133;X, got %q", s.lastChar())
}
return fmt.Errorf("Expected ESC \\ after ESC]133;X, got %q", s.lastChar())
}
}
@ -206,7 +206,7 @@ func (s *styledStringSplitter) handleOsc(sequence string) error {
}
// We just got ESC]8; and should now read the URL. URLs end with ASCII 7 BEL or ESC \.
func (s *styledStringSplitter) handleUrl() error {
func (s *styledStringSplitter) handleURL() error {
// Valid URL characters.
// Ref: https://stackoverflow.com/a/1547940/473672
const validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;="

View File

@ -11,7 +11,7 @@ import (
)
// From LESS_TERMCAP_so, overrides statusbarStyle from the Chroma style if set
var standoutStyle *twin.Style = nil
var standoutStyle *twin.Style
var manPageBold = twin.StyleDefault.WithAttr(twin.AttrBold)
var manPageUnderline = twin.StyleDefault.WithAttr(twin.AttrUnderline)
@ -75,7 +75,7 @@ func consumeLessTermcapEnvs(chromaStyle *chroma.Style, chromaFormatter *chroma.F
}
}
func styleUi(chromaStyle *chroma.Style, chromaFormatter *chroma.Formatter, statusbarOption StatusBarOption) {
func styleUI(chromaStyle *chroma.Style, chromaFormatter *chroma.Formatter, statusbarOption StatusBarOption) {
if chromaStyle == nil || chromaFormatter == nil {
return
}