Skip to content

Commit

Permalink
Fixed up everything with the viewport for puzzles and leaderboards
Browse files Browse the repository at this point in the history
  • Loading branch information
DaltonSW committed Dec 3, 2024
1 parent e2af1d5 commit 5805451
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 65 deletions.
28 changes: 13 additions & 15 deletions internal/resources/LeaderboardView.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"golang.org/x/term"
)

type LeaderboardModel struct {
content string
viewport viewport.Model
title string
ready bool
}

type ViewableLB interface {
Expand All @@ -27,6 +27,7 @@ func NewLeaderboardViewport(content, title string) {
m := LeaderboardModel{
content: content,
title: title,
ready: false,
}

p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion())
Expand All @@ -39,7 +40,7 @@ func NewLeaderboardViewport(content, title string) {
func (m LeaderboardModel) Init() tea.Cmd {
log.Debug("'Init' function")

return func() tea.Msg { return initMsg(0) }
return nil
}

func (m LeaderboardModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Expand All @@ -49,19 +50,6 @@ func (m LeaderboardModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
)

switch msg := msg.(type) {
case initMsg:
width, height, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
return m, nil
}

headerHeight := lipgloss.Height(m.headerView())

m.viewport = viewport.New(min(ViewportWidth, width), height-headerHeight)
m.viewport.YPosition = headerHeight
m.viewport.HighPerformanceRendering = UseHighPerformanceRenderer
m.viewport.SetContent(m.content)
m.viewport.YPosition = headerHeight + 1

case tea.KeyMsg:
switch msg.String() {
Expand All @@ -75,6 +63,16 @@ func (m LeaderboardModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.viewport.Width = min(ViewportWidth, msg.Width)
m.viewport.Height = msg.Height - headerHeight

if !m.ready {

m.viewport = viewport.New(min(ViewportWidth, msg.Width), msg.Height-headerHeight)
m.viewport.YPosition = headerHeight
m.viewport.HighPerformanceRendering = UseHighPerformanceRenderer
m.viewport.SetContent(m.content)
// m.viewport.YPosition = headerHeight + 1
m.ready = true
}

if UseHighPerformanceRenderer {
cmds = append(cmds, viewport.Sync(m.viewport))
}
Expand Down
77 changes: 52 additions & 25 deletions internal/resources/Puzzle.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,18 @@ func (p *Puzzle) GetUserInput() ([]byte, error) {

// GetPrettyPageData parses the puzzle's stored information and displays it in a visually pleasing way.
func (p *Puzzle) GetPrettyPageData() []string {
sOut := p.ArticleOne
var sOut []string
sOut = append(sOut, lipgloss.NewStyle().BorderStyle(lipgloss.RoundedBorder()).Render(" - Part One - "))
sOut = append(sOut, "\n")

sOut = append(sOut, p.ArticleOne...)

if p.AnswerOne != "" {
sOut = append(sOut, "Answer: "+p.AnswerOne)
}

if len(p.ArticleTwo) != 0 {
sOut = append(sOut, "\n\n")
sOut = append(sOut, "\n"+lipgloss.NewStyle().BorderStyle(lipgloss.RoundedBorder()).Render(" --- Part Two --- "))
sOut = append(sOut, "\n\n"+lipgloss.NewStyle().BorderStyle(lipgloss.RoundedBorder()).Render(" - Part Two - "))
sOut = append(sOut, "\n")
sOut = append(sOut, p.ArticleTwo...)
sOut = append(sOut, "\n")
Expand Down Expand Up @@ -307,10 +310,10 @@ func (p *Puzzle) processPageContents(mainContents *goquery.Selection) {
if outStr != "" {
if p.AnswerOne == "" {
log.Debug("Answer found!", "year", p.Year, "day", p.Day, "answer", outStr)
p.AnswerOne = outStr
p.AnswerOne = styles.CodeStyle.Render(outStr)
} else {
log.Debug("Answer found!", "year", p.Year, "day", p.Day, "answer", outStr)
p.AnswerTwo = outStr
p.AnswerTwo = styles.CodeStyle.Render(outStr) + "\n"
}
}
}
Expand All @@ -321,33 +324,57 @@ func getPrettyArticle(article *goquery.Selection) []string {
var articleOut []string

article.Contents().Each(func(i int, sel *goquery.Selection) {
if goquery.NodeName(sel) == "h2" {
switch goquery.NodeName(sel) {
case "h2":
return
case "p":
paraContents := getPrettySelection(sel)

articleOut = append(articleOut, wrapText(paraContents, ViewportWidth)+"\n\n")
case "ul":
sel.Find("li").Each(func(j int, s *goquery.Selection) {
articleOut = append(articleOut, " - "+wrapText(getPrettySelection(s), ViewportWidth-2)+"\n\n")
})
articleOut = append(articleOut)
case "pre":
// Extract the <code> content
preContent := sel.Find("code").Text()

// Split content into lines
lines := strings.Split(preContent, "\n")

for _, line := range lines {
if line != "" { // Ignore empty lines
articleOut = append(articleOut, styles.CodeStyle.Render(line)+"\n")
}
}
articleOut = append(articleOut, "\n") // Add spacing after the block
}
})

loopContents := ""
sel.Contents().Each(func(j int, s *goquery.Selection) {
// TODO: Try to fix links. Maybe try "termlink" module
return articleOut
}

// loopContents += s.Text()
//
if goquery.NodeName(s) == "em" {
if s.HasClass("star") {
loopContents += styles.StarStyle.Render(s.Text())
} else {
loopContents += styles.ItalStyle.Render(s.Text())
}
} else if goquery.NodeName(s) == "code" {
loopContents += styles.CodeStyle.Render(s.Text())
} else if goquery.NodeName(s) != "h2" {
loopContents += s.Text()
func getPrettySelection(sel *goquery.Selection) string {
selContents := ""
sel.Contents().Each(func(j int, s *goquery.Selection) {
switch goquery.NodeName(s) {
case "em":
if s.HasClass("star") {
selContents += styles.StarStyle.Render(s.Text())
} else {
selContents += styles.ItalStyle.Render(s.Text())
}
})

articleOut = append(articleOut, wrapText(loopContents, ViewportWidth)+"\n")
case "code":
selContents += styles.CodeStyle.Render(s.Text())
case "h2":
return
default:
selContents += s.Text()
}
})

return articleOut
return selContents
}

var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*[a-zA-Z]`)
Expand Down
36 changes: 15 additions & 21 deletions internal/resources/PuzzleView.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"go.dalton.dog/aocgo/internal/utils"
"golang.org/x/term"
)

type PuzzleModel struct {
Expand All @@ -22,6 +21,7 @@ type PuzzleModel struct {
help help.Model
keys helpKeymap
status string
ready bool
}

func NewPuzzleViewport(puzzle *Puzzle) {
Expand All @@ -32,6 +32,7 @@ func NewPuzzleViewport(puzzle *Puzzle) {
content: contentStr,
keys: helpKeys,
help: help.New(),
ready: false,
}

p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion())
Expand All @@ -43,7 +44,7 @@ func NewPuzzleViewport(puzzle *Puzzle) {
func (m PuzzleModel) Init() tea.Cmd {
log.Debug("'Init' function")

return func() tea.Msg { return initMsg(0) }
return nil
}
func (m PuzzleModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var (
Expand All @@ -52,22 +53,6 @@ func (m PuzzleModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
)

switch msg := msg.(type) {
case initMsg:
width, height, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
return m, nil
}

headerHeight := lipgloss.Height(m.headerView())
footerHeight := lipgloss.Height(m.footerView())
verticalMarginHeight := headerHeight + footerHeight

m.viewport = viewport.New(min(ViewportWidth, width), height-verticalMarginHeight)
m.viewport.YPosition = headerHeight
m.viewport.HighPerformanceRendering = UseHighPerformanceRenderer
m.viewport.SetContent(m.content)
m.viewport.YPosition = headerHeight + 1

case tea.KeyMsg:
switch msg.String() {
case "esc", "q", "ctrl+c":
Expand All @@ -84,12 +69,12 @@ func (m PuzzleModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if err != nil {
log.Fatal(err)
}
m.content = strings.Join(m.puzzle.GetPrettyPageData(), "\n")
m.viewport.SetContent(strings.Join(m.puzzle.GetPrettyPageData(), "\n"))
m.status = "Page refreshed!"
// Clear terminal
fmt.Print("\033[H\033[2J")

return m, func() tea.Msg { return initMsg(1) }
return m, nil
case "s":
out, err := os.Create("./input.txt")
if err != nil {
Expand All @@ -112,6 +97,15 @@ func (m PuzzleModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
footerHeight := lipgloss.Height(m.footerView())
verticalMarginHeight := headerHeight + footerHeight

if !m.ready {
m.viewport = viewport.New(min(ViewportWidth, msg.Width), msg.Height-verticalMarginHeight)
// m.viewport.YPosition = headerHeight
m.viewport.HighPerformanceRendering = UseHighPerformanceRenderer
m.viewport.SetContent(m.content)
// m.viewport.YPosition = headerHeight + 1
m.ready = true
}

m.viewport.Width = min(ViewportWidth, msg.Width)
m.viewport.Height = msg.Height - verticalMarginHeight

Expand All @@ -132,7 +126,7 @@ func (m PuzzleModel) View() string {
}

func (m PuzzleModel) headerView() string {
title := puzzleTitleStyle.Render(m.puzzle.Title)
title := titleStyle.Render(m.puzzle.Title)
line := strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(title)))
return lipgloss.JoinHorizontal(lipgloss.Center, title, line)
}
Expand Down
6 changes: 2 additions & 4 deletions internal/resources/viewShared.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ package resources

import "github.com/charmbracelet/lipgloss"

type initMsg int

var UseHighPerformanceRenderer = true
var UseHighPerformanceRenderer = false
var ViewportWidth = 80

var (
titleStyle = func() lipgloss.Style {
b := lipgloss.RoundedBorder()
b.Right = "├"
return lipgloss.NewStyle().BorderStyle(b).Padding(0, 1).Foreground(lipgloss.Color("#FFFF00")).Underline(true)
return lipgloss.NewStyle().BorderStyle(b).Padding(0, 1).Foreground(lipgloss.Color("#FFFF00"))
}()

infoStyle = func() lipgloss.Style {
Expand Down

0 comments on commit 5805451

Please sign in to comment.