Skip to content

Commit

Permalink
Cleanup of less command
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan O'Leary <ryanoleary@google.com>
  • Loading branch information
rjoleary committed Sep 25, 2018
1 parent 0624844 commit 58fd3ac
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 52 deletions.
27 changes: 13 additions & 14 deletions cmds/less/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// less pages through files
// less pages through a file
//
// Synopsis:
// less [OPTIONS] FILES...
// less [OPTIONS] FILE
//
// Options:
// -profile FILE: Save profile in this file
Expand Down Expand Up @@ -34,18 +34,21 @@
// * N: Jump up to previous search result
//
// Author:
// Michael Pratt (github.com/prattmic)
// Michael Pratt (github.com/prattmic) whom we are forever grateful for
// writing https://github.com/prattmic/lesser.
package main

import (
"bytes"
"flag"
"fmt"
"log"
"os"
"runtime/pprof"
"syscall"

"github.com/nsf/termbox-go"
flag "github.com/spf13/pflag"
"github.com/u-root/u-root/pkg/less"
)

var profile = flag.String("profile", "", "Save profile in this file")
Expand All @@ -61,11 +64,11 @@ func mmapFile(f *os.File) ([]byte, error) {
}

func main() {
flag.Parse()
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s: %s filename\n", os.Args[0], os.Args[0])
flag.PrintDefaults()
}
flag.Parse()

if len(flag.Args()) != 1 {
flag.Usage()
Expand All @@ -75,35 +78,31 @@ func main() {
name := flag.Arg(0)
f, err := os.Open(name)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open %s: %v\n", name, err)
os.Exit(1)
log.Fatalf("failed to open %s: %v", name, err)
}

m, err := mmapFile(f)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to mmap file: %v\n", err)
os.Exit(1)
log.Fatalf("failed to mmap file: %v", err)
}
defer syscall.Munmap(m)

err = termbox.Init()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to init: %v\n", err)
os.Exit(1)
log.Fatalf("Failed to init: %v", err)
}
defer termbox.Close()

if *profile != "" {
p, err := os.Create(*profile)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create profile: %v\n", err)
os.Exit(1)
log.Fatalf("Failed to create profile: %v", err)
}
defer p.Close()
pprof.StartCPUProfile(p)
defer pprof.StopCPUProfile()
}

l := NewLesser(bytes.NewReader(m), *tabStop)
l := less.NewLess(bytes.NewReader(m), *tabStop)
l.Run()
}
47 changes: 24 additions & 23 deletions cmds/less/less.go → pkg/less/less.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main
package less

