Skip to content

Commit

Permalink
Minor enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
canack committed Jun 29, 2024
1 parent ed66581 commit e16e299
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 260 deletions.
52 changes: 26 additions & 26 deletions internal/terminal/handler/error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ func SetupModelError() ModelError {
}
}

func (m *ModelError) View() string {
var windowStyle = lipgloss.NewStyle().BorderStyle(lipgloss.RoundedBorder())

doc := strings.Builder{}
if m.HaveError() {
windowStyle = ts.WindowStyleError.Width(*hdltypes.ScreenWidth)
doc.WriteString(windowStyle.Render(m.ViewError()))
return doc.String()
}

switch m.messageType {
case MessageTypeDefault:
windowStyle = ts.WindowStyleDefault.Width(*hdltypes.ScreenWidth)
case MessageTypeProgress:
windowStyle = ts.WindowStyleProgress.Width(*hdltypes.ScreenWidth)
case MessageTypeSuccess:
windowStyle = ts.WindowStyleSuccess.Width(*hdltypes.ScreenWidth)
default:
windowStyle = ts.WindowStyleDefault.Width(*hdltypes.ScreenWidth)
}

doc.WriteString(windowStyle.Render(m.ViewMessage()))

return doc.String()
}

func (m *ModelError) SetError(err error) {
m.err = err
}
Expand Down Expand Up @@ -107,29 +133,3 @@ func (m *ModelError) ViewMessage() string {
doc.WriteString(m.message)
return doc.String()
}

func (m *ModelError) View() string {
var windowStyle = lipgloss.NewStyle().BorderStyle(lipgloss.RoundedBorder())

doc := strings.Builder{}
if m.HaveError() {
windowStyle = ts.WindowStyleError.Width(*hdltypes.ScreenWidth)
doc.WriteString(windowStyle.Render(m.ViewError()))
return doc.String()
}

switch m.messageType {
case MessageTypeDefault:
windowStyle = ts.WindowStyleDefault.Width(*hdltypes.ScreenWidth)
case MessageTypeProgress:
windowStyle = ts.WindowStyleProgress.Width(*hdltypes.ScreenWidth)
case MessageTypeSuccess:
windowStyle = ts.WindowStyleSuccess.Width(*hdltypes.ScreenWidth)
default:
windowStyle = ts.WindowStyleDefault.Width(*hdltypes.ScreenWidth)
}

doc.WriteString(windowStyle.Render(m.ViewMessage()))

return doc.String()
}
142 changes: 70 additions & 72 deletions internal/terminal/handler/ghrepository/ghrepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ type ModelGithubRepository struct {
searchTableGithubRepository table.Model
modelError *hdlerror.ModelError

modelTabOptions tea.Model
actualModelTabOptions *taboptions.Options
modelTabOptions *taboptions.Options

textInput textinput.Model
}
Expand Down Expand Up @@ -122,7 +121,6 @@ func SetupModelGithubRepository(viewport *viewport.Model, githubUseCase gu.UseCa
modelError: &modelError,
SelectedRepository: selectedRepository,
modelTabOptions: tabOptions,
actualModelTabOptions: tabOptions,
textInput: ti,
syncRepositoriesContext: context.Background(),
cancelSyncRepositories: func() {},
Expand All @@ -145,75 +143,9 @@ func (m *ModelGithubRepository) Init() tea.Cmd {
m.modelError.SetSuccessMessage("Opened in browser")
}

m.actualModelTabOptions.AddOption("Open in browser", openInBrowser)
m.modelTabOptions.AddOption("Open in browser", openInBrowser)

return nil
}

func (m *ModelGithubRepository) syncRepositories(ctx context.Context) {
m.modelError.ResetError() // reset previous errors
m.actualModelTabOptions.SetStatus(taboptions.OptionWait)
m.modelError.SetProgressMessage("Fetching repositories...")

// delete all rows
m.tableGithubRepository.SetRows([]table.Row{})
m.searchTableGithubRepository.SetRows([]table.Row{})

repositories, err := m.github.ListRepositories(ctx, gu.ListRepositoriesInput{
Limit: 100, // limit to 100 repositories
Page: 5, // page 1 to page 5, at summary we fetch 500 repositories
Sort: domain.SortByUpdated,
})
if errors.Is(err, context.Canceled) {
return
} else if err != nil {
m.modelError.SetError(err)
m.modelError.SetErrorMessage("Repositories cannot be listed")
return
}

if len(repositories.Repositories) == 0 {
m.actualModelTabOptions.SetStatus(taboptions.OptionNone)
m.modelError.SetDefaultMessage("No repositories found")
m.textInput.Blur()
return
}

tableRowsGithubRepository := make([]table.Row, 0, len(repositories.Repositories))
for _, repository := range repositories.Repositories {
tableRowsGithubRepository = append(tableRowsGithubRepository,
table.Row{repository.Name, repository.DefaultBranch, strconv.Itoa(repository.Stars), strconv.Itoa(len(repository.Workflows))})
}

m.tableGithubRepository.SetRows(tableRowsGithubRepository)
m.searchTableGithubRepository.SetRows(tableRowsGithubRepository)

// set cursor to 0
m.tableGithubRepository.SetCursor(0)
m.searchTableGithubRepository.SetCursor(0)

m.tableReady = true
//m.updateSearchBarSuggestions()
m.textInput.Focus()
m.modelError.SetSuccessMessage("Repositories fetched")
go m.Update(m) // update model
}

