From a282c7ca86234d7050cc440d70bf71364c04d956 Mon Sep 17 00:00:00 2001 From: Wesley Bos Date: Fri, 6 Sep 2024 16:02:53 +0200 Subject: [PATCH] fix: use pointers --- model/main_model.go | 11 +++++++---- model/table_model.go | 8 ++++---- model/unit_model.go | 16 ++++++++-------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/model/main_model.go b/model/main_model.go index 7571c78..ef511ae 100644 --- a/model/main_model.go +++ b/model/main_model.go @@ -1,17 +1,20 @@ package model -import tea "github.com/charmbracelet/bubbletea" +import ( + tea "github.com/charmbracelet/bubbletea" +) func NewMainModel() MainModel { m := MainModel{} - m.TableModel = NewTableModel(&m) + t := NewTableModel(&m) + m.TableModel = &t m.activeModel = m.TableModel return m } type MainModel struct { - TableModel Table - UnitModel Unit + TableModel *Table + UnitModel *Unit activeModel tea.Model } diff --git a/model/table_model.go b/model/table_model.go index 999e90f..faf5526 100644 --- a/model/table_model.go +++ b/model/table_model.go @@ -103,14 +103,14 @@ type Table struct { mainModel *MainModel } -func (m Table) Init() tea.Cmd { +func (m *Table) Init() tea.Cmd { return nil } -func (m Table) Update(msg tea.Msg) (tea.Model, tea.Cmd) { +func (m *Table) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmd tea.Cmd sortStringColumn := -1 - sortIntColumn := 0 + sortIntColumn := -1 reverse := false switch msg := msg.(type) { case tea.KeyMsg: @@ -191,6 +191,6 @@ func (m Table) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, cmd } -func (m Table) View() string { +func (m *Table) View() string { return baseStyle.Render(m.Table.View()) + "\n " + m.Table.HelpView() + "\n" } diff --git a/model/unit_model.go b/model/unit_model.go index 6410d18..d0b92dc 100644 --- a/model/unit_model.go +++ b/model/unit_model.go @@ -35,7 +35,7 @@ var ( } ) -func NewUnitModel(ref util.UnitRef, mainModel *MainModel) Unit { +func NewUnitModel(ref util.UnitRef, mainModel *MainModel) *Unit { m := Unit{} m.ref = ref m.mainModel = mainModel @@ -55,7 +55,7 @@ func NewUnitModel(ref util.UnitRef, mainModel *MainModel) Unit { m.sightRange = progress.New(progress.WithSolidFill("#C6C8C9"), progress.WithoutPercentage()) m.speed = progress.New(progress.WithSolidFill("#1175AE"), progress.WithoutPercentage()) - return m + return &m } type Unit struct { @@ -76,11 +76,11 @@ type Unit struct { speed progress.Model } -func (m Unit) Init() tea.Cmd { +func (m *Unit) Init() tea.Cmd { return nil } -func (m Unit) Update(msg tea.Msg) (tea.Model, tea.Cmd) { +func (m *Unit) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmd tea.Cmd switch msg := msg.(type) { case tea.KeyMsg: @@ -93,7 +93,7 @@ func (m Unit) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, cmd } -func (m Unit) RenderBar(labelWidth int, label string, progress string, maxValueWidth int, value string) string { +func (m *Unit) RenderBar(labelWidth int, label string, progress string, maxValueWidth int, value string) string { v := value for range maxValueWidth - len(value) { v = " " + v @@ -106,15 +106,15 @@ func (m Unit) RenderBar(labelWidth int, label string, progress string, maxValueW return padding.Render(lipgloss.JoinHorizontal(lipgloss.Top, bar...)) } -func (m Unit) PercentageWithBase(value int, base float64) float64 { +func (m *Unit) PercentageWithBase(value int, base float64) float64 { return m.PercentageWithBaseF(float64(value), base) } -func (m Unit) PercentageWithBaseF(value float64, base float64) float64 { +func (m *Unit) PercentageWithBaseF(value float64, base float64) float64 { return min(value/base, 100.0) / 100 } -func (m Unit) View() string { +func (m *Unit) View() string { var sections []string var titleRow []string