main.go
This file
Path
examples/table-resize/main.go
163 lines · 4.7 KiB · Go
Last changed by
Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
History of this fileTake it
1package main23import (4 "fmt"5 "image/color"6 "os"78 tea "charm.land/bubbletea/v2"9 "charm.land/lipgloss/v2"10 "charm.land/lipgloss/v2/table"11)1213// Pokemon types.14const (15 None = ""16 Bug = "Bug"17 Electric = "Electric"18 Fire = "Fire"19 Flying = "Flying"20 Grass = "Grass"21 Ground = "Ground"22 Normal = "Normal"23 Poison = "Poison"24 Water = "Water"25)2627type model struct {28 table *table.Table29}3031func (m model) Init() tea.Cmd { return nil }3233func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {34 var cmd tea.Cmd35 switch msg := msg.(type) {36 case tea.WindowSizeMsg:37 m.table = m.table.Width(msg.Width)38 m.table = m.table.Height(msg.Height)39 case tea.KeyPressMsg:40 switch msg.String() {41 case "q", "ctrl+c":42 return m, tea.Quit43 case "enter":44 }45 }46 return m, cmd47}4849func (m model) View() tea.View {50 v := tea.NewView("\n" + m.table.String() + "\n")51 v.AltScreen = true52 return v53}5455func main() {56 baseStyle := lipgloss.NewStyle().Padding(0, 1)57 headerStyle := baseStyle.Foreground(lipgloss.Color("252")).Bold(true)58 selectedStyle := baseStyle.Foreground(lipgloss.Color("#01BE85")).Background(lipgloss.Color("#00432F"))59 typeColors := map[string]color.Color{60 Bug: lipgloss.Color("#D7FF87"),61 Electric: lipgloss.Color("#FDFF90"),62 Fire: lipgloss.Color("#FF7698"),63 Flying: lipgloss.Color("#FF87D7"),64 Grass: lipgloss.Color("#75FBAB"),65 Ground: lipgloss.Color("#FF875F"),66 Normal: lipgloss.Color("#929292"),67 Poison: lipgloss.Color("#7D5AFC"),68 Water: lipgloss.Color("#00E2C7"),69 }70 dimTypeColors := map[string]color.Color{71 Bug: lipgloss.Color("#97AD64"),72 Electric: lipgloss.Color("#FCFF5F"),73 Fire: lipgloss.Color("#BA5F75"),74 Flying: lipgloss.Color("#C97AB2"),75 Grass: lipgloss.Color("#59B980"),76 Ground: lipgloss.Color("#C77252"),77 Normal: lipgloss.Color("#727272"),78 Poison: lipgloss.Color("#634BD0"),79 Water: lipgloss.Color("#439F8E"),80 }81 headers := []string{"#", "NAME", "TYPE 1", "TYPE 2", "JAPANESE", "OFFICIAL ROM."}82 rows := [][]string{83 {"1", "Bulbasaur", Grass, Poison, "フシギダネ", "Bulbasaur"},84 {"2", "Ivysaur", Grass, Poison, "フシギソウ", "Ivysaur"},85 {"3", "Venusaur", Grass, Poison, "フシギバナ", "Venusaur"},86 {"4", "Charmander", Fire, None, "ヒトカゲ", "Hitokage"},87 {"5", "Charmeleon", Fire, None, "リザード", "Lizardo"},88 {"6", "Charizard", Fire, Flying, "リザードン", "Lizardon"},89 {"7", "Squirtle", Water, None, "ゼニガメ", "Zenigame"},90 {"8", "Wartortle", Water, None, "カメール", "Kameil"},91 {"9", "Blastoise", Water, None, "カメックス", "Kamex"},92 {"10", "Caterpie", Bug, None, "キャタピー", "Caterpie"},93 {"11", "Metapod", Bug, None, "トランセル", "Trancell"},94 {"12", "Butterfree", Bug, Flying, "バタフリー", "Butterfree"},95 {"13", "Weedle", Bug, Poison, "ビードル", "Beedle"},96 {"14", "Kakuna", Bug, Poison, "コクーン", "Cocoon"},97 {"15", "Beedrill", Bug, Poison, "スピアー", "Spear"},98 {"16", "Pidgey", Normal, Flying, "ポッポ", "Poppo"},99 {"17", "Pidgeotto", Normal, Flying, "ピジョン", "Pigeon"},100 {"18", "Pidgeot", Normal, Flying, "ピジョット", "Pigeot"},101 {"19", "Rattata", Normal, None, "コラッタ", "Koratta"},102 {"20", "Raticate", Normal, None, "ラッタ", "Ratta"},103 {"21", "Spearow", Normal, Flying, "オニスズメ", "Onisuzume"},104 {"22", "Fearow", Normal, Flying, "オニドリル", "Onidrill"},105 {"23", "Ekans", Poison, None, "アーボ", "Arbo"},106 {"24", "Arbok", Poison, None, "アーボック", "Arbok"},107 {"25", "Pikachu", Electric, None, "ピカチュウ", "Pikachu"},108 {"26", "Raichu", Electric, None, "ライチュウ", "Raichu"},109 {"27", "Sandshrew", Ground, None, "サンド", "Sand"},110 {"28", "Sandslash", Ground, None, "サンドパン", "Sandpan"},111 }112113 t := table.New().114 Headers(headers...).115 Rows(rows...).116 Border(lipgloss.NormalBorder()).117 BorderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("238"))).118 StyleFunc(func(row, col int) lipgloss.Style {119 if row == 0 {120 return headerStyle121 }122123 rowIndex := row - 1124 if rowIndex < 0 || rowIndex >= len(rows) {125 return baseStyle126 }127128 if rows[rowIndex][1] == "Pikachu" {129 return selectedStyle130 }131132 even := row%2 == 0133134 switch col {135 case 2, 3: // Type 1 + 2136 c := typeColors137 if even {138 c = dimTypeColors139 }140141 if col >= len(rows[rowIndex]) {142 return baseStyle143 }144145 color, ok := c[rows[rowIndex][col]]146 if !ok {147 return baseStyle148 }149 return baseStyle.Foreground(color)150 }151152 if even {153 return baseStyle.Foreground(lipgloss.Color("245"))154 }155 return baseStyle.Foreground(lipgloss.Color("252"))156 }).157 Border(lipgloss.ThickBorder())158159 if _, err := tea.NewProgram(model{t}).Run(); err != nil {160 fmt.Println("Error running program:", err)161 os.Exit(1)162 }163}