Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds modal dialogs #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func main() {
log.Fatal().Err(err)
}

appUI := ui.New()
appUI := ui.NewUI()

// Get the "root" cobra cli command
cmd := commands.Root(&commands.CommandProps{
Expand Down
51 changes: 51 additions & 0 deletions internal/ui/component/modal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package component

import (
"github.com/rivo/tview"
"github.com/robgonnella/ops/internal/ui/style"
)

type ModalButton struct {
Label string
OnClick func()
}

type Modal struct {
root *tview.Modal
}

func NewModal(message string, buttons []ModalButton) *Modal {
modal := tview.NewModal()

buttonLabels := []string{}

for _, b := range buttons {
buttonLabels = append(buttonLabels, b.Label)
}

modal.AddButtons(buttonLabels)

modal.SetText(message)

modal.SetDoneFunc(func(buttonIdx int, buttonLabel string) {
for _, b := range buttons {
if buttonLabel == b.Label {
b.OnClick()
}
}
})

modal.SetBackgroundColor(style.ColorDefault).
SetTextColor(style.ColorPurple).
SetButtonBackgroundColor(style.ColorLightGreen).
SetButtonTextColor(style.ColorBlack).
SetBorderColor(style.ColorLightGreen)

return &Modal{
root: modal,
}
}

func (m *Modal) Primitive() tview.Primitive {
return m.root
}
2 changes: 1 addition & 1 deletion internal/ui/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type UI struct {
var originalStdout = os.Stdout
var originalStderr = os.Stderr

func New() *UI {
func NewUI() *UI {
return &UI{}
}

Expand Down
68 changes: 64 additions & 4 deletions internal/ui/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ui

import (
"context"
"fmt"
"os"
"os/exec"
"sort"
Expand All @@ -19,7 +20,7 @@ import (
)

func restart() error {
newUI := New()
newUI := NewUI()
return newUI.Launch()
}

Expand All @@ -34,6 +35,7 @@ type view struct {
eventTable *component.EventTable
configureForm *component.ConfigureForm
contextTable *component.ConfigContext
contextToDelete string
appCore *core.Core
serverUpdateChan chan []*server.Server
eventUpdateChan chan *event.Event
Expand Down Expand Up @@ -109,6 +111,7 @@ func newView(userIP string, appCore *core.Core) *view {
v.eventTable = eventTable
v.configureForm = configureForm
v.contextTable = contextTable
v.contextToDelete = ""
v.serverUpdateChan = serverUpdateChan
v.eventUpdateChan = eventUpdateChan
v.serverPollListenerId = serverPollListenerId
Expand Down Expand Up @@ -149,6 +152,8 @@ func (v *view) onActionSubmit(text string) {
func (v *view) onConfigureFormSubmit(conf config.Config) {
if err := v.appCore.UpdateConfig(conf); err != nil {
v.logger.Error().Err(err).Msg("failed to write config file")
v.showErrorModal("Failed to write config file")
return
}

v.stop()
Expand All @@ -157,24 +162,76 @@ func (v *view) onConfigureFormSubmit(conf config.Config) {

func (v *view) onContextSelect(name string) {
if err := v.appCore.SetConfig(name); err != nil {
v.logger.Error().Err(err).Msg("failed to set new config")
v.logger.Error().Err(err).Msg("failed to set new context")
v.showErrorModal("Failed to set new context")
return
}

v.stop()
restart()
}

func (v *view) dismissContextDelete() {
v.contextToDelete = ""
v.app.SetRoot(v.root, true)
}

func (v *view) onContextDelete(name string) {
if err := v.appCore.DeleteConfig(name); err != nil {
v.contextToDelete = name
buttons := []component.ModalButton{
{
Label: "OK",
OnClick: v.deleteContext,
},
{
Label: "Dismiss",
OnClick: v.dismissContextDelete,
},
}
contextDeleteConfirm := component.NewModal(
fmt.Sprintf("Delete %s configuration?", name),
buttons,
)
v.app.SetRoot(contextDeleteConfirm.Primitive(), false)
}

func (v *view) deleteContext() {
if v.contextToDelete == "" {
return
}

defer func() {
v.contextToDelete = ""
}()

if err := v.appCore.DeleteConfig(v.contextToDelete); err != nil {
v.logger.Error().Err(err).Msg("failed to delete config")
v.showErrorModal("Failed to delete context")
return
}

v.stop()
restart()
}

func (v *view) showErrorModal(message string) {
buttons := []component.ModalButton{
{
Label: "Dismiss",
OnClick: v.dismissErrorModal,
},
}
errorModal := component.NewModal(
message,
buttons,
)
v.app.SetRoot(errorModal.Primitive(), false)
}

func (v *view) dismissErrorModal() {
v.app.SetRoot(v.root, true)
}

func (v *view) bindKeys() {
v.app.SetInputCapture(func(evt *tcell.EventKey) *tcell.EventKey {
switch evt.Key() {
Expand Down Expand Up @@ -214,9 +271,11 @@ func (v *view) focus() {

if err != nil {
v.logger.Error().Err(err).Msg("")
v.showErrorModal("Failed to retrieve configurations from database")
return
}

if err == nil && len(confs) > 1 {
if len(confs) > 1 {
v.header.RemoveAllExtraLegendKeys()
v.header.AddLegendKey("ctrl+d", "delete context")
v.header.AddLegendKey("enter", "select new context")
Expand All @@ -235,6 +294,7 @@ func (v *view) onSSH(ip string) {
defer func() {
if err := restart(); err != nil {
v.logger.Error().Err(err).Msg("error restarting ui")
os.Exit(1)
}
}()

Expand Down