Skip to content

Commit

Permalink
Merge pull request #34 from termkit/improve-status-messages
Browse files Browse the repository at this point in the history
Show are you sure? message when switch between options
  • Loading branch information
canack authored Jan 24, 2024
2 parents 90338d0 + a824c2a commit 5f1d808
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
6 changes: 3 additions & 3 deletions internal/terminal/handler/ghrepository/ghrepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions internal/terminal/handler/ghworkflow/ghworkflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
34 changes: 33 additions & 1 deletion internal/terminal/handler/taboptions/taboptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = "┤"
Expand All @@ -68,6 +74,7 @@ func NewOptions() *Options {
optionsAction: initialOptionsAction,
optionsWithFunc: optionsWithFunc,
status: OptionWait,
modelError: modelError,
}
}

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
}
}
Expand Down

0 comments on commit 5f1d808

Please sign in to comment.