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

39
main.go
View file

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

View file

@ -21,10 +21,10 @@ type Model struct {
styles Styles
styleFunc StyleFunc
viewport viewport.Model
start int
viewport viewport.Model
start int
activeIndex int
end int
end int
}
// Row represents one line in the table.
@ -112,7 +112,7 @@ type Styles struct {
Header lipgloss.Style
Cell lipgloss.Style
Selected lipgloss.Style
Active lipgloss.Style
Active lipgloss.Style
}
// 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.
func New(opts ...Option) Model {
m := Model{
cursor: 0,
cursor: 0,
activeIndex: 0,
viewport: viewport.New(0, 20),
viewport: viewport.New(0, 20),
KeyMap: DefaultKeyMap(),
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
// Constant runtime, independent of number of rows in a table.
// 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 {
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 {
m.start = 0
m.start = 0 + m.activeIndex
}
m.end = clamp(m.cursor+m.viewport.Height, m.cursor, len(m.rows))
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
// and are still adding
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[m.activeIndex] = selectedRow
m.rows = newSelection
m.activeIndex += 1
} else {
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[m.activeIndex] = selectedRow
m.rows = newSelection
@ -396,10 +401,10 @@ func (m *Model) MoveUp(n int) {
switch {
case m.start == 0:
m.viewport.SetYOffset(clamp(m.viewport.YOffset, 0, m.cursor))
case m.start < m.viewport.Height:
m.viewport.YOffset = (clamp(clamp(m.viewport.YOffset+n, 0, m.cursor), 0, m.viewport.Height))
case m.viewport.YOffset >= 1:
m.viewport.YOffset = clamp(m.viewport.YOffset+n, 1, 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))
//case m.viewport.YOffset >= 1:
// m.viewport.YOffset = clamp(m.viewport.YOffset+n, 1, m.viewport.Height)
}
m.UpdateViewport()
}
@ -519,5 +524,5 @@ func clamp(v, low, high int) int {
}
func remove(slice []Row, s int) []Row {
return append(slice[:s], slice[s+1:]...)
return append(slice[:s], slice[s+1:]...)
}