Skip to content
Myr

docs: add example of Lip Gloss table resizing (#838)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Maas Lalani · committed by GitHub · parent aaf740e · Browse files · 1 file+136 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+136 added−0 removed

  • other:+136 added−0 removed

1 file+136 added−0 removed

added:examples/table-resize/main.go+136 added
1package main23import (4 "fmt"5 "os"67 tea "github.com/charmbracelet/bubbletea"8 "github.com/charmbracelet/lipgloss"9 "github.com/charmbracelet/lipgloss/table"10)1112type model struct {13 table *table.Table14}1516func (m model) Init() tea.Cmd { return nil }1718func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {19 var cmd tea.Cmd20 switch msg := msg.(type) {21 case tea.WindowSizeMsg:22 m.table = m.table.Width(msg.Width)23 m.table = m.table.Height(msg.Height)24 case tea.KeyMsg:25 switch msg.String() {26 case "q", "ctrl+c":27 return m, tea.Quit28 case "enter":29 }30 }31 return m, cmd32}3334func (m model) View() string {35 return "\n" + m.table.String() + "\n"36}3738func main() {39 re := lipgloss.NewRenderer(os.Stdout)40 baseStyle := re.NewStyle().Padding(0, 1)41 headerStyle := baseStyle.Copy().Foreground(lipgloss.Color("252")).Bold(true)42 selectedStyle := baseStyle.Copy().Foreground(lipgloss.Color("#01BE85")).Background(lipgloss.Color("#00432F"))43 typeColors := map[string]lipgloss.Color{44 "Bug": lipgloss.Color("#D7FF87"),45 "Electric": lipgloss.Color("#FDFF90"),46 "Fire": lipgloss.Color("#FF7698"),47 "Flying": lipgloss.Color("#FF87D7"),48 "Grass": lipgloss.Color("#75FBAB"),49 "Ground": lipgloss.Color("#FF875F"),50 "Normal": lipgloss.Color("#929292"),51 "Poison": lipgloss.Color("#7D5AFC"),52 "Water": lipgloss.Color("#00E2C7"),53 }54 dimTypeColors := map[string]lipgloss.Color{55 "Bug": lipgloss.Color("#97AD64"),56 "Electric": lipgloss.Color("#FCFF5F"),57 "Fire": lipgloss.Color("#BA5F75"),58 "Flying": lipgloss.Color("#C97AB2"),59 "Grass": lipgloss.Color("#59B980"),60 "Ground": lipgloss.Color("#C77252"),61 "Normal": lipgloss.Color("#727272"),62 "Poison": lipgloss.Color("#634BD0"),63 "Water": lipgloss.Color("#439F8E"),64 }65 headers := []string{"#", "NAME", "TYPE 1", "TYPE 2", "JAPANESE", "OFFICIAL ROM."}66 rows := [][]string{67 {"1", "Bulbasaur", "Grass", "Poison", "フシギダネ", "Bulbasaur"},68 {"2", "Ivysaur", "Grass", "Poison", "フシギソウ", "Ivysaur"},69 {"3", "Venusaur", "Grass", "Poison", "フシギバナ", "Venusaur"},70 {"4", "Charmander", "Fire", "", "ヒトカゲ", "Hitokage"},71 {"5", "Charmeleon", "Fire", "", "リザード", "Lizardo"},72 {"6", "Charizard", "Fire", "Flying", "リザードン", "Lizardon"},73 {"7", "Squirtle", "Water", "", "ゼニガメ", "Zenigame"},74 {"8", "Wartortle", "Water", "", "カメール", "Kameil"},75 {"9", "Blastoise", "Water", "", "カメックス", "Kamex"},76 {"10", "Caterpie", "Bug", "", "キャタピー", "Caterpie"},77 {"11", "Metapod", "Bug", "", "トランセル", "Trancell"},78 {"12", "Butterfree", "Bug", "Flying", "バタフリー", "Butterfree"},79 {"13", "Weedle", "Bug", "Poison", "ビードル", "Beedle"},80 {"14", "Kakuna", "Bug", "Poison", "コクーン", "Cocoon"},81 {"15", "Beedrill", "Bug", "Poison", "スピアー", "Spear"},82 {"16", "Pidgey", "Normal", "Flying", "ポッポ", "Poppo"},83 {"17", "Pidgeotto", "Normal", "Flying", "ピジョン", "Pigeon"},84 {"18", "Pidgeot", "Normal", "Flying", "ピジョット", "Pigeot"},85 {"19", "Rattata", "Normal", "", "コラッタ", "Koratta"},86 {"20", "Raticate", "Normal", "", "ラッタ", "Ratta"},87 {"21", "Spearow", "Normal", "Flying", "オニスズメ", "Onisuzume"},88 {"22", "Fearow", "Normal", "Flying", "オニドリル", "Onidrill"},89 {"23", "Ekans", "Poison", "", "アーボ", "Arbo"},90 {"24", "Arbok", "Poison", "", "アーボック", "Arbok"},91 {"25", "Pikachu", "Electric", "", "ピカチュウ", "Pikachu"},92 {"26", "Raichu", "Electric", "", "ライチュウ", "Raichu"},93 {"27", "Sandshrew", "Ground", "", "サンド", "Sand"},94 {"28", "Sandslash", "Ground", "", "サンドパン", "Sandpan"},95 }9697 t := table.New().98 Headers(headers...).99 Rows(rows...).100 Border(lipgloss.NormalBorder()).101 BorderStyle(re.NewStyle().Foreground(lipgloss.Color("238"))).102 StyleFunc(func(row, col int) lipgloss.Style {103 if row == 0 {104 return headerStyle105 }106107 if rows[row-1][1] == "Pikachu" {108 return selectedStyle109 }110111 even := row%2 == 0112113 switch col {114 case 2, 3: // Type 1 + 2115 c := typeColors116 if even {117 c = dimTypeColors118 }119120 color := c[fmt.Sprint(rows[row-1][col])]121 return baseStyle.Copy().Foreground(color)122 }123124 if even {125 return baseStyle.Copy().Foreground(lipgloss.Color("245"))126 }127 return baseStyle.Copy().Foreground(lipgloss.Color("252"))128 }).129 Border(lipgloss.ThickBorder())130131 m := model{t}132 if _, err := tea.NewProgram(m, tea.WithAltScreen()).Run(); err != nil {133 fmt.Println("Error running program:", err)134 os.Exit(1)135 }136}