Skip to content

Instantly share code, notes, and snippets.

@76creates
Created February 22, 2022 23:07
Show Gist options
  • Select an option

  • Save 76creates/8f529f2b975f3cd6dee1d2a70260d53e to your computer and use it in GitHub Desktop.

Select an option

Save 76creates/8f529f2b975f3cd6dee1d2a70260d53e to your computer and use it in GitHub Desktop.

Revisions

  1. 76creates renamed this gist Feb 22, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. 76creates created this gist Feb 22, 2022.
    125 changes: 125 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,125 @@
    package main

    import (
    tea "github.com/charmbracelet/bubbletea"
    "github.com/charmbracelet/lipgloss"
    "os"
    )

    type m struct {
    s lipgloss.Style
    x int
    y int
    rr []*r
    }
    type r struct {
    s lipgloss.Style
    cc []*c
    }
    type c struct {
    s lipgloss.Style
    val string
    }

    var (
    rBase = lipgloss.NewStyle().Background(lipgloss.Color("#6c5ce7"))
    rAlt = lipgloss.NewStyle().Background(lipgloss.Color("#0984e3"))
    sBase = lipgloss.NewStyle().Width(3).Height(1).Align(lipgloss.Center)
    sAlt = lipgloss.NewStyle().Width(3).Height(1).Align(lipgloss.Center).Background(lipgloss.Color("#74b9ff"))
    )

    func main() {
    _m := m{
    s: lipgloss.NewStyle().Bold(true),
    x: 0,
    y: 0,
    rr: []*r{
    {
    s: rBase,
    cc: []*c{
    {
    s: sBase,
    val: "a",
    },
    {
    s: sBase,
    val: "b",
    },
    },
    },
    {
    s: rBase,
    cc: []*c{
    {
    s: sBase,
    val: "c",
    },
    {
    s: sBase,
    val: "d",
    },
    },
    },
    },
    }
    p := tea.NewProgram(&_m)
    if err := p.Start(); err != nil {
    os.Exit(1)
    }
    }

    func (_m *m) Init() tea.Cmd {
    return nil
    }
    func (_m *m) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    switch msg := msg.(type) {

    // Is it a key press?
    case tea.KeyMsg:
    switch msg.String() {
    case "ctrl+c", "q":
    return _m, tea.Quit
    case "up":
    if _m.y > 0 {
    _m.y--
    }
    case "down":
    if _m.y < len(_m.rr)-1 {
    _m.y++
    }
    case "left":
    if _m.x > 0 {
    _m.x--
    }
    case "right":
    if _m.x+1 < len(_m.rr[_m.y].cc) {
    _m.x++
    }
    }
    }

    return _m, nil
    }

    func (_m *m) View() string {
    var rScreen []string
    for ir, r := range _m.rr {
    if ir == _m.y {
    r.s = rAlt
    } else {
    r.s = rBase
    }

    cScreen := []string{}
    for ic, c := range r.cc {
    if ir == _m.y && ic == _m.x {
    c.s = sAlt.Copy().Inherit(r.s)
    } else {
    c.s = sBase.Copy().Inherit(r.s)
    }
    cScreen = append(cScreen, c.s.Render(c.val))
    }
    rScreen = append(rScreen, lipgloss.JoinHorizontal(lipgloss.Top, cScreen...))
    }
    return lipgloss.JoinVertical(lipgloss.Left, rScreen...)
    }