-
Notifications
You must be signed in to change notification settings - Fork 0
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
6 changed files
with
263 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/noppaz/collie/internal/commands" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
var amount int | ||
lessCommand := &cobra.Command{ | ||
Use: "less", | ||
Short: "Scroll through N values from the file", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
filename := args[0] | ||
return commands.LessCommand(filename, amount) | ||
}, | ||
} | ||
|
||
lessCommand.Flags().IntVarP(&amount, "rows", "n", 1000, "Amount of rows to load to the buffer") | ||
|
||
rootCmd.AddCommand(lessCommand) | ||
} |
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,58 @@ | ||
package parse | ||
|
||
import ( | ||
"slices" | ||
"testing" | ||
) | ||
|
||
func Test_ReadRows(t *testing.T) { | ||
type Input struct { | ||
filename string | ||
amount int | ||
} | ||
|
||
type Expected struct { | ||
rowLength int | ||
headers []string | ||
} | ||
|
||
testCases := []struct { | ||
name string | ||
input Input | ||
expected Expected | ||
}{ | ||
{ | ||
"amount less than one row group", | ||
Input{"../../testdata/sample.parquet", 33}, | ||
Expected{33, []string{"col_int64", "col_double", "col_string", "col_timestamp"}}, | ||
}, | ||
{ | ||
"amount more than several row groups", | ||
Input{"../../testdata/sample.parquet", 646}, | ||
Expected{646, []string{"col_int64", "col_double", "col_string", "col_timestamp"}}, | ||
}, | ||
{ | ||
"amount more than file has", | ||
Input{"../../testdata/sample.parquet", 1400}, | ||
Expected{1000, []string{"col_int64", "col_double", "col_string", "col_timestamp"}}, | ||
}, | ||
} | ||
|
||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
headers, rows, err := ReadRows(tt.input.filename, tt.input.amount) | ||
if err != nil { | ||
t.Errorf("ReadRows returned err: %v", err) | ||
} | ||
if !slices.Equal(headers, tt.expected.headers) { | ||
t.Errorf("ReadRows error: header mismatch, got=%v, expected=%v", headers, tt.expected.headers) | ||
} | ||
if len(rows) != tt.expected.rowLength { | ||
t.Errorf( | ||
"ReadRows error: read row length mismatch, got=%v, expected=%v", | ||
len(rows), | ||
tt.expected.rowLength) | ||
} | ||
}) | ||
} | ||
} |
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 visualize | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/charmbracelet/bubbles/viewport" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/lipgloss" | ||
) | ||
|
||
const useHighPerformanceRenderer = true | ||
|
||
var ( | ||
titleStyle = func() lipgloss.Style { | ||
b := lipgloss.RoundedBorder() | ||
b.Right = "├" | ||
return lipgloss.NewStyle().BorderStyle(b).Padding(0, 1) | ||
}() | ||
|
||
infoStyle = func() lipgloss.Style { | ||
b := lipgloss.RoundedBorder() | ||
b.Left = "┤" | ||
return titleStyle.Copy().BorderStyle(b) | ||
}() | ||
) | ||
|
||
func ViewportCreator(header, content string) error { | ||
p := tea.NewProgram( | ||
ViewportModel{header: header, content: content}, | ||
tea.WithAltScreen(), | ||
tea.WithMouseCellMotion(), | ||
) | ||
|
||
if _, err := p.Run(); err != nil { | ||
return fmt.Errorf("running viewport app: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
type ViewportModel struct { | ||
header string | ||
content string | ||
ready bool | ||
viewport viewport.Model | ||
} | ||
|
||
func (m ViewportModel) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
func (m ViewportModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
var ( | ||
cmd tea.Cmd | ||
cmds []tea.Cmd | ||
) | ||
|
||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
if k := msg.String(); k == "ctrl+c" || k == "q" || k == "esc" { | ||
return m, tea.Quit | ||
} | ||
|
||
case tea.WindowSizeMsg: | ||
headerHeight := lipgloss.Height(m.headerView()) | ||
footerHeight := lipgloss.Height(m.footerView()) | ||
verticalMarginHeight := headerHeight + footerHeight | ||
|
||
if !m.ready { | ||
m.viewport = viewport.New(msg.Width, msg.Height-verticalMarginHeight) | ||
m.viewport.YPosition = headerHeight | ||
m.viewport.HighPerformanceRendering = useHighPerformanceRenderer | ||
m.viewport.SetContent(m.content) | ||
m.ready = true | ||
|
||
m.viewport.YPosition = headerHeight + 1 | ||
} else { | ||
m.viewport.Width = msg.Width | ||
m.viewport.Height = msg.Height - verticalMarginHeight | ||
} | ||
|
||
if useHighPerformanceRenderer { | ||
cmds = append(cmds, viewport.Sync(m.viewport)) | ||
} | ||
} | ||
|
||
m.viewport, cmd = m.viewport.Update(msg) | ||
cmds = append(cmds, cmd) | ||
|
||
return m, tea.Batch(cmds...) | ||
} | ||
|
||
func (m ViewportModel) View() string { | ||
if !m.ready { | ||
return "\n Initializing..." | ||
} | ||
return fmt.Sprintf("%s\n%s\n%s", m.headerView(), m.viewport.View(), m.footerView()) | ||
} | ||
|
||
func (m ViewportModel) headerView() string { | ||
return m.header | ||
} | ||
|
||
func (m ViewportModel) footerView() string { | ||
info := infoStyle.Render(fmt.Sprintf("%3.f%%", m.viewport.ScrollPercent()*100)) | ||
line := strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(info))) | ||
return lipgloss.JoinHorizontal(lipgloss.Center, line, info) | ||
} |