import (
"fmt"
Expand All @@ -12,9 +12,9 @@ import (
"regexp"
"sync"

"github.com/nsf/termbox-go" // TODO: vendor
"github.com/u-root/u-root/cmds/less/lineio"
"github.com/u-root/u-root/cmds/less/sortedmap"
"github.com/nsf/termbox-go"
"github.com/u-root/u-root/pkg/lineio"
"github.com/u-root/u-root/pkg/sortedmap"
)

type size struct {
Expand Down Expand Up @@ -43,7 +43,7 @@ const (
ModeSearchEntry
)

type Lesser struct {
type Less struct {
// src is the source file being displayed.
src lineio.LineReader

Expand Down Expand Up @@ -72,13 +72,13 @@ type Lesser struct {

// searchResults are the results for the current search.
// They should be highlighted.
searchResults searchResults
searchResults *searchResults
}

// lastLine returns the last line on the display. It may be beyond the end
// of the file, if the file is short enough.
// mu must be held on call.
func (l *Lesser) lastLine() int64 {
func (l *Less) lastLine() int64 {
return l.line + int64(l.size.y) - 1
}

Expand Down Expand Up @@ -107,7 +107,7 @@ const (
// scrollLine tries to scroll the display to the given line,
// but will not scroll beyond the first or last lines in the file.
// l.mu must be held when calling scrollLine.
func (l *Lesser) scrollLine(dest int64) {
func (l *Less) scrollLine(dest int64) {
var delta int64
if dest > l.line {
delta = 1
Expand All @@ -122,7 +122,7 @@ func (l *Lesser) scrollLine(dest int64) {

// scroll moves the display based on the passed scroll action, without
// going past the beginning or end of the file.
func (l *Lesser) scroll(s Scroll) {
func (l *Less) scroll(s Scroll) {
l.mu.Lock()
defer l.mu.Unlock()

Expand Down Expand Up @@ -150,7 +150,7 @@ func (l *Lesser) scroll(s Scroll) {
l.scrollLine(dest)
}

func (l *Lesser) handleEvent(e termbox.Event) {
func (l *Less) handleEvent(e termbox.Event) {
l.mu.Lock()
mode := l.mode
l.mu.Unlock()
Expand Down Expand Up @@ -238,7 +238,7 @@ func (l *Lesser) handleEvent(e termbox.Event) {
}
}

func (l *Lesser) listenEvents() {
func (l *Less) listenEvents() {
for {
e := termbox.PollEvent()
l.handleEvent(e)
Expand Down Expand Up @@ -278,8 +278,8 @@ type searchResults struct {
results []searchResult
}

func NewSearchResults() searchResults {
return searchResults{
func NewSearchResults() *searchResults {
return &searchResults{
lines: sortedmap.NewMap(),
}
}
Expand Down Expand Up @@ -337,7 +337,7 @@ func (s *searchResults) Prev(line int64) (searchResult, bool) {
return s.results[i], true
}

func (l *Lesser) search(s string) searchResults {
func (l *Less) search(s string) *searchResults {
reg, err := regexp.Compile(s)
if err != nil {
// TODO(prattmic): display a better error
Expand Down Expand Up @@ -407,7 +407,7 @@ func (l *Lesser) search(s string) searchResults {

// statusBar renders the status bar.
// mu must be held on call.
func (l *Lesser) statusBar() {
func (l *Less) statusBar() {
// The statusbar is just below the display.

// Clear the statusbar
Expand Down Expand Up @@ -435,7 +435,7 @@ func alignUp(n, divisor int) int {
return n + (divisor - (n % divisor))
}

func (l *Lesser) refreshScreen() error {
func (l *Less) refreshScreen() error {
l.mu.Lock()
defer l.mu.Unlock()

Expand Down Expand Up @@ -491,7 +491,7 @@ func (l *Lesser) refreshScreen() error {
return nil
}

func (l *Lesser) Run() {
func (l *Less) Run() {
// Start populating the LineReader cache, to speed things up later.
go l.src.Populate()

Expand Down Expand Up @@ -519,16 +519,17 @@ func (l *Lesser) Run() {
}
}

func NewLesser(r io.ReaderAt, ts int) Lesser {
func NewLess(r io.ReaderAt, ts int) Less {
x, y := termbox.Size()

return Lesser{
return Less{
src: lineio.NewLineReader(r),
tabStop: ts,
// Save one line for statusbar.
size: size{x: x, y: y - 1},
line: 1,
events: make(chan Event, 1),
mode: ModeNormal,
size: size{x: x, y: y - 1},
line: 1,
events: make(chan Event, 1),
mode: ModeNormal,
searchResults: NewSearchResults(),
}
}
4 changes: 2 additions & 2 deletions cmds/less/lineio/lineio.go → pkg/lineio/lineio.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"io"
"regexp"

"github.com/u-root/u-root/cmds/less/sortedmap"
"github.com/u-root/u-root/pkg/sortedmap"
)

type LineReader struct {
Expand Down Expand Up @@ -57,7 +57,7 @@ func (l *LineReader) scanForLine(line, curLine, curOffset int64) (offset int64,
}
}

curLine += 1
curLine++

l.offsetCache.Insert(curLine, offset)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"sync"
)

var ErrNoSuchKey = errors.New("No such key exists.")
var ErrNoSuchKey = errors.New("no such key exists")

// SearchInt64s implements sort.SearchInts for int64.
func SearchInt64s(a []int64, x int64) int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,30 +79,30 @@ func TestDelete(t *testing.T) {
},
}

for _, c := range cases {
for _, k := range c.del {
c.before.Delete(k)
for i := range cases {
for _, k := range cases[i].del {
cases[i].before.Delete(k)
}

// All expected entries exist
for k, e := range c.after.m {
v, ok := c.before.m[k]
for k, e := range cases[i].after.m {
v, ok := cases[i].before.m[k]
if !ok {
t.Errorf("%d not found in %v", k, c.before.m)
t.Errorf("%d not found in %v", k, cases[i].before.m)
}
if v != e {
t.Errorf("got %d want %d in %v", v, e, c.before.m)
t.Errorf("got %d want %d in %v", v, e, cases[i].before.m)
}
}

if len(c.before.k) != len(c.after.k) {
t.Errorf("Bad length, got %d, expected %d. %v vs %v", len(c.before.k), len(c.after.k), c.before.k, c.after.k)
if len(cases[i].before.k) != len(cases[i].after.k) {
t.Errorf("Bad length, got %d, expected %d. %v vs %v", len(cases[i].before.k), len(cases[i].after.k), cases[i].before.k, cases[i].after.k)
}

// Key slice is correct
for i, e := range c.after.k {
if c.before.k[i] != e {
t.Errorf("Got %v, expected %v", c.before.k, c.after.k)
for i, e := range cases[i].after.k {
if cases[i].before.k[i] != e {
t.Errorf("Got %v, expected %v", cases[i].before.k, cases[i].after.k)
break
}
}
Expand Down
File renamed without changes.

0 comments on commit 58fd3ac

Please sign in to comment.