From 5b4f1dc22ca2a73c6755828f4b236bd4fc873ade Mon Sep 17 00:00:00 2001 From: Engin Date: Thu, 27 Jun 2024 00:16:30 +0300 Subject: [PATCH] Make the code better --- .../handler/ghrepository/ghrepository.go | 5 +- .../terminal/handler/ghtrigger/ghtrigger.go | 7 +- .../terminal/handler/ghworkflow/ghworkflow.go | 5 +- .../ghworkflowhistory/ghworkflowhistory.go | 5 +- internal/terminal/handler/handler.go | 121 ++++++------------ internal/terminal/handler/header/header.go | 5 +- .../handler/information/information.go | 9 +- 7 files changed, 63 insertions(+), 94 deletions(-) diff --git a/internal/terminal/handler/ghrepository/ghrepository.go b/internal/terminal/handler/ghrepository/ghrepository.go index 75892ea..30204b1 100644 --- a/internal/terminal/handler/ghrepository/ghrepository.go +++ b/internal/terminal/handler/ghrepository/ghrepository.go @@ -54,7 +54,7 @@ var baseStyle = lipgloss.NewStyle(). BorderStyle(lipgloss.NormalBorder()). BorderForeground(lipgloss.Color("240")) -func SetupModelGithubRepository(githubUseCase gu.UseCase, selectedRepository *hdltypes.SelectedRepository) *ModelGithubRepository { +func SetupModelGithubRepository(viewport *viewport.Model, githubUseCase gu.UseCase, selectedRepository *hdltypes.SelectedRepository) *ModelGithubRepository { var tableRowsGithubRepository []table.Row tableGithubRepository := table.New( @@ -114,6 +114,7 @@ func SetupModelGithubRepository(githubUseCase gu.UseCase, selectedRepository *hd tabOptions := taboptions.NewOptions(&modelError) return &ModelGithubRepository{ + Viewport: viewport, Help: help.New(), Keys: keys, github: githubUseCase, @@ -215,7 +216,7 @@ func (m *ModelGithubRepository) handleTableInputs(_ context.Context) { m.actualModelTabOptions.SetStatus(taboptions.OptionIdle) } -func (m *ModelGithubRepository) Update(msg tea.Msg) (tea.Model, tea.Cmd) { +func (m *ModelGithubRepository) Update(msg tea.Msg) (*ModelGithubRepository, tea.Cmd) { var cmds []tea.Cmd var cmd tea.Cmd diff --git a/internal/terminal/handler/ghtrigger/ghtrigger.go b/internal/terminal/handler/ghtrigger/ghtrigger.go index 9b9f3e8..c865d6f 100644 --- a/internal/terminal/handler/ghtrigger/ghtrigger.go +++ b/internal/terminal/handler/ghtrigger/ghtrigger.go @@ -56,7 +56,7 @@ type ModelGithubTrigger struct { tableTrigger table.Model } -func SetupModelGithubTrigger(githubUseCase gu.UseCase, selectedRepository *hdltypes.SelectedRepository, currentTab *int, forceUpdateWorkflowHistory *bool) *ModelGithubTrigger { +func SetupModelGithubTrigger(viewport *viewport.Model, githubUseCase gu.UseCase, selectedRepository *hdltypes.SelectedRepository, forceUpdateWorkflowHistory *bool) *ModelGithubTrigger { var tableRowsTrigger []table.Row tableTrigger := table.New( @@ -83,7 +83,8 @@ func SetupModelGithubTrigger(githubUseCase gu.UseCase, selectedRepository *hdlty ti.CharLimit = 72 return &ModelGithubTrigger{ - header: header.NewHeader(), + Viewport: viewport, + header: header.NewHeader(viewport), forceUpdateWorkflowHistory: forceUpdateWorkflowHistory, Help: help.New(), Keys: keys, @@ -102,7 +103,7 @@ func (m *ModelGithubTrigger) Init() tea.Cmd { return textinput.Blink } -func (m *ModelGithubTrigger) Update(msg tea.Msg) (tea.Model, tea.Cmd) { +func (m *ModelGithubTrigger) Update(msg tea.Msg) (*ModelGithubTrigger, tea.Cmd) { if m.SelectedRepository.WorkflowName == "" { m.modelError.Reset() m.modelError.SetDefaultMessage("No workflow selected.") diff --git a/internal/terminal/handler/ghworkflow/ghworkflow.go b/internal/terminal/handler/ghworkflow/ghworkflow.go index c79cf12..00a38e9 100644 --- a/internal/terminal/handler/ghworkflow/ghworkflow.go +++ b/internal/terminal/handler/ghworkflow/ghworkflow.go @@ -55,7 +55,7 @@ var baseStyle = lipgloss.NewStyle(). BorderStyle(lipgloss.NormalBorder()). BorderForeground(lipgloss.Color("240")) -func SetupModelGithubWorkflow(githubUseCase gu.UseCase, selectedRepository *hdltypes.SelectedRepository) *ModelGithubWorkflow { +func SetupModelGithubWorkflow(viewport *viewport.Model, githubUseCase gu.UseCase, selectedRepository *hdltypes.SelectedRepository) *ModelGithubWorkflow { var tableRowsTriggerableWorkflow []table.Row tableTriggerableWorkflow := table.New( @@ -81,6 +81,7 @@ func SetupModelGithubWorkflow(githubUseCase gu.UseCase, selectedRepository *hdlt tabOptions := taboptions.NewOptions(&modelError) return &ModelGithubWorkflow{ + Viewport: viewport, Help: help.New(), Keys: keys, github: githubUseCase, @@ -98,7 +99,7 @@ func (m *ModelGithubWorkflow) Init() tea.Cmd { return nil } -func (m *ModelGithubWorkflow) Update(msg tea.Msg) (tea.Model, tea.Cmd) { +func (m *ModelGithubWorkflow) Update(msg tea.Msg) (*ModelGithubWorkflow, tea.Cmd) { var cmd tea.Cmd if m.lastRepository != m.SelectedRepository.RepositoryName { diff --git a/internal/terminal/handler/ghworkflowhistory/ghworkflowhistory.go b/internal/terminal/handler/ghworkflowhistory/ghworkflowhistory.go index 1fa798a..dad05bb 100644 --- a/internal/terminal/handler/ghworkflowhistory/ghworkflowhistory.go +++ b/internal/terminal/handler/ghworkflowhistory/ghworkflowhistory.go @@ -54,7 +54,7 @@ var baseStyle = lipgloss.NewStyle(). BorderStyle(lipgloss.NormalBorder()). BorderForeground(lipgloss.Color("240")) -func SetupModelGithubWorkflowHistory(githubUseCase gu.UseCase, selectedRepository *hdltypes.SelectedRepository, forceUpdate *bool) *ModelGithubWorkflowHistory { +func SetupModelGithubWorkflowHistory(viewport *viewport.Model, githubUseCase gu.UseCase, selectedRepository *hdltypes.SelectedRepository, forceUpdate *bool) *ModelGithubWorkflowHistory { var tableRowsWorkflowHistory []table.Row tableWorkflowHistory := table.New( @@ -80,6 +80,7 @@ func SetupModelGithubWorkflowHistory(githubUseCase gu.UseCase, selectedRepositor tabOptions := taboptions.NewOptions(&modelError) return &ModelGithubWorkflowHistory{ + Viewport: viewport, Help: help.New(), Keys: keys, github: githubUseCase, @@ -178,7 +179,7 @@ func (m *ModelGithubWorkflowHistory) Init() tea.Cmd { return tea.Batch(m.modelTabOptions.Init()) } -func (m *ModelGithubWorkflowHistory) Update(msg tea.Msg) (tea.Model, tea.Cmd) { +func (m *ModelGithubWorkflowHistory) Update(msg tea.Msg) (*ModelGithubWorkflowHistory, tea.Cmd) { if m.lastRepository != m.SelectedRepository.RepositoryName { m.tableReady = false m.cancelSyncWorkflowHistory() // cancel previous sync diff --git a/internal/terminal/handler/handler.go b/internal/terminal/handler/handler.go index 80012e6..fec5745 100644 --- a/internal/terminal/handler/handler.go +++ b/internal/terminal/handler/handler.go @@ -27,25 +27,15 @@ type model struct { SelectedRepository *hdltypes.SelectedRepository // models - viewport viewport.Model + viewport *viewport.Model timer timer.Model - modelHeader *header.Header - - modelInfo tea.Model - actualModelInfo *hdlinfo.ModelInfo - - modelGithubRepository tea.Model - actualModelGithubRepository *hdlgithubrepo.ModelGithubRepository - - modelWorkflow tea.Model - directModelWorkflow *hdlWorkflow.ModelGithubWorkflow - - modelWorkflowHistory tea.Model - directModelWorkflowHistory *hdlworkflowhistory.ModelGithubWorkflowHistory - - modelTrigger tea.Model - actualModelTrigger *hdltrigger.ModelGithubTrigger + modelHeader *header.Header + modelInfo *hdlinfo.ModelInfo + modelGithubRepository *hdlgithubrepo.ModelGithubRepository + modelWorkflow *hdlWorkflow.ModelGithubWorkflow + modelWorkflowHistory *hdlworkflowhistory.ModelGithubWorkflowHistory + modelTrigger *hdltrigger.ModelGithubTrigger // keymap keys keyMap @@ -57,30 +47,17 @@ const ( ) func SetupTerminal(githubUseCase gu.UseCase, version pkgversion.Version) tea.Model { - var currentTab = new(int) - var forceUpdateWorkflowHistory = new(bool) - - selectedRepository := hdltypes.SelectedRepository{} + forceUpdateWorkflowHistory := new(bool) + vp := &viewport.Model{Width: minTerminalWidth, Height: minTerminalHeight} + selectedRepository := &hdltypes.SelectedRepository{} // setup models - hdlModelHeader := header.NewHeader() - hdlModelInfo := hdlinfo.SetupModelInfo(githubUseCase, version) - hdlModelGithubRepository := hdlgithubrepo.SetupModelGithubRepository(githubUseCase, &selectedRepository) - hdlModelWorkflowHistory := hdlworkflowhistory.SetupModelGithubWorkflowHistory(githubUseCase, &selectedRepository, forceUpdateWorkflowHistory) - hdlModelWorkflow := hdlWorkflow.SetupModelGithubWorkflow(githubUseCase, &selectedRepository) - hdlModelTrigger := hdltrigger.SetupModelGithubTrigger(githubUseCase, &selectedRepository, currentTab, forceUpdateWorkflowHistory) - - m := model{ - timer: timer.NewWithInterval(1<<63-1, time.Millisecond*200), - modelHeader: hdlModelHeader, - modelInfo: hdlModelInfo, actualModelInfo: hdlModelInfo, - SelectedRepository: &selectedRepository, - modelGithubRepository: hdlModelGithubRepository, actualModelGithubRepository: hdlModelGithubRepository, - modelWorkflowHistory: hdlModelWorkflowHistory, directModelWorkflowHistory: hdlModelWorkflowHistory, - modelWorkflow: hdlModelWorkflow, directModelWorkflow: hdlModelWorkflow, - modelTrigger: hdlModelTrigger, actualModelTrigger: hdlModelTrigger, - keys: keys, - } + hdlModelHeader := header.NewHeader(vp) + hdlModelInfo := hdlinfo.SetupModelInfo(vp, githubUseCase, version) + hdlModelGithubRepository := hdlgithubrepo.SetupModelGithubRepository(vp, githubUseCase, selectedRepository) + hdlModelWorkflowHistory := hdlworkflowhistory.SetupModelGithubWorkflowHistory(vp, githubUseCase, selectedRepository, forceUpdateWorkflowHistory) + hdlModelWorkflow := hdlWorkflow.SetupModelGithubWorkflow(vp, githubUseCase, selectedRepository) + hdlModelTrigger := hdltrigger.SetupModelGithubTrigger(vp, githubUseCase, selectedRepository, forceUpdateWorkflowHistory) hdlModelHeader.AddCommonHeader("Info", ts.TitleStyleInactive, ts.TitleStyleActive) hdlModelHeader.AddCommonHeader("Repository", ts.TitleStyleInactive, ts.TitleStyleActive) @@ -89,18 +66,23 @@ func SetupTerminal(githubUseCase gu.UseCase, version pkgversion.Version) tea.Mod hdlModelHeader.AddCommonHeader("Trigger", ts.TitleStyleInactive, ts.TitleStyleActive) hdlModelHeader.SetSpecialHeader("GAMA", ts.TitleStyleLiveModeOn, ts.TitleStyleLiveModeOff) - hdlModelHeader.Viewport = &m.viewport - hdlModelInfo.Viewport = &m.viewport - hdlModelGithubRepository.Viewport = &m.viewport - hdlModelWorkflowHistory.Viewport = &m.viewport - hdlModelWorkflow.Viewport = &m.viewport - hdlModelTrigger.Viewport = &m.viewport + m := model{ + viewport: vp, + timer: timer.NewWithInterval(1<<63-1, time.Millisecond*200), + modelHeader: hdlModelHeader, + modelInfo: hdlModelInfo, + SelectedRepository: selectedRepository, + modelGithubRepository: hdlModelGithubRepository, + modelWorkflowHistory: hdlModelWorkflowHistory, + modelWorkflow: hdlModelWorkflow, + modelTrigger: hdlModelTrigger, + keys: keys, + } return &m } func (m *model) Init() tea.Cmd { - m.viewport = viewport.Model{Width: minTerminalWidth, Height: minTerminalHeight} return tea.Batch( tea.EnterAltScreen, tea.SetWindowTitle("GitHub Actions Manager (GAMA)"), @@ -145,60 +127,41 @@ func (m *model) View() string { var mainDoc strings.Builder var helpDoc string var operationDoc string - var helpDocHeight int - var width = lipgloss.Width(strings.Repeat("-", m.viewport.Width)) - 4 + var width = lipgloss.Width(strings.Repeat("-", m.viewport.Width)) - 5 hdltypes.ScreenWidth = &width - dynamicWindowStyle := ts.WindowStyleCyan.Width(width).Height(m.viewport.Height - 20) - + dynamicWindowStyle := ts.WindowStyleCyan.Width(width).Height(m.viewport.Height - 22) helpWindowStyle := ts.WindowStyleHelp.Width(width) - operationWindowStyle := lipgloss.NewStyle() + mainDoc.WriteString(m.modelHeader.View() + "\n") switch m.modelHeader.GetCurrentTab() { case 0: - mainDoc.WriteString("\n" + m.modelHeader.View() + "\n") - mainDoc.WriteString(dynamicWindowStyle.Render(m.modelInfo.View())) - operationDoc = operationWindowStyle.Render(m.actualModelInfo.ViewStatus()) - helpDoc = helpWindowStyle.Render(m.actualModelInfo.ViewHelp()) + operationDoc = m.modelInfo.ViewStatus() + helpDoc = helpWindowStyle.Render(m.modelInfo.ViewHelp()) case 1: - mainDoc.WriteString("\n" + m.modelHeader.View() + "\n") - mainDoc.WriteString(dynamicWindowStyle.Render(m.modelGithubRepository.View())) - operationDoc = operationWindowStyle.Render(m.actualModelGithubRepository.ViewStatus()) - helpDoc = helpWindowStyle.Render(m.actualModelGithubRepository.ViewHelp()) + operationDoc = m.modelGithubRepository.ViewStatus() + helpDoc = helpWindowStyle.Render(m.modelGithubRepository.ViewHelp()) case 2: - mainDoc.WriteString("\n" + m.modelHeader.View() + "\n") - mainDoc.WriteString(dynamicWindowStyle.Render(m.modelWorkflowHistory.View())) - operationDoc = operationWindowStyle.Render(m.directModelWorkflowHistory.ViewStatus()) - helpDoc = helpWindowStyle.Render(m.directModelWorkflowHistory.ViewHelp()) + operationDoc = m.modelWorkflowHistory.ViewStatus() + helpDoc = helpWindowStyle.Render(m.modelWorkflowHistory.ViewHelp()) case 3: - mainDoc.WriteString("\n" + m.modelHeader.View() + "\n") - mainDoc.WriteString(dynamicWindowStyle.Render(m.modelWorkflow.View())) - operationDoc = operationWindowStyle.Render(m.directModelWorkflow.ViewStatus()) - helpDoc = helpWindowStyle.Render(m.directModelWorkflow.ViewHelp()) + operationDoc = m.modelWorkflow.ViewStatus() + helpDoc = helpWindowStyle.Render(m.modelWorkflow.ViewHelp()) case 4: - mainDoc.WriteString("\n" + m.modelHeader.View() + "\n") - mainDoc.WriteString(dynamicWindowStyle.Render(m.modelTrigger.View())) - operationDoc = operationWindowStyle.Render(m.actualModelTrigger.ViewStatus()) - helpDoc = helpWindowStyle.Render(m.actualModelTrigger.ViewHelp()) + operationDoc = m.modelTrigger.ViewStatus() + helpDoc = helpWindowStyle.Render(m.modelTrigger.ViewHelp()) } mainDocContent := ts.DocStyle.Render(mainDoc.String()) - - mainDocHeight := strings.Count(mainDocContent, "\n") - helpDocHeight = strings.Count(helpDoc, "\n") - errorDocHeight := strings.Count(operationDoc, "\n") - requiredNewlinesForPadding := m.viewport.Height - mainDocHeight - helpDocHeight - errorDocHeight - padding := strings.Repeat("\n", max(0, requiredNewlinesForPadding)) - informationPane := lipgloss.JoinVertical(lipgloss.Top, operationDoc, helpDoc) - return mainDocContent + padding + informationPane + return lipgloss.JoinVertical(lipgloss.Top, mainDocContent, informationPane) } func (m *model) handleTabContent(cmd tea.Cmd, msg tea.Msg) tea.Cmd { diff --git a/internal/terminal/handler/header/header.go b/internal/terminal/handler/header/header.go index 74d310a..2cd294e 100644 --- a/internal/terminal/handler/header/header.go +++ b/internal/terminal/handler/header/header.go @@ -48,9 +48,10 @@ var ( ) // NewHeader returns a new Header. -func NewHeader() *Header { +func NewHeader(viewport *viewport.Model) *Header { once.Do(func() { h = &Header{ + Viewport: viewport, currentTab: 0, lockTabs: true, keys: keys, @@ -155,7 +156,7 @@ func (h *Header) View() string { var specialHeader string specialHeader = h.specialHeaders[0].header - renderedTitles := make([]string, 0, len(h.commonHeaders)) + var renderedTitles []string for i, title := range h.commonHeaders { if i == h.currentTab { renderedTitles = append(renderedTitles, title.activeStyle.Render(title.header)) diff --git a/internal/terminal/handler/information/information.go b/internal/terminal/handler/information/information.go index afb6510..055dc78 100644 --- a/internal/terminal/handler/information/information.go +++ b/internal/terminal/handler/information/information.go @@ -54,15 +54,16 @@ var ( applicationDescription string ) -func SetupModelInfo(githubUseCase gu.UseCase, version pkgversion.Version) *ModelInfo { +func SetupModelInfo(viewport *viewport.Model, githubUseCase gu.UseCase, version pkgversion.Version) *ModelInfo { modelError := hdlerror.SetupModelError() - hdlModelHeader := header.NewHeader() + hdlModelHeader := header.NewHeader(viewport) s := spinner.New() s.Spinner = spinner.Pulse s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("120")) return &ModelInfo{ + Viewport: viewport, modelHeader: hdlModelHeader, github: githubUseCase, version: version, @@ -87,7 +88,7 @@ func (m *ModelInfo) checkUpdates(ctx context.Context) { if err != nil { m.modelError.SetError(err) m.modelError.SetErrorMessage("failed to check updates") - newVersionAvailableMsg = fmt.Sprintf("failed to check updates: %v\nPlease visit: %s", err, releaseURL) + newVersionAvailableMsg = fmt.Sprintf("failed to check updates.\nPlease visit: %s", releaseURL) return } @@ -98,7 +99,7 @@ func (m *ModelInfo) checkUpdates(ctx context.Context) { go m.Update(m) } -func (m *ModelInfo) Update(msg tea.Msg) (tea.Model, tea.Cmd) { +func (m *ModelInfo) Update(msg tea.Msg) (*ModelInfo, tea.Cmd) { var cmd tea.Cmd switch msg := msg.(type) { case tea.WindowSizeMsg: