Skip to content

Commit

Permalink
feat: real pager with incremental search
Browse files Browse the repository at this point in the history
Closes #51
Closes #77
Closes #105

bubbles.Viewport isn't really appropriate for a buffer that we keep
appending to like this: we have to manually bookkeep a lot of state back
and forth between the tui and the viewports——each time we receive a
line, we send the whole-ass buffer into the viewport.

Instead, let's encapsulate the viewports and the log content together
into a new ui component per-stream.

- adds vim-style incremental search with highlighting
- scroll performance is now roughly O(window height) instead of O(buffer size)
- adds "responsive design", making the ui a lot smaller in small windows
- moves tui help into its own pane, accessed with 'h' or '?'
- adds a bonus package, cmd/scroll, which is a tail replacement of dubious value
- adds some usability features, like a confirmation before quitting

Additionally, we clean up our use of color a bit, so that run looks good
whether you're in dark mode or light.

- make color hash adaptive
- use adaptive colors everywhere
- don't use backgrounds anywhere
  • Loading branch information
amonks committed Apr 2, 2024
1 parent 40da51e commit d6dd968
Show file tree
Hide file tree
Showing 35 changed files with 2,091 additions and 691 deletions.
27 changes: 0 additions & 27 deletions CREDITS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -633,33 +633,6 @@ SOFTWARE.



github.com/sahilm/[email protected]
==============================

The MIT License (MIT)

Copyright (c) 2017 Sahil Muthoo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



golang.org/x/[email protected]
========================

Expand Down
2 changes: 1 addition & 1 deletion cmd/run/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func tasklistText(tasks run.Tasks) string {
t := tasks[id]
meta := t.Metadata()

fmt.Fprintf(b, " %s\n", color.Render(id))
fmt.Fprintf(b, " %s\n", color.RenderHash(id))
fmt.Fprintf(b, " Type: %s\n", italicStyle.Render(meta.Type))
if meta.Description != "" {
fmt.Fprintf(b, " Description:\n")
Expand Down
103 changes: 103 additions & 0 deletions cmd/scroll/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Scroll is a tailing pager.
//
// - Unlike tail, it has interactive features like
// paging and incremental regex search.
//
// - Unlike less, it continuously tries to keep reading
// more bytes from the input file.
//
// - Unlike less and tail, it loads the whole file into
// memory before starting, which makes it perform worse
// on files which are already large.
//
// On my machine, I start to notice perceptible latency:
// - Changing search query in a file larger than a few dozen MB
// - Opening a file larger than a few hundred MB
//
// I never notice latency in scrolling or tailing, even in
// files up to 15 GB. I didn't test any files larger than that
// because they take too long to open.
package main

import (
"flag"
"fmt"
"os"

"github.com/amonks/run/pkg/logview"
tea "github.com/charmbracelet/bubbletea"
)

func main() {
flag.Parse()

program := tea.NewProgram(newScroll(),
tea.WithAltScreen(),
tea.WithMouseCellMotion())

filename := flag.Arg(0)

sinkErr := make(chan error)
sink := Sink(func(s string) { program.Send(writeMsg(s)) })
go func() {
switch filename {
case "-", "":
sinkErr <- sink.tailStdin()
default:
sinkErr <- sink.tailFile(filename)
}
}()

programErr := make(chan error)
go func() {
_, err := program.Run()
programErr <- err
}()

select {
case err := <-sinkErr:
program.Send(tea.Quit())
<-programErr
fmt.Println(err)
os.Exit(1)
case err := <-programErr:
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

os.Exit(0)
}

type (
writeMsg string
)

type scroll struct {
logview *logview.Model
}

func newScroll() *scroll {
return &scroll{logview.New()}
}

var _ tea.Model = &scroll{}

func (t *scroll) Init() tea.Cmd { return t.logview.Init() }

func (t *scroll) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
t.logview.SetDimensions(msg.Width, msg.Height)
return t, nil
case writeMsg:
t.logview.Write(string(msg))
return t, nil
}
model, cmd := t.logview.Update(msg)
t.logview = model.(*logview.Model)
return t, cmd
}

func (t *scroll) View() string { return t.logview.View() }
45 changes: 45 additions & 0 deletions cmd/scroll/tail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"bufio"
"io"
"os"
"time"
)

type Sink func(string)

func (sink Sink) tailStdin() error {
sc := bufio.NewScanner(os.Stdin)
defer os.Stdin.Close()
for {
if !sc.Scan() {
if err := sc.Err(); err != nil {
return err
} else {
sink("EOF\n")
}
break
}
sink(sc.Text() + "\n")
}
return nil
}

func (sink Sink) tailFile(filename string) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()

reader := bufio.NewReader(file)
for {
bs, err := io.ReadAll(reader)
if err != nil {
return err
}
sink(string(bs))
time.Sleep(time.Millisecond * 32)
}
}
32 changes: 18 additions & 14 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,6 @@ <h3>Package files</h3>

<a href="https://github.com/amonks/run/blob/main/pkg/run/func_task.go">func_task.go</a>

<a href="https://github.com/amonks/run/blob/main/pkg/run/mutex.go">mutex.go</a>

<a href="https://github.com/amonks/run/blob/main/pkg/run/output_writer.go">output_writer.go</a>

<a href="https://github.com/amonks/run/blob/main/pkg/run/printer.go">printer.go</a>
Expand All @@ -389,6 +387,12 @@ <h3>Package files</h3>

