feat: pimped up the output of select

This commit is contained in:
TakshakRamteke committed 2026-10-03 23:32:51 +05:30
1 parent a3fe457fd0
commit 27df6a52fc
3 files changed
+91 -4

No files matched your search

+33 -4
View File
@@ -8,6 +8,10 @@ import (
"strconv"
"strings"
// additional
"charm.land/lipgloss/v2"
"charm.land/lipgloss/v2/table"
. "godb/internals/buffer"
)
@@ -74,6 +78,7 @@ type Statement struct {
type Table struct {
numOfRows uint
pages [maxPages]any
tableRep *table.Table
}
func NewStatement() *Statement {
@@ -81,8 +86,29 @@ func NewStatement() *Statement {
return statement
}
var (
purple = lipgloss.Color("99")
headerStyle = lipgloss.NewStyle().Foreground(purple).Bold(true).Padding(0, 1)
cellStyle = lipgloss.NewStyle().Padding(0, 1)
rowStyle = cellStyle.Foreground(lipgloss.White)
)
func NewTable() *Table {
newTable := Table{numOfRows: 0}
tbl := table.New().
Border(lipgloss.ASCIIBorder()).
BorderStyle(lipgloss.NewStyle().Foreground(purple)).
StyleFunc(func(row, col int) lipgloss.Style {
switch {
case row == table.HeaderRow:
return headerStyle
default:
return rowStyle
}
}).
Headers("ID", "NAME", "EMAIL")
newTable := Table{numOfRows: 0, tableRep: tbl}
for i := range maxPages {
newTable.pages[i] = nil
}
@@ -122,8 +148,8 @@ func (row *row) seralizeRow(page []byte) {
func (row *row) deSeralizeRow(page []byte) {
row.id = uint(binary.NativeEndian.Uint64(page[idOffset:idSize]))
row.username = string(page[usernameOffset : usernameOffset+usernameSize])
row.email = string(page[emailOffset : emailOffset+emailSize])
row.username = strings.TrimRight(string(page[usernameOffset:usernameOffset+usernameSize]), "\x00")
row.email = strings.TrimRight(string(page[emailOffset:emailOffset+emailSize]), "\x00")
}
func (statement *Statement) execInsert(table *Table) ExecuteResult {
@@ -137,10 +163,13 @@ func (statement *Statement) execInsert(table *Table) ExecuteResult {
func (statement *Statement) execSelect(table *Table) ExecuteResult {
var row row
t := table.tableRep
t.ClearRows()
for i := range table.numOfRows {
row.deSeralizeRow(rowToUse(table, i))
fmt.Printf("%v\n", row)
t.Row(fmt.Sprintf("%v", row.id), row.username, row.email)
}
lipgloss.Println(t)
return ExecuteSuccess
}