diff --git a/internal/terminal/handler/ghrepository/ghrepository.go b/internal/terminal/handler/ghrepository/ghrepository.go index 23864ad..96318c8 100644 --- a/internal/terminal/handler/ghrepository/ghrepository.go +++ b/internal/terminal/handler/ghrepository/ghrepository.go @@ -41,7 +41,7 @@ type ModelGithubRepository struct { Viewport *viewport.Model tableGithubRepository table.Model searchTableGithubRepository table.Model - modelError hdlerror.ModelError + modelError *hdlerror.ModelError modelTabOptions tea.Model actualModelTabOptions *taboptions.Options @@ -110,14 +110,14 @@ func SetupModelGithubRepository(githubUseCase gu.UseCase, selectedRepository *hd // setup models modelError := hdlerror.SetupModelError() - tabOptions := taboptions.NewOptions() + tabOptions := taboptions.NewOptions(&modelError) return &ModelGithubRepository{ Help: help.New(), Keys: keys, githubUseCase: githubUseCase, tableGithubRepository: tableGithubRepository, - modelError: modelError, + modelError: &modelError, SelectedRepository: selectedRepository, modelTabOptions: tabOptions, actualModelTabOptions: tabOptions, diff --git a/internal/terminal/handler/ghworkflow/ghworkflow.go b/internal/terminal/handler/ghworkflow/ghworkflow.go index e0aa96e..a334782 100644 --- a/internal/terminal/handler/ghworkflow/ghworkflow.go +++ b/internal/terminal/handler/ghworkflow/ghworkflow.go @@ -41,7 +41,7 @@ type ModelGithubWorkflow struct { Viewport *viewport.Model list list.Model tableTriggerableWorkflow table.Model - modelError hdlerror.ModelError + modelError *hdlerror.ModelError modelTabOptions tea.Model actualModelTabOptions *taboptions.Options @@ -76,12 +76,14 @@ func SetupModelGithubWorkflow(githubUseCase gu.UseCase, selectedRepository *hdlt Bold(false) tableTriggerableWorkflow.SetStyles(s) - tabOptions := taboptions.NewOptions() + modelError := hdlerror.SetupModelError() + tabOptions := taboptions.NewOptions(&modelError) return &ModelGithubWorkflow{ Help: help.New(), Keys: keys, githubUseCase: githubUseCase, + modelError: &modelError, tableTriggerableWorkflow: tableTriggerableWorkflow, SelectedRepository: selectedRepository, modelTabOptions: tabOptions, diff --git a/internal/terminal/handler/ghworkflowhistory/ghworkflowhistory.go b/internal/terminal/handler/ghworkflowhistory/ghworkflowhistory.go index 1fc338b..bd0e162 100644 --- a/internal/terminal/handler/ghworkflowhistory/ghworkflowhistory.go +++ b/internal/terminal/handler/ghworkflowhistory/ghworkflowhistory.go @@ -44,7 +44,7 @@ type ModelGithubWorkflowHistory struct { Help help.Model Viewport *viewport.Model tableWorkflowHistory table.Model - modelError hdlerror.ModelError + modelError *hdlerror.ModelError modelTabOptions tea.Model actualModelTabOptions *taboptions.Options @@ -76,14 +76,15 @@ func SetupModelGithubWorkflowHistory(githubUseCase gu.UseCase, selectedRepositor Bold(false) tableWorkflowHistory.SetStyles(s) - tabOptions := taboptions.NewOptions() + modelError := hdlerror.SetupModelError() + tabOptions := taboptions.NewOptions(&modelError) return &ModelGithubWorkflowHistory{ Help: help.New(), Keys: keys, githubUseCase: githubUseCase, tableWorkflowHistory: tableWorkflowHistory, - modelError: hdlerror.SetupModelError(), + modelError: &modelError, SelectedRepository: selectedRepository, modelTabOptions: tabOptions, actualModelTabOptions: tabOptions, diff --git a/internal/terminal/handler/taboptions/taboptions.go b/internal/terminal/handler/taboptions/taboptions.go index 4379ebd..c0e116f 100644 --- a/internal/terminal/handler/taboptions/taboptions.go +++ b/internal/terminal/handler/taboptions/taboptions.go @@ -2,15 +2,21 @@ package taboptions import ( "fmt" + "strings" "time" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" + hdlerror "github.com/termkit/gama/internal/terminal/handler/error" ) type Options struct { Style lipgloss.Style + modelError *hdlerror.ModelError + previousModelError hdlerror.ModelError + modelLock bool + status OptionStatus options []string @@ -42,7 +48,7 @@ func (o OptionStatus) String() string { return string(o) } -func NewOptions() *Options { +func NewOptions(modelError *hdlerror.ModelError) *Options { var b = lipgloss.RoundedBorder() b.Right = "├" b.Left = "┤" @@ -68,6 +74,7 @@ func NewOptions() *Options { optionsAction: initialOptionsAction, optionsWithFunc: optionsWithFunc, status: OptionWait, + modelError: modelError, } } @@ -86,6 +93,28 @@ func (o *Options) AddOption(option string, action func()) { o.optionsWithFunc[optionNumber] = action } +func (o *Options) getOptionMessage() string { + option := o.options[o.cursor] + option = strings.TrimPrefix(option, fmt.Sprintf("%d) ", o.cursor)) + return option +} + +func (o *Options) showAreYouSure() { + if !o.modelLock { + o.previousModelError = *o.modelError + o.modelLock = true + } + o.modelError.Reset() + o.modelError.SetProgressMessage(fmt.Sprintf("Are you sure you want to %s?", o.getOptionMessage())) +} + +func (o *Options) switchToPreviousError() { + if o.modelLock { + return + } + *o.modelError = o.previousModelError +} + func (o *Options) executeOption() { go o.optionsWithFunc[o.cursor]() o.cursor = 0 @@ -107,6 +136,8 @@ func (o *Options) resetOptionsWithOriginal() { time.Sleep(1 * time.Second) o.timer-- } + o.modelLock = false + o.switchToPreviousError() o.optionsAction[0] = string(OptionIdle) o.cursor = 0 o.isTabSelected = false @@ -142,6 +173,7 @@ func (o *Options) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (o *Options) updateCursor(cursor int) { if cursor < len(o.options) { o.cursor = cursor + o.showAreYouSure() go o.resetOptionsWithOriginal() } }