<a href="https://github.com/amonks/run/blob/main/pkg/run/tui.go">tui.go</a>

<a href="https://github.com/amonks/run/blob/main/pkg/run/tui_styles.go">tui_styles.go</a>

<a href="https://github.com/amonks/run/blob/main/pkg/run/tui_update.go">tui_update.go</a>

<a href="https://github.com/amonks/run/blob/main/pkg/run/tui_view.go">tui_view.go</a>

<a href="https://github.com/amonks/run/blob/main/pkg/run/ui.go">ui.go</a>

<a href="https://github.com/amonks/run/blob/main/pkg/run/validator.go">validator.go</a>
Expand All @@ -409,7 +413,7 @@ <h3>Package files</h3>



<h2 id="MultiWriter">type <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L141">MultiWriter</a>
<h2 id="MultiWriter">type <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L142">MultiWriter</a>
<a class="permalink" href="#MultiWriter">&#xb6;</a>


Expand Down Expand Up @@ -437,7 +441,7 @@ <h2 id="MultiWriter">type <a href="https://github.com/amonks/run/blob/main/pkg/r



<h2 id="Run">type <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L100">Run</a>
<h2 id="Run">type <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L101">Run</a>
<a class="permalink" href="#Run">&#xb6;</a>


Expand All @@ -464,7 +468,7 @@ <h2 id="Run">type <a href="https://github.com/amonks/run/blob/main/pkg/run/run_t



<h3 id="RunTask">func <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L20">RunTask</a>
<h3 id="RunTask">func <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L21">RunTask</a>
<a class="permalink" href="#RunTask">&#xb6;</a>


Expand All @@ -480,7 +484,7 @@ <h3 id="RunTask">func <a href="https://github.com/amonks/run/blob/main/pkg/run/r



<h3 id="Run.IDs">func (*Run) <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L148">IDs</a>
<h3 id="Run.IDs">func (*Run) <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L149">IDs</a>
<a class="permalink" href="#Run.IDs">&#xb6;</a>


Expand All @@ -495,7 +499,7 @@ <h3 id="Run.IDs">func (*Run) <a href="https://github.com/amonks/run/blob/main/pk



<h3 id="Run.Invalidate">func (*Run) <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L170">Invalidate</a>
<h3 id="Run.Invalidate">func (*Run) <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L171">Invalidate</a>
<a class="permalink" href="#Run.Invalidate">&#xb6;</a>


Expand All @@ -509,7 +513,7 @@ <h3 id="Run.Invalidate">func (*Run) <a href="https://github.com/amonks/run/blob/



<h3 id="Run.Start">func (*Run) <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L193">Start</a>
<h3 id="Run.Start">func (*Run) <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L194">Start</a>
<a class="permalink" href="#Run.Start">&#xb6;</a>


Expand All @@ -523,7 +527,7 @@ <h3 id="Run.Start">func (*Run) <a href="https://github.com/amonks/run/blob/main/



<h3 id="Run.TaskStatus">func (*Run) <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L164">TaskStatus</a>
<h3 id="Run.TaskStatus">func (*Run) <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L165">TaskStatus</a>
<a class="permalink" href="#Run.TaskStatus">&#xb6;</a>


Expand All @@ -536,7 +540,7 @@ <h3 id="Run.TaskStatus">func (*Run) <a href="https://github.com/amonks/run/blob/



<h3 id="Run.Tasks">func (*Run) <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L159">Tasks</a>
<h3 id="Run.Tasks">func (*Run) <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L160">Tasks</a>
<a class="permalink" href="#Run.Tasks">&#xb6;</a>


Expand All @@ -549,7 +553,7 @@ <h3 id="Run.Tasks">func (*Run) <a href="https://github.com/amonks/run/blob/main/



<h3 id="Run.Type">func (*Run) <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L187">Type</a>
<h3 id="Run.Type">func (*Run) <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L188">Type</a>
<a class="permalink" href="#Run.Type">&#xb6;</a>


Expand All @@ -568,7 +572,7 @@ <h3 id="Run.Type">func (*Run) <a href="https://github.com/amonks/run/blob/main/p



<h2 id="RunType">type <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L489">RunType</a>
<h2 id="RunType">type <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L490">RunType</a>
<a class="permalink" href="#RunType">&#xb6;</a>


Expand Down Expand Up @@ -650,7 +654,7 @@ <h3 id="FuncTask">func <a href="https://github.com/amonks/run/blob/main/pkg/run/



<h3 id="ScriptTask">func <a href="https://github.com/amonks/run/blob/main/pkg/run/script_task.go#L29">ScriptTask</a>
<h3 id="ScriptTask">func <a href="https://github.com/amonks/run/blob/main/pkg/run/script_task.go#L30">ScriptTask</a>
<a class="permalink" href="#ScriptTask">&#xb6;</a>


Expand Down Expand Up @@ -777,7 +781,7 @@ <h2 id="TaskMetadata">type <a href="https://github.com/amonks/run/blob/main/pkg/



<h2 id="TaskStatus">type <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L125">TaskStatus</a>
<h2 id="TaskStatus">type <a href="https://github.com/amonks/run/blob/main/pkg/run/run_task.go#L126">TaskStatus</a>
<a class="permalink" href="#TaskStatus">&#xb6;</a>


Expand Down
Loading

0 comments on commit d6dd968

Please sign in to comment.