Skip to content

Commit

Permalink
Move creating and redrawing the UI into functions
Browse files Browse the repository at this point in the history
  • Loading branch information
slarwise committed Aug 16, 2024
1 parent 598f24d commit 0e5bbfa
Showing 1 changed file with 43 additions and 28 deletions.
71 changes: 43 additions & 28 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ type Ui struct {
ShowHelp bool
}

func newUi(vaultClient vault.Client, mounts []string) (Ui, error) {
screen, err := tcell.NewScreen()
if err != nil {
return Ui{}, fmt.Errorf("Failed to create a terminal screen: %s", err)
}
if err := screen.Init(); err != nil {
return Ui{}, fmt.Errorf("Failed to initialize terminal screen: %s", err)
}
screen.EnablePaste()
screen.Clear()
width, height := screen.Size()
return Ui{
Vault: vaultClient,
Mounts: mounts,
CurrentMount: 0,
ShowHelp: true,
Screen: screen,
Width: width,
Height: height,
}, nil
}

const (
SCROLL_OFF = 4
)
Expand Down Expand Up @@ -75,47 +97,36 @@ func main() {
} else {
log.SetOutput(io.Discard)
}
screen, err := tcell.NewScreen()
ui, err := newUi(vaultClient, mounts)
if err != nil {
fatal("Failed to create a terminal screen", "err", err)
}
if err := screen.Init(); err != nil {
fatal("Failed to initialize terminal screen", "err", err)
}
screen.EnablePaste()
screen.Clear()
ui := Ui{
Screen: screen,
Vault: vaultClient,
Mounts: mounts,
CurrentMount: 0,
ShowHelp: true,
fatal("Failed to initialize UI", "err", err)
}
quit := func() {
// You have to catch panics in a defer, clean up, and
// re-raise them - otherwise your application can
// die without leaving any diagnostic trace.
errorMsg := recover()
screen.Fini()
ui.Screen.Fini()
if errorMsg != nil {
fmt.Fprintf(os.Stderr, "%s\n", errorMsg)
} else if len(ui.Result) != 0 {
fmt.Printf("%s\n", ui.Result)
}
}
defer quit()
ui.Width, ui.Height = screen.Size()
ui.drawPrompt()
drawLoadingScreen(ui)
screen.Show()
ui.Screen.Show()
ui.Keys = vaultClient.GetKeys(ui.Mounts[ui.CurrentMount])
ui.newKeysView()
ui.Redraw()
for {
ev := screen.PollEvent()
ev := ui.Screen.PollEvent()
slog.Info("event", "ev", fmt.Sprintf("%T", ev))
switch ev := ev.(type) {
case *tcell.EventResize:
screen.Sync()
ui.Width, ui.Height = screen.Size()
ui.Screen.Sync()
ui.Width, ui.Height = ui.Screen.Size()
ui.ViewEnd = min(nKeysToShow(ui.Height), len(ui.FilteredKeys))
if ui.ViewStart+ui.Cursor >= ui.ViewEnd {
ui.Cursor = 0
Expand Down Expand Up @@ -165,17 +176,21 @@ func main() {
}
}

screen.Clear()
ui.drawKeys()
ui.drawScrollbar()
ui.drawStats()
ui.drawHelp()
ui.drawPrompt()
ui.drawSecret()
screen.Show()
ui.Redraw()
}
}

func (u Ui) Redraw() {
u.Screen.Clear()
u.drawKeys()
u.drawScrollbar()
u.drawStats()
u.drawHelp()
u.drawPrompt()
u.drawSecret()
u.Screen.Show()
}

func drawLine(s tcell.Screen, x, y int, style tcell.Style, text string) {
for _, r := range []rune(text) {
s.SetContent(x, y, r, nil, style)
Expand Down

0 comments on commit 0e5bbfa

Please sign in to comment.