Skip to content

Commit

Permalink
Remove obsolete and unused code for better maintainability
Browse files Browse the repository at this point in the history
  • Loading branch information
canack committed Dec 17, 2024
1 parent 7e7b11a commit 30d6b06
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 56 deletions.
9 changes: 3 additions & 6 deletions header.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ func (h *header) Update(msg tea.Msg) (*header, tea.Cmd) {

h.calculateTitleLength()

h.updater.Update()

cmds = append(cmds, h.calculateTitleLength())
case tea.KeyMsg:
switch {
Expand All @@ -132,12 +130,8 @@ func (h *header) Update(msg tea.Msg) (*header, tea.Cmd) {
h.currentTab = min(h.currentTab+1, len(h.headers)-1)
}
}

h.updater.Update()
}

cmds = append(cmds, h.calculateTitleLength())

return h, tea.Batch(cmds...)
}

Expand Down Expand Up @@ -277,6 +271,7 @@ func (h *header) AddCommonHeader(key string, title string) {
title: title,
})
h.calculateTitleLength()
h.updater.Update()
}

// UpdateCommonHeader updates the header by the given key.
Expand All @@ -287,6 +282,7 @@ func (h *header) UpdateCommonHeader(key string, title string) {
}
}
h.calculateTitleLength()
h.updater.Update()
}

// DeleteCommonHeader deletes the header by the given key.
Expand All @@ -297,4 +293,5 @@ func (h *header) DeleteCommonHeader(key string) {
}
}
h.calculateTitleLength()
h.updater.Update()
}
61 changes: 41 additions & 20 deletions skeleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ func defaultSkeletonProperties() *skeletonProperties {
}
}

func (s *Skeleton) TriggerUpdate() {
s.updater.Update()
}

func (s *Skeleton) TriggerUpdateWithMsg(msg tea.Msg) {
s.updater.UpdateWithMsg(msg)
}

// SetBorderColor sets the border color of the Skeleton.
func (s *Skeleton) SetBorderColor(color string) *Skeleton {
s.header.SetBorderColor(color)
Expand Down Expand Up @@ -187,6 +195,18 @@ func (s *Skeleton) IsTabsLocked() bool {
return s.lockTabs
}

// AddPageMsg adds a new page to the Skeleton.
type AddPageMsg struct {
// Key is unique key of the page, it is used to identify the page
Key string

// Title is the title of the page, it is used to show the title on the header
Title string

// Page is the page model, it is used to show the content of the page
Page tea.Model
}

// AddPage adds a new page to the Skeleton.
func (s *Skeleton) AddPage(key string, title string, page tea.Model) *Skeleton {
// do not add if key already exists
Expand All @@ -199,7 +219,11 @@ func (s *Skeleton) AddPage(key string, title string, page tea.Model) *Skeleton {
s.header.AddCommonHeader(key, title)
s.pages = append(s.pages, page)

s.updater.Update()
s.updater.UpdateWithMsg(AddPageMsg{
Key: key,
Title: title,
Page: page,
})
return s
}

Expand All @@ -212,16 +236,9 @@ func (s *Skeleton) UpdatePageTitle(key string, title string) *Skeleton {

// DeletePage deletes the page by the given key.
func (s *Skeleton) DeletePage(key string) *Skeleton {
s.deletePage(key)
s.updater.Update()
return s
}

// deletePage deletes the page by the given key.
func (s *Skeleton) deletePage(key string) {
if len(s.pages) == 1 {
// skeleton should have at least one page
return
return s
}

// if active tab is about deleting tab, switch to the first tab
Expand All @@ -236,8 +253,11 @@ func (s *Skeleton) deletePage(key string) {
pages = append(pages, s.pages[i])
}
}

s.header.DeleteCommonHeader(key)
s.pages = pages
s.updater.Update()
return s
}

// AddWidget adds a new widget to the Skeleton.
Expand All @@ -250,17 +270,12 @@ func (s *Skeleton) AddWidget(key string, value string) *Skeleton {
// UpdateWidgetValue updates the Value content by the given key.
// Adds the widget if it doesn't exist.
func (s *Skeleton) UpdateWidgetValue(key string, value string) *Skeleton {
go func() {
// if widget not exists, add it
if s.widget.GetWidget(key) == nil {
s.AddWidget(key, value)
}

s.widget.updateWidgetContent(key, value)

s.updater.Update()
}()

// if widget not exists, add it
if s.widget.GetWidget(key) == nil {
s.AddWidget(key, value)
}
s.widget.updateWidgetContent(key, value)
s.updater.Update()
return s
}

Expand Down Expand Up @@ -371,8 +386,13 @@ func (s *Skeleton) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds = s.switchPage(cmds, "right")
}
cmds = s.updateSkeleton(msg, cmd, cmds)
case AddPageMsg:
cmds = append(cmds, msg.Page.Init()) // init the page
cmds = s.updateSkeleton(msg, cmd, cmds)
cmds = append(cmds, s.updater.Listen()) // listen to the update channel
case UpdateMsg:
// do nothing, just to trigger the update
cmds = s.updateSkeleton(msg, cmd, cmds)
cmds = append(cmds, s.updater.Listen()) // listen to the update channel
case HeaderSizeMsg:
s.termSizeNotEnoughToHandleHeaders = msg.NotEnoughToHandleHeaders
Expand All @@ -381,6 +401,7 @@ func (s *Skeleton) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

default:
cmds = s.updateSkeleton(msg, cmd, cmds)
cmds = append(cmds, s.updater.Listen()) // listen to the update channel
}

return s, tea.Batch(cmds...)
Expand Down
34 changes: 4 additions & 30 deletions widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,7 @@ func (w *widget) GetWidget(key string) *commonWidget {
func (w *widget) DeleteAllWidgets() {
w.widgets = nil
w.calculateWidgetLength()
}

type AddNewWidget struct {
Key string
Value string
}

type UpdateWidgetContent struct {
Key string
Value string
}

type DeleteWidget struct {
Key string
w.updater.Update()
}

func (w *widget) addNewWidget(key, value string) {
Expand All @@ -143,8 +130,8 @@ func (w *widget) addNewWidget(key, value string) {
Value: value,
})

w.updater.Update()
w.calculateWidgetLength()
w.updater.Update()
}

func (w *widget) updateWidgetContent(key, value string) {
Expand All @@ -153,8 +140,8 @@ func (w *widget) updateWidgetContent(key, value string) {
x.Value = value
}

w.updater.Update()
w.calculateWidgetLength()
w.updater.Update()
}

func (w *widget) deleteWidget(key string) {
Expand All @@ -165,8 +152,8 @@ func (w *widget) deleteWidget(key string) {
}
}

w.updater.Update()
w.calculateWidgetLength()
w.updater.Update()
}

type WidgetSizeMsg struct {
Expand All @@ -191,19 +178,6 @@ func (w *widget) Update(msg tea.Msg) (*widget, tea.Cmd) {
w.viewport.Height = msg.Height

cmds = append(cmds, w.calculateWidgetLength())
w.updater.Update()
case AddNewWidget:
w.addNewWidget(msg.Key, msg.Value)
w.updater.Update()
cmds = append(cmds, w.calculateWidgetLength())
case UpdateWidgetContent:
w.updateWidgetContent(msg.Key, msg.Value)
w.updater.Update()
cmds = append(cmds, w.calculateWidgetLength())
case DeleteWidget:
w.deleteWidget(msg.Key)
w.updater.Update()
cmds = append(cmds, w.calculateWidgetLength())
}

return w, tea.Batch(cmds...)
Expand Down

0 comments on commit 30d6b06

Please sign in to comment.