Skip to content

Commit

Permalink
docs(examples): update table-width example for Lip Gloss v2
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Oct 29, 2024
1 parent 9a4cd84 commit 600303e
Showing 1 changed file with 80 additions and 55 deletions.
135 changes: 80 additions & 55 deletions examples/table-resize/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,28 @@ package main

import (
"fmt"
"image/color"
"os"

tea "github.com/charmbracelet/bubbletea/v2"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
)

// Pokemon types.
const (
None = ""
Bug = "Bug"
Electric = "Electric"
Fire = "Fire"
Flying = "Flying"
Grass = "Grass"
Ground = "Ground"
Normal = "Normal"
Poison = "Poison"
Water = "Water"
)

type model struct {
table *table.Table
}
Expand Down Expand Up @@ -36,75 +51,79 @@ func (m model) View() string {
}

func main() {
re := lipgloss.NewRenderer(os.Stdout)
baseStyle := re.NewStyle().Padding(0, 1)
baseStyle := lipgloss.NewStyle().Padding(0, 1)
headerStyle := baseStyle.Foreground(lipgloss.Color("252")).Bold(true)
selectedStyle := baseStyle.Foreground(lipgloss.Color("#01BE85")).Background(lipgloss.Color("#00432F"))
typeColors := map[string]lipgloss.Color{
"Bug": lipgloss.Color("#D7FF87"),
"Electric": lipgloss.Color("#FDFF90"),
"Fire": lipgloss.Color("#FF7698"),
"Flying": lipgloss.Color("#FF87D7"),
"Grass": lipgloss.Color("#75FBAB"),
"Ground": lipgloss.Color("#FF875F"),
"Normal": lipgloss.Color("#929292"),
"Poison": lipgloss.Color("#7D5AFC"),
"Water": lipgloss.Color("#00E2C7"),
typeColors := map[string]color.Color{
Bug: lipgloss.Color("#D7FF87"),
Electric: lipgloss.Color("#FDFF90"),
Fire: lipgloss.Color("#FF7698"),
Flying: lipgloss.Color("#FF87D7"),
Grass: lipgloss.Color("#75FBAB"),
Ground: lipgloss.Color("#FF875F"),
Normal: lipgloss.Color("#929292"),
Poison: lipgloss.Color("#7D5AFC"),
Water: lipgloss.Color("#00E2C7"),
}
dimTypeColors := map[string]lipgloss.Color{
"Bug": lipgloss.Color("#97AD64"),
"Electric": lipgloss.Color("#FCFF5F"),
"Fire": lipgloss.Color("#BA5F75"),
"Flying": lipgloss.Color("#C97AB2"),
"Grass": lipgloss.Color("#59B980"),
"Ground": lipgloss.Color("#C77252"),
"Normal": lipgloss.Color("#727272"),
"Poison": lipgloss.Color("#634BD0"),
"Water": lipgloss.Color("#439F8E"),
dimTypeColors := map[string]color.Color{
Bug: lipgloss.Color("#97AD64"),
Electric: lipgloss.Color("#FCFF5F"),
Fire: lipgloss.Color("#BA5F75"),
Flying: lipgloss.Color("#C97AB2"),
Grass: lipgloss.Color("#59B980"),
Ground: lipgloss.Color("#C77252"),
Normal: lipgloss.Color("#727272"),
Poison: lipgloss.Color("#634BD0"),
Water: lipgloss.Color("#439F8E"),
}
headers := []string{"#", "NAME", "TYPE 1", "TYPE 2", "JAPANESE", "OFFICIAL ROM."}
rows := [][]string{
{"1", "Bulbasaur", "Grass", "Poison", "フシギダネ", "Bulbasaur"},
{"2", "Ivysaur", "Grass", "Poison", "フシギソウ", "Ivysaur"},
{"3", "Venusaur", "Grass", "Poison", "フシギバナ", "Venusaur"},
{"4", "Charmander", "Fire", "", "ヒトカゲ", "Hitokage"},
{"5", "Charmeleon", "Fire", "", "リザード", "Lizardo"},
{"6", "Charizard", "Fire", "Flying", "リザードン", "Lizardon"},
{"7", "Squirtle", "Water", "", "ゼニガメ", "Zenigame"},
{"8", "Wartortle", "Water", "", "カメール", "Kameil"},
{"9", "Blastoise", "Water", "", "カメックス", "Kamex"},
{"10", "Caterpie", "Bug", "", "キャタピー", "Caterpie"},
{"11", "Metapod", "Bug", "", "トランセル", "Trancell"},
{"12", "Butterfree", "Bug", "Flying", "バタフリー", "Butterfree"},
{"13", "Weedle", "Bug", "Poison", "ビードル", "Beedle"},
{"14", "Kakuna", "Bug", "Poison", "コクーン", "Cocoon"},
{"15", "Beedrill", "Bug", "Poison", "スピアー", "Spear"},
{"16", "Pidgey", "Normal", "Flying", "ポッポ", "Poppo"},
{"17", "Pidgeotto", "Normal", "Flying", "ピジョン", "Pigeon"},
{"18", "Pidgeot", "Normal", "Flying", "ピジョット", "Pigeot"},
{"19", "Rattata", "Normal", "", "コラッタ", "Koratta"},
{"20", "Raticate", "Normal", "", "ラッタ", "Ratta"},
{"21", "Spearow", "Normal", "Flying", "オニスズメ", "Onisuzume"},
{"22", "Fearow", "Normal", "Flying", "オニドリル", "Onidrill"},
{"23", "Ekans", "Poison", "", "アーボ", "Arbo"},
{"24", "Arbok", "Poison", "", "アーボック", "Arbok"},
{"25", "Pikachu", "Electric", "", "ピカチュウ", "Pikachu"},
{"26", "Raichu", "Electric", "", "ライチュウ", "Raichu"},
{"27", "Sandshrew", "Ground", "", "サンド", "Sand"},
{"28", "Sandslash", "Ground", "", "サンドパン", "Sandpan"},
{"1", "Bulbasaur", Grass, Poison, "フシギダネ", "Bulbasaur"},
{"2", "Ivysaur", Grass, Poison, "フシギソウ", "Ivysaur"},
{"3", "Venusaur", Grass, Poison, "フシギバナ", "Venusaur"},
{"4", "Charmander", Fire, None, "ヒトカゲ", "Hitokage"},
{"5", "Charmeleon", Fire, None, "リザード", "Lizardo"},
{"6", "Charizard", Fire, Flying, "リザードン", "Lizardon"},
{"7", "Squirtle", Water, None, "ゼニガメ", "Zenigame"},
{"8", "Wartortle", Water, None, "カメール", "Kameil"},
{"9", "Blastoise", Water, None, "カメックス", "Kamex"},
{"10", "Caterpie", Bug, None, "キャタピー", "Caterpie"},
{"11", "Metapod", Bug, None, "トランセル", "Trancell"},
{"12", "Butterfree", Bug, Flying, "バタフリー", "Butterfree"},
{"13", "Weedle", Bug, Poison, "ビードル", "Beedle"},
{"14", "Kakuna", Bug, Poison, "コクーン", "Cocoon"},
{"15", "Beedrill", Bug, Poison, "スピアー", "Spear"},
{"16", "Pidgey", Normal, Flying, "ポッポ", "Poppo"},
{"17", "Pidgeotto", Normal, Flying, "ピジョン", "Pigeon"},
{"18", "Pidgeot", Normal, Flying, "ピジョット", "Pigeot"},
{"19", "Rattata", Normal, None, "コラッタ", "Koratta"},
{"20", "Raticate", Normal, None, "ラッタ", "Ratta"},
{"21", "Spearow", Normal, Flying, "オニスズメ", "Onisuzume"},
{"22", "Fearow", Normal, Flying, "オニドリル", "Onidrill"},
{"23", "Ekans", Poison, None, "アーボ", "Arbo"},
{"24", "Arbok", Poison, None, "アーボック", "Arbok"},
{"25", "Pikachu", Electric, None, "ピカチュウ", "Pikachu"},
{"26", "Raichu", Electric, None, "ライチュウ", "Raichu"},
{"27", "Sandshrew", Ground, None, "サンド", "Sand"},
{"28", "Sandslash", Ground, None, "サンドパン", "Sandpan"},
}

t := table.New().
Headers(headers...).
Rows(rows...).
Border(lipgloss.NormalBorder()).
BorderStyle(re.NewStyle().Foreground(lipgloss.Color("238"))).
BorderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("238"))).
StyleFunc(func(row, col int) lipgloss.Style {
if row == 0 {
return headerStyle
}

if rows[row-1][1] == "Pikachu" {
rowIndex := row - 1
if rowIndex < 0 || rowIndex >= len(rows) {
return baseStyle
}

if rows[rowIndex][1] == "Pikachu" {
return selectedStyle
}

Expand All @@ -117,7 +136,14 @@ func main() {
c = dimTypeColors
}

color := c[fmt.Sprint(rows[row-1][col])]
if col >= len(rows[rowIndex]) {
return baseStyle
}

color, ok := c[rows[rowIndex][col]]
if !ok {
return baseStyle
}
return baseStyle.Foreground(color)
}

Expand All @@ -128,8 +154,7 @@ func main() {
}).
Border(lipgloss.ThickBorder())

m := model{t}
if _, err := tea.NewProgram(m, tea.WithAltScreen()).Run(); err != nil {
if _, err := tea.NewProgram(model{t}, tea.WithAltScreen()).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
Expand Down

0 comments on commit 600303e

Please sign in to comment.