func (m *ModelGithubRepository) handleTableInputs(_ context.Context) {
if !m.tableReady {
return
}

// To avoid go routine leak
selectedRow := m.tableGithubRepository.SelectedRow()

// Synchronize selected repository name with parent model
if len(selectedRow) > 0 && selectedRow[0] != "" {
m.SelectedRepository.RepositoryName = selectedRow[0]
m.SelectedRepository.BranchName = selectedRow[1]
}

m.actualModelTabOptions.SetStatus(taboptions.OptionIdle)
return tea.Batch(m.modelTabOptions.Init())
}

func (m *ModelGithubRepository) Update(msg tea.Msg) (*ModelGithubRepository, tea.Cmd) {
Expand Down Expand Up @@ -278,7 +210,73 @@ func (m *ModelGithubRepository) View() string {
doc := strings.Builder{}
doc.WriteString(baseStyle.Render(m.tableGithubRepository.View()))

return lipgloss.JoinVertical(lipgloss.Top, doc.String(), m.viewSearchBar(), m.actualModelTabOptions.View())
return lipgloss.JoinVertical(lipgloss.Top, doc.String(), m.viewSearchBar(), m.modelTabOptions.View())
}

func (m *ModelGithubRepository) syncRepositories(ctx context.Context) {
m.modelError.ResetError() // reset previous errors
m.modelTabOptions.SetStatus(taboptions.OptionWait)
m.modelError.SetProgressMessage("Fetching repositories...")

// delete all rows
m.tableGithubRepository.SetRows([]table.Row{})
m.searchTableGithubRepository.SetRows([]table.Row{})

repositories, err := m.github.ListRepositories(ctx, gu.ListRepositoriesInput{
Limit: 100, // limit to 100 repositories
Page: 5, // page 1 to page 5, at summary we fetch 500 repositories
Sort: domain.SortByUpdated,
})
if errors.Is(err, context.Canceled) {
return
} else if err != nil {
m.modelError.SetError(err)
m.modelError.SetErrorMessage("Repositories cannot be listed")
return
}

if len(repositories.Repositories) == 0 {
m.modelTabOptions.SetStatus(taboptions.OptionNone)
m.modelError.SetDefaultMessage("No repositories found")
m.textInput.Blur()
return
}

tableRowsGithubRepository := make([]table.Row, 0, len(repositories.Repositories))
for _, repository := range repositories.Repositories {
tableRowsGithubRepository = append(tableRowsGithubRepository,
table.Row{repository.Name, repository.DefaultBranch, strconv.Itoa(repository.Stars), strconv.Itoa(len(repository.Workflows))})
}

m.tableGithubRepository.SetRows(tableRowsGithubRepository)
m.searchTableGithubRepository.SetRows(tableRowsGithubRepository)

// set cursor to 0
m.tableGithubRepository.SetCursor(0)
m.searchTableGithubRepository.SetCursor(0)

m.tableReady = true
//m.updateSearchBarSuggestions()
m.textInput.Focus()
m.modelError.SetSuccessMessage("Repositories fetched")
go m.Update(m) // update model
}

func (m *ModelGithubRepository) handleTableInputs(_ context.Context) {
if !m.tableReady {
return
}

// To avoid go routine leak
selectedRow := m.tableGithubRepository.SelectedRow()

// Synchronize selected repository name with parent model
if len(selectedRow) > 0 && selectedRow[0] != "" {
m.SelectedRepository.RepositoryName = selectedRow[0]
m.SelectedRepository.BranchName = selectedRow[1]
}

m.modelTabOptions.SetStatus(taboptions.OptionIdle)
}

func (m *ModelGithubRepository) viewSearchBar() string {
Expand Down
72 changes: 34 additions & 38 deletions internal/terminal/handler/ghtrigger/ghtrigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import (
"context"
"errors"
"fmt"
"github.com/termkit/gama/internal/terminal/handler/header"
"slices"
"strings"
"time"

"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/table"
"github.com/charmbracelet/bubbles/textinput"
Expand All @@ -17,25 +12,29 @@ import (
"github.com/charmbracelet/lipgloss"
gu "github.com/termkit/gama/internal/github/usecase"
hdlerror "github.com/termkit/gama/internal/terminal/handler/error"
"github.com/termkit/gama/internal/terminal/handler/ghworkflowhistory"
"github.com/termkit/gama/internal/terminal/handler/header"
hdltypes "github.com/termkit/gama/internal/terminal/handler/types"
"github.com/termkit/gama/pkg/workflow"
"slices"
"strings"
"time"
)

type ModelGithubTrigger struct {
// current handler's properties
syncWorkflowContext context.Context
cancelSyncWorkflow context.CancelFunc
workflowContent *workflow.Pretty
tableReady bool
isTriggerable bool
forceUpdateWorkflowHistory *bool
optionInit bool
optionCursor int
optionValues []string
currentOption string
selectedWorkflow string
selectedRepositoryName string
triggerFocused bool
syncWorkflowContext context.Context
cancelSyncWorkflow context.CancelFunc
workflowContent *workflow.Pretty
tableReady bool
isTriggerable bool
optionInit bool
optionCursor int
optionValues []string
currentOption string
selectedWorkflow string
selectedRepositoryName string
triggerFocused bool

// shared properties
SelectedRepository *hdltypes.SelectedRepository
Expand All @@ -56,7 +55,7 @@ type ModelGithubTrigger struct {
tableTrigger table.Model
}

func SetupModelGithubTrigger(viewport *viewport.Model, githubUseCase gu.UseCase, selectedRepository *hdltypes.SelectedRepository, forceUpdateWorkflowHistory *bool) *ModelGithubTrigger {
func SetupModelGithubTrigger(viewport *viewport.Model, githubUseCase gu.UseCase, selectedRepository *hdltypes.SelectedRepository) *ModelGithubTrigger {
var tableRowsTrigger []table.Row

tableTrigger := table.New(
Expand All @@ -83,24 +82,23 @@ func SetupModelGithubTrigger(viewport *viewport.Model, githubUseCase gu.UseCase,
ti.CharLimit = 72

return &ModelGithubTrigger{
Viewport: viewport,
header: header.NewHeader(viewport),
forceUpdateWorkflowHistory: forceUpdateWorkflowHistory,
Help: help.New(),
Keys: keys,
github: githubUseCase,
SelectedRepository: selectedRepository,
modelError: hdlerror.SetupModelError(),
tableTrigger: tableTrigger,
textInput: ti,
syncWorkflowContext: context.Background(),
cancelSyncWorkflow: func() {},
Viewport: viewport,
header: header.NewHeader(viewport),
Help: help.New(),
Keys: keys,
github: githubUseCase,
SelectedRepository: selectedRepository,
modelError: hdlerror.SetupModelError(),
tableTrigger: tableTrigger,
textInput: ti,
syncWorkflowContext: context.Background(),
cancelSyncWorkflow: func() {},
}
}

func (m *ModelGithubTrigger) Init() tea.Cmd {
m.modelError.SetDefaultMessage("No workflow contents found.")
return textinput.Blink
return tea.Batch(textinput.Blink)
}

func (m *ModelGithubTrigger) Update(msg tea.Msg) (*ModelGithubTrigger, tea.Cmd) {
Expand Down Expand Up @@ -172,6 +170,9 @@ func (m *ModelGithubTrigger) Update(msg tea.Msg) (*ModelGithubTrigger, tea.Cmd)
case "enter", tea.KeyEnter.String():
if m.triggerFocused && m.isTriggerable {
go m.triggerWorkflow()
return m, func() tea.Msg {
return ghworkflowhistory.UpdateWorkflowHistoryMsg{UpdateAfter: time.Second * 5}
}
}
}
}
Expand Down Expand Up @@ -601,7 +602,7 @@ func (m *ModelGithubTrigger) triggerWorkflow() {

time.Sleep(1 * time.Second)
m.modelError.SetProgressMessage("Switching to workflow history tab...")
time.Sleep(1 * time.Second)
time.Sleep(1500 * time.Millisecond)

// move these operations under new function named "resetTabSettings"
m.workflowContent = nil // reset workflow content
Expand All @@ -610,11 +611,6 @@ func (m *ModelGithubTrigger) triggerWorkflow() {
m.optionValues = nil // reset option values
m.selectedRepositoryName = "" // reset selected repository name

go func() {
time.Sleep(1 * time.Second)
*m.forceUpdateWorkflowHistory = true // force update workflow history
}()

m.header.SetCurrentTab(2) // switch tab to workflow history
}

Expand Down
Loading

0 comments on commit e16e299

Please sign in to comment.