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:
+10
-8
@@ -119,17 +119,19 @@ func DefaultStyles() Styles {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO do we want this?
|
|
||||||
// NewFromTemplate lets you create a table [Model] from Lip Gloss' [table.Table].
|
// NewFromTemplate lets you create a table [Model] from Lip Gloss' [table.Table].
|
||||||
func NewFromTemplate(t *table.Table, rows [][]string, headers []string) *Model {
|
func NewFromTemplate(t *table.Table, headers []string, rows [][]string) *Model {
|
||||||
m := &Model{
|
m := &Model{
|
||||||
cursor: 0,
|
cursor: 0,
|
||||||
KeyMap: DefaultKeyMap(),
|
KeyMap: DefaultKeyMap(),
|
||||||
Help: help.New(),
|
Help: help.New(),
|
||||||
table: t,
|
table: t,
|
||||||
|
useStyleFunc: true,
|
||||||
}
|
}
|
||||||
m.SetRows(rows...)
|
// We can't get the rows and headers from the table, so the user needs to
|
||||||
m.SetHeaders(headers...)
|
// provide them as arguments.
|
||||||
|
m.rows = rows
|
||||||
|
m.headers = headers
|
||||||
|
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|||||||
+100
-1
@@ -2,6 +2,7 @@ package table
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"image/color"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"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
|
// Examples
|
||||||
|
|
||||||
func ExampleOption() {
|
func ExampleOption() {
|
||||||
@@ -295,7 +394,7 @@ func ExampleOption() {
|
|||||||
//│ 5 São Paulo Brazil 22,429,800 │
|
//│ 5 São Paulo Brazil 22,429,800 │
|
||||||
//│ … … … … │
|
//│ … … … … │
|
||||||
//└───────────────────────────────────────────┘
|
//└───────────────────────────────────────────┘
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleModel_SetRows() {
|
func ExampleModel_SetRows() {
|
||||||
var niceMargins = lipgloss.NewStyle().Padding(0, 1)
|
var niceMargins = lipgloss.NewStyle().Padding(0, 1)
|
||||||
|
|||||||
+32
@@ -0,0 +1,32 @@
|
|||||||
|
[38;5;238m┌[m[38;5;238m────────────[m[38;5;238m┬[m[38;5;238m────────────[m[38;5;238m┬[m[38;5;238m───────────[m[38;5;238m┬[m[38;5;238m───────────[m[38;5;238m┬[m[38;5;238m────────────[m[38;5;238m┬[m[38;5;238m───────────────[m[38;5;238m┐[m
|
||||||
|
[38;5;238m│[m [1;38;5;252m#[m [38;5;238m│[m [1;38;5;252mName[m [38;5;238m│[m [1;38;5;252mType 1[m [38;5;238m│[m [1;38;5;252mType 2[m [38;5;238m│[m [1;38;5;252mJapanese[m [38;5;238m│[m [1;38;5;252mOfficial Rom.[m [38;5;238m│[m
|
||||||
|
[38;5;238m├[m[38;5;238m────────────[m[38;5;238m┼[m[38;5;238m────────────[m[38;5;238m┼[m[38;5;238m───────────[m[38;5;238m┼[m[38;5;238m───────────[m[38;5;238m┼[m[38;5;238m────────────[m[38;5;238m┼[m[38;5;238m───────────────[m[38;5;238m┤[m
|
||||||
|
[38;5;238m│[m [38;5;245m1[m [38;5;238m│[m [38;5;245mBulbasaur[m [38;5;238m│[m [38;2;89;185;128mGrass[m [38;5;238m│[m [38;2;99;75;208mPoison[m [38;5;238m│[m [38;5;245mフシギダネ[m [38;5;238m│[m [38;5;245mFushigidane[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;252m2[m [38;5;238m│[m [38;5;252mIvysaur[m [38;5;238m│[m [38;2;117;251;171mGrass[m [38;5;238m│[m [38;2;125;90;252mPoison[m [38;5;238m│[m [38;5;252mフシギソウ[m [38;5;238m│[m [38;5;252mFushigisou[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;245m3[m [38;5;238m│[m [38;5;245mVenusaur[m [38;5;238m│[m [38;2;89;185;128mGrass[m [38;5;238m│[m [38;2;99;75;208mPoison[m [38;5;238m│[m [38;5;245mフシギバナ[m [38;5;238m│[m [38;5;245mFushigibana[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;252m4[m [38;5;238m│[m [38;5;252mCharmander[m [38;5;238m│[m [38;2;255;118;152mFire[m [38;5;238m│[m [38;5;238m│[m [38;5;252mヒトカゲ[m [38;5;238m│[m [38;5;252mHitokage[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;245m5[m [38;5;238m│[m [38;5;245mCharmeleon[m [38;5;238m│[m [38;2;186;95;117mFire[m [38;5;238m│[m [38;5;238m│[m [38;5;245mリザード[m [38;5;238m│[m [38;5;245mLizardo[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;252m6[m [38;5;238m│[m [38;5;252mCharizard[m [38;5;238m│[m [38;2;255;118;152mFire[m [38;5;238m│[m [38;2;255;135;215mFlying[m [38;5;238m│[m [38;5;252mリザードン[m [38;5;238m│[m [38;5;252mLizardon[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;245m7[m [38;5;238m│[m [38;5;245mSquirtle[m [38;5;238m│[m [38;2;67;159;142mWater[m [38;5;238m│[m [38;5;238m│[m [38;5;245mゼニガメ[m [38;5;238m│[m [38;5;245mZenigame[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;252m8[m [38;5;238m│[m [38;5;252mWartortle[m [38;5;238m│[m [38;2;0;226;199mWater[m [38;5;238m│[m [38;5;238m│[m [38;5;252mカメール[m [38;5;238m│[m [38;5;252mKameil[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;245m9[m [38;5;238m│[m [38;5;245mBlastoise[m [38;5;238m│[m [38;2;67;159;142mWater[m [38;5;238m│[m [38;5;238m│[m [38;5;245mカメックス[m [38;5;238m│[m [38;5;245mKamex[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;252m10[m [38;5;238m│[m [38;5;252mCaterpie[m [38;5;238m│[m [38;2;215;255;135mBug[m [38;5;238m│[m [38;5;238m│[m [38;5;252mキャタピー[m [38;5;238m│[m [38;5;252mCaterpie[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;245m11[m [38;5;238m│[m [38;5;245mMetapod[m [38;5;238m│[m [38;2;151;173;100mBug[m [38;5;238m│[m [38;5;238m│[m [38;5;245mトランセル[m [38;5;238m│[m [38;5;245mTrancell[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;252m12[m [38;5;238m│[m [38;5;252mButterfree[m [38;5;238m│[m [38;2;215;255;135mBug[m [38;5;238m│[m [38;2;255;135;215mFlying[m [38;5;238m│[m [38;5;252mバタフリー[m [38;5;238m│[m [38;5;252mButterfree[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;245m13[m [38;5;238m│[m [38;5;245mWeedle[m [38;5;238m│[m [38;2;151;173;100mBug[m [38;5;238m│[m [38;2;99;75;208mPoison[m [38;5;238m│[m [38;5;245mビードル[m [38;5;238m│[m [38;5;245mBeedle[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;252m14[m [38;5;238m│[m [38;5;252mKakuna[m [38;5;238m│[m [38;2;215;255;135mBug[m [38;5;238m│[m [38;2;125;90;252mPoison[m [38;5;238m│[m [38;5;252mコクーン[m [38;5;238m│[m [38;5;252mCocoon[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;245m15[m [38;5;238m│[m [38;5;245mBeedrill[m [38;5;238m│[m [38;2;151;173;100mBug[m [38;5;238m│[m [38;2;99;75;208mPoison[m [38;5;238m│[m [38;5;245mスピアー[m [38;5;238m│[m [38;5;245mSpear[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;252m16[m [38;5;238m│[m [38;5;252mPidgey[m [38;5;238m│[m [38;2;146;146;146mNormal[m [38;5;238m│[m [38;2;255;135;215mFlying[m [38;5;238m│[m [38;5;252mポッポ[m [38;5;238m│[m [38;5;252mPoppo[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;245m17[m [38;5;238m│[m [38;5;245mPidgeotto[m [38;5;238m│[m [38;2;114;114;114mNormal[m [38;5;238m│[m [38;2;201;122;178mFlying[m [38;5;238m│[m [38;5;245mピジョン[m [38;5;238m│[m [38;5;245mPigeon[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;252m18[m [38;5;238m│[m [38;5;252mPidgeot[m [38;5;238m│[m [38;2;146;146;146mNormal[m [38;5;238m│[m [38;2;255;135;215mFlying[m [38;5;238m│[m [38;5;252mピジョット[m [38;5;238m│[m [38;5;252mPigeot[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;245m19[m [38;5;238m│[m [38;5;245mRattata[m [38;5;238m│[m [38;2;114;114;114mNormal[m [38;5;238m│[m [38;5;238m│[m [38;5;245mコラッタ[m [38;5;238m│[m [38;5;245mKoratta[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;252m20[m [38;5;238m│[m [38;5;252mRaticate[m [38;5;238m│[m [38;2;146;146;146mNormal[m [38;5;238m│[m [38;5;238m│[m [38;5;252mラッタ[m [38;5;238m│[m [38;5;252mRatta[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;245m21[m [38;5;238m│[m [38;5;245mSpearow[m [38;5;238m│[m [38;2;114;114;114mNormal[m [38;5;238m│[m [38;2;201;122;178mFlying[m [38;5;238m│[m [38;5;245mオニスズメ[m [38;5;238m│[m [38;5;245mOnisuzume[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;252m22[m [38;5;238m│[m [38;5;252mFearow[m [38;5;238m│[m [38;2;146;146;146mNormal[m [38;5;238m│[m [38;2;255;135;215mFlying[m [38;5;238m│[m [38;5;252mオニドリル[m [38;5;238m│[m [38;5;252mOnidrill[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;245m23[m [38;5;238m│[m [38;5;245mEkans[m [38;5;238m│[m [38;2;99;75;208mPoison[m [38;5;238m│[m [38;5;238m│[m [38;5;245mアーボ[m [38;5;238m│[m [38;5;245mArbo[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;252m24[m [38;5;238m│[m [38;5;252mArbok[m [38;5;238m│[m [38;2;125;90;252mPoison[m [38;5;238m│[m [38;5;238m│[m [38;5;252mアーボック[m [38;5;238m│[m [38;5;252mArbok[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m[48;2;0;67;47m [m[38;2;1;190;133;48;2;0;67;47m25[m[48;2;0;67;47m [m[48;2;0;67;47m [m[38;5;238m│[m[48;2;0;67;47m [m[38;2;1;190;133;48;2;0;67;47mPikachu[m[48;2;0;67;47m [m[48;2;0;67;47m [m[38;5;238m│[m[48;2;0;67;47m [m[38;2;1;190;133;48;2;0;67;47mElectric[m[48;2;0;67;47m [m[48;2;0;67;47m [m[38;5;238m│[m[48;2;0;67;47m [m[38;2;1;190;133;48;2;0;67;47m[m[48;2;0;67;47m [m[48;2;0;67;47m [m[38;5;238m│[m[48;2;0;67;47m [m[38;2;1;190;133;48;2;0;67;47mピカチュウ[m[48;2;0;67;47m [m[38;5;238m│[m[48;2;0;67;47m [m[38;2;1;190;133;48;2;0;67;47mPikachu[m[48;2;0;67;47m [m[48;2;0;67;47m [m[38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;252m26[m [38;5;238m│[m [38;5;252mRaichu[m [38;5;238m│[m [38;2;253;255;144mElectric[m [38;5;238m│[m [38;5;238m│[m [38;5;252mライチュウ[m [38;5;238m│[m [38;5;252mRaichu[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;245m27[m [38;5;238m│[m [38;5;245mSandshrew[m [38;5;238m│[m [38;2;199;114;82mGround[m [38;5;238m│[m [38;5;238m│[m [38;5;245mサンド[m [38;5;238m│[m [38;5;245mSand[m [38;5;238m│[m
|
||||||
|
[38;5;238m│[m [38;5;252m28[m [38;5;238m│[m [38;5;252mSandslash[m [38;5;238m│[m [38;2;255;135;95mGround[m [38;5;238m│[m [38;5;238m│[m [38;5;252mサンドパン[m [38;5;238m│[m [38;5;252mSandpan[m [38;5;238m│[m
|
||||||
|
[38;5;238m└[m[38;5;238m────────────[m[38;5;238m┴[m[38;5;238m────────────[m[38;5;238m┴[m[38;5;238m───────────[m[38;5;238m┴[m[38;5;238m───────────[m[38;5;238m┴[m[38;5;238m────────────[m[38;5;238m┴[m[38;5;238m───────────────[m[38;5;238m┘[m
|
||||||
Reference in New Issue
Block a user