-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
562 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"strings" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/evertras/bubble-table/table" | ||
) | ||
|
||
const ( | ||
columnKeyName = "name" | ||
columnKeyType = "type" | ||
columnKeyWins = "element" | ||
) | ||
|
||
type Model struct { | ||
simpleTable table.Model | ||
|
||
columnSortKey string | ||
sortDirection string | ||
} | ||
|
||
func NewModel() Model { | ||
return Model{ | ||
simpleTable: table.New([]table.Column{ | ||
table.NewColumn(columnKeyName, "Name", 13), | ||
table.NewColumn(columnKeyType, "Type", 13), | ||
table.NewColumn(columnKeyWins, "Wins", 5), | ||
}).WithRows([]table.Row{ | ||
table.NewRow(table.RowData{ | ||
columnKeyName: "ピカピカ", | ||
columnKeyType: "Pikachu", | ||
columnKeyWins: 4, | ||
}), | ||
table.NewRow(table.RowData{ | ||
columnKeyName: "Alphonse", | ||
columnKeyType: "Pikachu", | ||
columnKeyWins: 13, | ||
}), | ||
table.NewRow(table.RowData{ | ||
columnKeyName: "Burninator", | ||
columnKeyType: "Charmander", | ||
columnKeyWins: 8, | ||
}), | ||
table.NewRow(table.RowData{ | ||
columnKeyName: "Dihydrogen Monoxide", | ||
columnKeyType: "Squirtle", | ||
columnKeyWins: 31, | ||
}), | ||
}), | ||
} | ||
} | ||
|
||
func (m Model) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
var ( | ||
cmd tea.Cmd | ||
cmds []tea.Cmd | ||
) | ||
|
||
m.simpleTable, cmd = m.simpleTable.Update(msg) | ||
cmds = append(cmds, cmd) | ||
|
||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
switch msg.String() { | ||
case "ctrl+c", "esc", "q": | ||
cmds = append(cmds, tea.Quit) | ||
|
||
case "n": | ||
m.columnSortKey = columnKeyName | ||
m.simpleTable = m.simpleTable.SortByAsc(m.columnSortKey) | ||
|
||
case "t": | ||
m.columnSortKey = columnKeyType | ||
// Within the same type, order each by wins | ||
m.simpleTable = m.simpleTable.SortByAsc(m.columnSortKey).ThenSortByDesc(columnKeyWins) | ||
|
||
case "w": | ||
m.columnSortKey = columnKeyWins | ||
m.simpleTable = m.simpleTable.SortByDesc(m.columnSortKey) | ||
} | ||
} | ||
|
||
return m, tea.Batch(cmds...) | ||
} | ||
|
||
func (m Model) View() string { | ||
body := strings.Builder{} | ||
|
||
body.WriteString("A sorted simple default table\nSort by (n)ame, (t)ype, or (w)ins\nCurrently sorting by: " + m.columnSortKey + "\nPress q or ctrl+c to quit\n\n") | ||
|
||
body.WriteString(m.simpleTable.View()) | ||
|
||
return body.String() | ||
} | ||
|
||
func main() { | ||
p := tea.NewProgram(NewModel()) | ||
|
||
if err := p.Start(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package table | ||
|
||
import "time" | ||
|
||
// This is just a bunch of data type checks, so... no linting here | ||
// nolint: cyclop | ||
func asInt(data interface{}) (int64, bool) { | ||
switch val := data.(type) { | ||
case int: | ||
return int64(val), true | ||
|
||
case int8: | ||
return int64(val), true | ||
|
||
case int16: | ||
return int64(val), true | ||
|
||
case int32: | ||
return int64(val), true | ||
|
||
case int64: | ||
return val, true | ||
|
||
case uint: | ||
return int64(val), true | ||
|
||
case uint8: | ||
return int64(val), true | ||
|
||
case uint16: | ||
return int64(val), true | ||
|
||
case uint32: | ||
return int64(val), true | ||
|
||
case uint64: | ||
return int64(val), true | ||
|
||
case time.Duration: | ||
return int64(val), true | ||
|
||
case StyledCell: | ||
return asInt(val.Data) | ||
} | ||
|
||
return 0, false | ||
} | ||
|
||
func asNumber(data interface{}) (float64, bool) { | ||
switch val := data.(type) { | ||
case float32: | ||
return float64(val), true | ||
|
||
case float64: | ||
return val, true | ||
|
||
case StyledCell: | ||
return asNumber(val.Data) | ||
} | ||
|
||
intVal, isInt := asInt(data) | ||
|
||
return float64(intVal), isInt | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package table | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAsInt(t *testing.T) { | ||
check := func(data interface{}, isInt bool, expectedValue int64) { | ||
val, ok := asInt(data) | ||
assert.Equal(t, isInt, ok) | ||
assert.Equal(t, expectedValue, val) | ||
} | ||
|
||
check(3, true, 3) | ||
check(3.3, false, 0) | ||
check(int8(3), true, 3) | ||
check(int16(3), true, 3) | ||
check(int32(3), true, 3) | ||
check(int64(3), true, 3) | ||
check(uint(3), true, 3) | ||
check(uint8(3), true, 3) | ||
check(uint16(3), true, 3) | ||
check(uint32(3), true, 3) | ||
check(uint64(3), true, 3) | ||
check(StyledCell{Data: 3}, true, 3) | ||
check(time.Duration(3), true, 3) | ||
} | ||
|
||
func TestAsNumber(t *testing.T) { | ||
check := func(data interface{}, isFloat bool, expectedValue float64) { | ||
val, ok := asNumber(data) | ||
assert.Equal(t, isFloat, ok) | ||
assert.InDelta(t, expectedValue, val, 0.001) | ||
} | ||
|
||
check(uint32(3), true, 3) | ||
check(3.3, true, 3.3) | ||
check(float32(3.3), true, 3.3) | ||
check(StyledCell{Data: 3.3}, true, 3.3) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.