mirror of
https://github.com/zoriya/bubbles.git
synced 2026-08-16 01:43:21 +00:00
feat(table): create table bubble from a lip gloss table
This commit is contained in:
+100
-1
@@ -2,6 +2,7 @@ package table
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image/color"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
@@ -256,6 +257,104 @@ func TestSetBorder(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewFromTemplate(t *testing.T) {
|
||||
// Using Pokemon example from https://github.com/charmbracelet/lipgloss.
|
||||
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]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]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"),
|
||||
}
|
||||
|
||||
pokemonHeaders := []string{"#", "Name", "Type 1", "Type 2", "Japanese", "Official Rom."}
|
||||
pokemonData := [][]string{
|
||||
{"1", "Bulbasaur", "Grass", "Poison", "フシギダネ", "Fushigidane"},
|
||||
{"2", "Ivysaur", "Grass", "Poison", "フシギソウ", "Fushigisou"},
|
||||
{"3", "Venusaur", "Grass", "Poison", "フシギバナ", "Fushigibana"},
|
||||
{"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"},
|
||||
}
|
||||
|
||||
lipglossTable := table.New().
|
||||
Border(lipgloss.NormalBorder()).
|
||||
BorderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("238"))).
|
||||
Headers(pokemonHeaders...).
|
||||
Width(80).
|
||||
Rows(pokemonData...).
|
||||
StyleFunc(func(row, col int) lipgloss.Style {
|
||||
if row == table.HeaderRow {
|
||||
return headerStyle
|
||||
}
|
||||
|
||||
if pokemonData[row][1] == "Pikachu" {
|
||||
return selectedStyle
|
||||
}
|
||||
|
||||
even := row%2 == 0
|
||||
|
||||
switch col {
|
||||
case 2, 3: // Type 1 + 2
|
||||
c := typeColors
|
||||
if even {
|
||||
c = dimTypeColors
|
||||
}
|
||||
|
||||
color := c[fmt.Sprint(pokemonData[row][col])]
|
||||
return baseStyle.Foreground(color)
|
||||
}
|
||||
|
||||
if even {
|
||||
return baseStyle.Foreground(lipgloss.Color("245"))
|
||||
}
|
||||
return baseStyle.Foreground(lipgloss.Color("252"))
|
||||
})
|
||||
|
||||
bubblesTable := NewFromTemplate(lipglossTable, headers, pokemonData)
|
||||
golden.RequireEqual(t, []byte(bubblesTable.View()))
|
||||
}
|
||||
|
||||
// Examples
|
||||
|
||||
func ExampleOption() {
|
||||
@@ -295,7 +394,7 @@ func ExampleOption() {
|
||||
//│ 5 São Paulo Brazil 22,429,800 │
|
||||
//│ … … … … │
|
||||
//└───────────────────────────────────────────┘
|
||||
}
|
||||
}
|
||||
|
||||
func ExampleModel_SetRows() {
|
||||
var niceMargins = lipgloss.NewStyle().Padding(0, 1)
|
||||
|
||||
Reference in New Issue
Block a user