cmd/ncdu: refactor redraw handling

This commit is contained in:
eNV25 2023-01-17 17:44:54 +04:00 committed by Nick Craig-Wood
parent 3affba6fa6
commit 23579e3b99
1 changed files with 11 additions and 15 deletions

View File

@ -82,7 +82,7 @@ the remote you can also use the [size](/commands/rclone_size/) command.
cmd.CheckArgs(1, 1, command, args) cmd.CheckArgs(1, 1, command, args)
fsrc := cmd.NewFsSrc(args) fsrc := cmd.NewFsSrc(args)
cmd.Run(false, false, command, func() error { cmd.Run(false, false, command, func() error {
return NewUI(fsrc).Show() return NewUI(fsrc).Run()
}) })
}, },
} }
@ -354,7 +354,7 @@ func (u *UI) hasEmptyDir() bool {
} }
// Draw the current screen // Draw the current screen
func (u *UI) Draw() error { func (u *UI) Draw() {
ctx := context.Background() ctx := context.Background()
w, h := u.s.Size() w, h := u.s.Size()
u.dirListHeight = h - 3 u.dirListHeight = h - 3
@ -490,8 +490,6 @@ func (u *UI) Draw() error {
if u.showBox { if u.showBox {
u.Box() u.Box()
} }
u.s.Show()
return nil
} }
// Move the cursor this many spaces adjusting the viewport as necessary // Move the cursor this many spaces adjusting the viewport as necessary
@ -901,8 +899,8 @@ func NewUI(f fs.Fs) *UI {
} }
} }
// Show shows the user interface // Run shows the user interface
func (u *UI) Show() error { func (u *UI) Run() error {
var err error var err error
u.s, err = tcell.NewScreen() u.s, err = tcell.NewScreen()
if err != nil { if err != nil {
@ -947,10 +945,6 @@ func (u *UI) Show() error {
// Main loop, waiting for events and channels // Main loop, waiting for events and channels
outer: outer:
for { for {
err := u.Draw()
if err != nil {
return fmt.Errorf("draw failed: %w", err)
}
select { select {
case root := <-rootChan: case root := <-rootChan:
u.root = root u.root = root
@ -961,16 +955,14 @@ outer:
} }
u.listing = false u.listing = false
case <-updated: case <-updated:
// redraw
// TODO: might want to limit updates per second // TODO: might want to limit updates per second
u.sortCurrentDir() u.sortCurrentDir()
case ev := <-events: case ev := <-events:
switch ev := ev.(type) { switch ev := ev.(type) {
case *tcell.EventResize: case *tcell.EventResize:
if u.root != nil { u.Draw()
u.sortCurrentDir() // redraw
}
u.s.Sync() u.s.Sync()
continue // don't draw again
case *tcell.EventKey: case *tcell.EventKey:
var c rune var c rune
if k := ev.Key(); k == tcell.KeyRune { if k := ev.Key(); k == tcell.KeyRune {
@ -1049,11 +1041,15 @@ outer:
// Refresh the screen. Not obvious what key to map // Refresh the screen. Not obvious what key to map
// this onto, but ^L is a common choice. // this onto, but ^L is a common choice.
case key(tcell.KeyCtrlL): case key(tcell.KeyCtrlL):
u.Draw()
u.s.Sync() u.s.Sync()
continue // don't draw again
} }
} }
} }
// listen to key presses, etc.
u.Draw()
u.s.Show()
} }
return nil return nil
} }