WIP: almost working selected rows

This commit is contained in:
Anish Lakhwara 2024-05-17 17:56:38 -07:00
parent b6e195c10b
commit bc7c635aac
2 changed files with 39 additions and 35 deletions

37
main.go
View file

@ -2,36 +2,35 @@ package main
import ( import (
"fmt" "fmt"
"os/exec" table "git.sealight.xyz/aynish/poms/table"
"log" task "git.sealight.xyz/aynish/poms/task"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
"log"
"os" "os"
"os/exec"
"strconv" "strconv"
"strings" "strings"
"time" "time"
task "git.sealight.xyz/aynish/poms/task"
table "git.sealight.xyz/aynish/poms/table"
) )
var ( var (
tasks = task.GetTasks() tasks = task.GetTasks()
columns = []table.Column{ columns = []table.Column{
{Title: "ID", Width: 4}, {Title: "ID", Width: 4},
{Title: "Project", Width: 20}, {Title: "Project", Width: 20},
{Title: "Tags", Width: 40}, {Title: "Tags", Width: 20},
{Title: "Description", Width: 95}, {Title: "Description", Width: 35},
// {Title: "Urgency", Width: 10}, // {Title: "Urgency", Width: 10},
} }
baseStyle = lipgloss.NewStyle(). baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()). BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240")) BorderForeground(lipgloss.Color("240"))
) )
type model struct { type model struct {
state string state string
taskList table.Model taskList table.Model
} }
@ -79,7 +78,7 @@ func (m model) rowsToFile() []string {
if index < m.taskList.ActiveIndex() { if index < m.taskList.ActiveIndex() {
// get uuid // get uuid
id := row[0] id := row[0]
uuid_byte, err := exec.Command("task", "_get", id + ".uuid").Output() uuid_byte, err := exec.Command("task", "_get", id+".uuid").Output()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -95,9 +94,9 @@ func (m model) rowsToFile() []string {
path := time.Now().Format(time.DateOnly) path := time.Now().Format(time.DateOnly)
tags := strings.Replace(row[2], " ", ":", -1) tags := strings.Replace(row[2], " ", ":", -1)
tags = ":" + row[1] + ":" + tags + ":" tags = ":" + row[1] + ":" + tags + ":"
fmt.Fprintln(f, "%title " + row[3]) fmt.Fprintln(f, "%title "+row[3])
fmt.Fprintln(f, tags) fmt.Fprintln(f, tags)
fmt.Fprintln(f, "%date " + path) fmt.Fprintln(f, "%date "+path)
fmt.Fprintln(f, "") fmt.Fprintln(f, "")
f.Close() f.Close()
} }
@ -126,13 +125,13 @@ func (m model) writeFile() string {
defer f.Close() defer f.Close()
fmt.Fprintln(f, "%title ") fmt.Fprintln(f, "%title ")
fmt.Fprintln(f, ":pom:priv:plan:") fmt.Fprintln(f, ":pom:priv:plan:")
fmt.Fprintln(f, "%date " + path) fmt.Fprintln(f, "%date "+path)
fmt.Fprintln(f, "") fmt.Fprintln(f, "")
rows := m.rowsToFile() rows := m.rowsToFile()
for _, value := range rows { for _, value := range rows {
fmt.Fprintln(f, value) // print values to f, one per line fmt.Fprintln(f, value) // print values to f, one per line
} }
return full_path return full_path
} }

View file

@ -21,10 +21,10 @@ type Model struct {
styles Styles styles Styles
styleFunc StyleFunc styleFunc StyleFunc
viewport viewport.Model viewport viewport.Model
start int start int
activeIndex int activeIndex int
end int end int
} }
// Row represents one line in the table. // Row represents one line in the table.
@ -112,7 +112,7 @@ type Styles struct {
Header lipgloss.Style Header lipgloss.Style
Cell lipgloss.Style Cell lipgloss.Style
Selected lipgloss.Style Selected lipgloss.Style
Active lipgloss.Style Active lipgloss.Style
} }
// DefaultStyles returns a set of default style definitions for this table. // DefaultStyles returns a set of default style definitions for this table.
@ -139,9 +139,9 @@ type Option func(*Model)
// New creates a new model for the table widget. // New creates a new model for the table widget.
func New(opts ...Option) Model { func New(opts ...Option) Model {
m := Model{ m := Model{
cursor: 0, cursor: 0,
activeIndex: 0, activeIndex: 0,
viewport: viewport.New(0, 20), viewport: viewport.New(0, 20),
KeyMap: DefaultKeyMap(), KeyMap: DefaultKeyMap(),
styles: DefaultStyles(), styles: DefaultStyles(),
@ -278,10 +278,15 @@ func (m *Model) UpdateViewport() {
// Render only rows from: m.cursor-m.viewport.Height to: m.cursor+m.viewport.Height // Render only rows from: m.cursor-m.viewport.Height to: m.cursor+m.viewport.Height
// Constant runtime, independent of number of rows in a table. // Constant runtime, independent of number of rows in a table.
// Limits the number of renderedRows to a maximum of 2*m.viewport.Height // Limits the number of renderedRows to a maximum of 2*m.viewport.Height
if m.activeIndex > 0 {
for i := 0; i < m.activeIndex; i++ {
renderedRows = append(renderedRows, m.renderRow(i))
}
}
if m.cursor >= 0 { if m.cursor >= 0 {
m.start = clamp(m.cursor-m.viewport.Height, 0, m.cursor) m.start = clamp(m.cursor-m.viewport.Height, 0, m.cursor) + m.activeIndex
} else { } else {
m.start = 0 m.start = 0 + m.activeIndex
} }
m.end = clamp(m.cursor+m.viewport.Height, m.cursor, len(m.rows)) m.end = clamp(m.cursor+m.viewport.Height, m.cursor, len(m.rows))
for i := m.start; i < m.end; i++ { for i := m.start; i < m.end; i++ {
@ -336,14 +341,14 @@ func (m *Model) SwitchRow() {
// We check equals because if it's on the border, we just moved into selection // We check equals because if it's on the border, we just moved into selection
// and are still adding // and are still adding
if m.cursor >= m.activeIndex { if m.cursor >= m.activeIndex {
newSelection := make([]Row, len(removedRows) + 1) newSelection := make([]Row, len(removedRows)+1)
newSelection = append(removedRows[:m.activeIndex+1], removedRows[m.activeIndex:]...) newSelection = append(removedRows[:m.activeIndex+1], removedRows[m.activeIndex:]...)
newSelection[m.activeIndex] = selectedRow newSelection[m.activeIndex] = selectedRow
m.rows = newSelection m.rows = newSelection
m.activeIndex += 1 m.activeIndex += 1
} else { } else {
m.activeIndex -= 1 m.activeIndex -= 1
newSelection := make([]Row, len(removedRows) + 1) newSelection := make([]Row, len(removedRows)+1)
newSelection = append(removedRows[:m.activeIndex+1], removedRows[m.activeIndex:]...) newSelection = append(removedRows[:m.activeIndex+1], removedRows[m.activeIndex:]...)
newSelection[m.activeIndex] = selectedRow newSelection[m.activeIndex] = selectedRow
m.rows = newSelection m.rows = newSelection
@ -396,10 +401,10 @@ func (m *Model) MoveUp(n int) {
switch { switch {
case m.start == 0: case m.start == 0:
m.viewport.SetYOffset(clamp(m.viewport.YOffset, 0, m.cursor)) m.viewport.SetYOffset(clamp(m.viewport.YOffset, 0, m.cursor))
case m.start < m.viewport.Height: //case m.start < m.viewport.Height:
m.viewport.YOffset = (clamp(clamp(m.viewport.YOffset+n, 0, m.cursor), 0, m.viewport.Height)) // m.viewport.YOffset = (clamp(clamp(m.viewport.YOffset+n, 0, m.cursor), 0, m.viewport.Height))
case m.viewport.YOffset >= 1: //case m.viewport.YOffset >= 1:
m.viewport.YOffset = clamp(m.viewport.YOffset+n, 1, m.viewport.Height) // m.viewport.YOffset = clamp(m.viewport.YOffset+n, 1, m.viewport.Height)
} }
m.UpdateViewport() m.UpdateViewport()
} }
@ -519,5 +524,5 @@ func clamp(v, low, high int) int {
} }
func remove(slice []Row, s int) []Row { func remove(slice []Row, s int) []Row {
return append(slice[:s], slice[s+1:]...) return append(slice[:s], slice[s+1:]...)
} }