Skip to content

Commit

Permalink
Make the code better
Browse files Browse the repository at this point in the history
  • Loading branch information
canack committed Jun 26, 2024
1 parent b5da129 commit 5b4f1dc
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 94 deletions.
5 changes: 3 additions & 2 deletions internal/terminal/handler/ghrepository/ghrepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions internal/terminal/handler/ghtrigger/ghtrigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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.")
Expand Down
5 changes: 3 additions & 2 deletions internal/terminal/handler/ghworkflow/ghworkflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
121 changes: 42 additions & 79 deletions internal/terminal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)"),
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions internal/terminal/handler/header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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))
Expand Down
9 changes: 5 additions & 4 deletions internal/terminal/handler/information/information.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

Expand All @@ -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:
Expand Down

0 comments on commit 5b4f1dc

Please sign in to comment.