mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-15 00:15:32 +02:00
Make sure scrollbars have the right size initially
We refresh the view after reading just enough to fill it, so that we see the initial content as quickly as possible, but then we continue reading enough lines so that we can tell how long the scrollbar needs to be, and then we refresh again. This can result in slight flicker of the scrollbar when it is first drawn with a bigger size and then jumps to a smaller size; however, that's a good tradeoff for a solution that provides both good speed and accuracy.
This commit is contained in:
@ -4,6 +4,8 @@ import (
|
||||
"bytes"
|
||||
"io"
|
||||
"os/exec"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
@ -45,7 +47,7 @@ func TestNewCmdTaskInstantStop(t *testing.T) {
|
||||
return cmd, reader
|
||||
}
|
||||
|
||||
fn := manager.NewCmdTask(start, "prefix\n", 20, onDone)
|
||||
fn := manager.NewCmdTask(start, "prefix\n", LinesToRead{20, -1}, onDone)
|
||||
|
||||
_ = fn(stop)
|
||||
|
||||
@ -99,7 +101,7 @@ func TestNewCmdTask(t *testing.T) {
|
||||
return cmd, reader
|
||||
}
|
||||
|
||||
fn := manager.NewCmdTask(start, "prefix\n", 20, onDone)
|
||||
fn := manager.NewCmdTask(start, "prefix\n", LinesToRead{20, -1}, onDone)
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
@ -134,3 +136,111 @@ func TestNewCmdTask(t *testing.T) {
|
||||
t.Errorf("expected writer to receive the following content: \n%s\n. But instead it received: %s", expectedContent, actualContent)
|
||||
}
|
||||
}
|
||||
|
||||
// A dummy reader that simply yields as many blank lines as requested. The only
|
||||
// thing we want to do with the output is count the number of lines.
|
||||
type BlankLineReader struct {
|
||||
totalLinesToYield int
|
||||
linesYielded int
|
||||
}
|
||||
|
||||
func (d *BlankLineReader) Read(p []byte) (n int, err error) {
|
||||
if d.totalLinesToYield == d.linesYielded {
|
||||
return 0, io.EOF
|
||||
}
|
||||
|
||||
d.linesYielded += 1
|
||||
p[0] = '\n'
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
func TestNewCmdTaskRefresh(t *testing.T) {
|
||||
type scenario struct {
|
||||
name string
|
||||
totalTaskLines int
|
||||
linesToRead LinesToRead
|
||||
expectedLineCountsOnRefresh []int
|
||||
}
|
||||
|
||||
scenarios := []scenario{
|
||||
{
|
||||
"total < initialRefreshAfter",
|
||||
150,
|
||||
LinesToRead{100, 120},
|
||||
[]int{100, 100},
|
||||
},
|
||||
{
|
||||
"total == initialRefreshAfter",
|
||||
150,
|
||||
LinesToRead{100, 100},
|
||||
[]int{100, 100, 100},
|
||||
},
|
||||
{
|
||||
"total > initialRefreshAfter",
|
||||
150,
|
||||
LinesToRead{100, 50},
|
||||
[]int{50, 100, 100},
|
||||
},
|
||||
{
|
||||
"initialRefreshAfter == -1",
|
||||
150,
|
||||
LinesToRead{100, -1},
|
||||
[]int{100, 100},
|
||||
},
|
||||
{
|
||||
"totalTaskLines < initialRefreshAfter",
|
||||
25,
|
||||
LinesToRead{100, 50},
|
||||
[]int{25},
|
||||
},
|
||||
{
|
||||
"totalTaskLines between total and initialRefreshAfter",
|
||||
75,
|
||||
LinesToRead{100, 50},
|
||||
[]int{50, 75},
|
||||
},
|
||||
}
|
||||
|
||||
for _, s := range scenarios {
|
||||
writer := bytes.NewBuffer(nil)
|
||||
lineCountsOnRefresh := []int{}
|
||||
refreshView := func() {
|
||||
lineCountsOnRefresh = append(lineCountsOnRefresh, strings.Count(writer.String(), "\n"))
|
||||
}
|
||||
|
||||
manager := NewViewBufferManager(
|
||||
utils.NewDummyLog(),
|
||||
writer,
|
||||
func() {},
|
||||
refreshView,
|
||||
func() {},
|
||||
func() {},
|
||||
)
|
||||
|
||||
stop := make(chan struct{})
|
||||
reader := BlankLineReader{totalLinesToYield: s.totalTaskLines}
|
||||
start := func() (*exec.Cmd, io.Reader) {
|
||||
// not actually starting this because it's not necessary
|
||||
cmd := secureexec.Command("blah blah")
|
||||
|
||||
return cmd, &reader
|
||||
}
|
||||
|
||||
fn := manager.NewCmdTask(start, "", s.linesToRead, func() {})
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
close(stop)
|
||||
wg.Done()
|
||||
}()
|
||||
_ = fn(stop)
|
||||
|
||||
wg.Wait()
|
||||
|
||||
if !reflect.DeepEqual(lineCountsOnRefresh, s.expectedLineCountsOnRefresh) {
|
||||
t.Errorf("%s: expected line counts on refresh: %v, got %v",
|
||||
s.name, s.expectedLineCountsOnRefresh, lineCountsOnRefresh)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user