Skip to content

Commit

Permalink
fix: fix rendering issue for viewport sizing
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Mar 4, 2022
1 parent f1b4892 commit 96ff1f0
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 47 deletions.
2 changes: 1 addition & 1 deletion cmd/project-management/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func projectPrompt(pr models.ProjectRepository) models.Project {
}

func OpenSqlite() *gorm.DB {
db, err := gorm.Open(sqlite.Open("new.db"), &gorm.Config{})
db, err := gorm.Open(sqlite.Open("new.db"), &gorm.Config{})
if err != nil {
log.Fatalf("unable to open database: %v", err)
}
Expand Down
3 changes: 3 additions & 0 deletions frontend/commands.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package frontend

import (
"log"

"github.com/bashbunni/project-management/models"
"github.com/bashbunni/project-management/utils"
tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -38,6 +40,7 @@ func (m model) createEntryCmd(activeProject uint, er *models.GormEntryRepository
p.ReleaseTerminal()
err := er.CreateEntry(utils.CaptureInputFromFile(), activeProject)
if err != nil {
log.Print(err)
return errMsg{err}
}
p.RestoreTerminal()
Expand Down
21 changes: 1 addition & 20 deletions frontend/entry_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,18 @@ package frontend

import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/lipgloss"
)

func (m model) handleEntriesList(msg tea.Msg, cmds []tea.Cmd, cmd tea.Cmd) (model, tea.Cmd) {
switch msg := msg.(type) {
case updateEntryListMsg:
// update vp.SetContent
case tea.WindowSizeMsg:
// TODO: why isn't this working?
if !m.ready {
m.viewport = viewport.New(msg.Width, msg.Height-2)
m.initEntries()
m.viewport.YPosition = 1
m.ready = true
}
m.viewport.Width = msg.Width
m.viewport.Height = msg.Height
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keymap.create):
cmds = append(cmds, m.createEntryCmd(m.activeProjectID, m.er))
cmds = append(cmds, m.createEntryCmd(m.getActiveProjectID(), m.er))
case key.Matches(msg, m.keymap.back):
m.state = "viewProjectList"
case msg.String() == "ctrl+c":
Expand All @@ -46,13 +34,6 @@ func (m *model) initEntries() error {
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("62")).
PaddingRight(2)
renderer, err := glamour.NewTermRenderer(
glamour.WithStylePath("dracula"),
glamour.WithAutoStyle(),
)
if err != nil {
return err
}
content, err := getEntryMessagesByProjectIDAsSingleString(m.getActiveProjectID(), m.er)
if err != nil {
return err
Expand Down
6 changes: 2 additions & 4 deletions frontend/project_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ func (m model) handleProjectList(msg tea.Msg, cmds []tea.Cmd, cmd tea.Cmd) (mode
m.list, cmd = m.list.Update(msg)
cmds = append(cmds, cmd)
}
case tea.WindowSizeMsg:
top, right, bottom, left := docStyle.GetMargin()
m.list.SetSize(msg.Width-left-right, msg.Height-top-bottom-1)
}
return m, tea.Batch(cmds...)
}
Expand Down Expand Up @@ -114,7 +111,8 @@ func initProjectView(items []list.Item, input textinput.Model, pr *models.GormPr
m.keymap.back,
}
}
m.viewport = viewport.New(28, 40)

m.viewport = viewport.New(8, 8)
err := m.initEntries()
if err != nil {
log.Fatal(err)
Expand Down
62 changes: 43 additions & 19 deletions frontend/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,28 @@ import (
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/lipgloss"
)

var docStyle = lipgloss.NewStyle().Margin(1, 2)
var helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("241")).Render

var p *tea.Program
var (
p *tea.Program
renderer *glamour.TermRenderer
)

// implements tea.Model (Init, Update, View)
type model struct {
state string
viewport viewport.Model
list list.Model
input textinput.Model
pr *models.GormProjectRepository
er *models.GormEntryRepository
keymap keymap
mode string
activeProjectID uint
ready bool
state string
viewport viewport.Model
list list.Model
input textinput.Model
pr *models.GormProjectRepository
er *models.GormEntryRepository
keymap keymap
mode string
}

type keymap struct {
Expand All @@ -44,25 +46,35 @@ type keymap struct {

// The entry point for the UI. Initializes the model.
func StartTea(pr models.GormProjectRepository, er models.GormEntryRepository) {
if os.Getenv("HELP_DEBUG") != "" {
if f, err := tea.LogToFile("debug.log", "help"); err != nil {
fmt.Println("Couldn't open a file for logging:", err)
os.Exit(1)
} else {
defer f.Close()
}
}

input := textinput.New()
input.Prompt = "$ "
input.Placeholder = "Project name..."
input.CharLimit = 250
input.Width = 50

var err error
renderer, err = glamour.NewTermRenderer(
glamour.WithStylePath("dracula"),
)
if err != nil {
panic(err)
}

projects, err := pr.GetAllProjects()
if err != nil {
log.Fatal(err)
}
items := projectsToItems(projects)
m := initProjectView(items, input, &pr, &er)
if os.Getenv("HELP_DEBUG") != "" {
if f, err := tea.LogToFile("debug.log", "help"); err != nil {
fmt.Println("Couldn't open a file for logging:", err)
os.Exit(1)
} else {
defer f.Close()
}
}
p = tea.NewProgram(m)
p.EnterAltScreen()
if err := p.Start(); err != nil {
Expand All @@ -78,6 +90,18 @@ func (m model) Init() tea.Cmd {
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
var cmd tea.Cmd

switch msg := msg.(type) {
case tea.WindowSizeMsg:
// update entry view size
m.viewport.Width = msg.Width
m.viewport.Height = msg.Height - 8

// update list size
top, right, bottom, left := docStyle.GetMargin()
m.list.SetSize(msg.Width-left-right, msg.Height-top-bottom-1)
}

switch m.state {
case "viewProjectList":
return m.handleProjectList(msg, cmds, cmd)
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ replace github.com/bashbunni/project-management/models => ./models

require (
github.com/charmbracelet/bubbles v0.10.2
github.com/charmbracelet/bubbletea v0.19.3
github.com/charmbracelet/bubbletea v0.20.1-0.20220301222809-b96d11cbbb86
github.com/charmbracelet/glamour v0.5.0
github.com/charmbracelet/lipgloss v0.4.0
github.com/pkg/errors v0.9.1
Expand Down Expand Up @@ -40,5 +40,3 @@ require (
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
)

replace github.com/charmbracelet/bubbletea => /Users/bashbunni/bubbletea
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuP
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/charmbracelet/bubbles v0.10.2 h1:VK1Q7nnBMDFTlrMmvBgE9nidtU5udsIcZvFXvjE2Cfk=
github.com/charmbracelet/bubbles v0.10.2/go.mod h1:jOA+DUF1rjZm7gZHcNyIVW+YrBPALKfpGVdJu8UiJsA=
github.com/charmbracelet/bubbletea v0.19.3/go.mod h1:VuXF2pToRxDUHcBUcPmCRUHRvFATM4Ckb/ql1rBl3KA=
github.com/charmbracelet/bubbletea v0.20.1-0.20220301222809-b96d11cbbb86 h1:Yq8huZoBRoQ3GjyuyiCNxS+ZfpBfh/PviynQ2hw3D0g=
github.com/charmbracelet/bubbletea v0.20.1-0.20220301222809-b96d11cbbb86/go.mod h1:HzsvMPPhvPTYUeAs6ts2/UIybIEzTuikLVvtF+QlQX8=
github.com/charmbracelet/glamour v0.5.0 h1:wu15ykPdB7X6chxugG/NNfDUbyyrCLV9XBalj5wdu3g=
github.com/charmbracelet/glamour v0.5.0/go.mod h1:9ZRtG19AUIzcTm7FGLGbq3D5WKQ5UyZBbQsMQN0XIqc=
github.com/charmbracelet/harmonica v0.1.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
github.com/charmbracelet/lipgloss v0.4.0 h1:768h64EFkGUr8V5yAKV7/Ta0NiVceiPaV+PphaW1K9g=
github.com/charmbracelet/lipgloss v0.4.0/go.mod h1:vmdkHvce7UzX6xkyf4cca8WlwdQ5RQr8fzta+xl7BOM=
github.com/containerd/console v1.0.2/go.mod h1:ytZPjGgY2oeTkAONYafi2kSj0aYggsf8acV1PGKCbzQ=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -82,6 +86,7 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down

0 comments on commit 96ff1f0

Please sign in to comment.