2024-07-28 21:06:41 -04:00
|
|
|
package interactive
|
2024-06-19 18:55:01 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"path/filepath"
|
|
|
|
"slices"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.burning.moe/celediel/gt/internal/dirs"
|
|
|
|
"git.burning.moe/celediel/gt/internal/files"
|
2024-07-28 21:06:41 -04:00
|
|
|
"git.burning.moe/celediel/gt/internal/interactive/modes"
|
|
|
|
"git.burning.moe/celediel/gt/internal/interactive/sorting"
|
2024-07-03 18:05:17 -04:00
|
|
|
|
2024-06-19 18:55:01 -04:00
|
|
|
"github.com/charmbracelet/bubbles/key"
|
|
|
|
"github.com/charmbracelet/bubbles/table"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
"github.com/dustin/go-humanize"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
uncheck string = "☐"
|
|
|
|
check string = "☑"
|
|
|
|
space string = " "
|
|
|
|
woffset int = 13 // why this number, I don't know
|
2024-06-19 21:24:57 -04:00
|
|
|
hoffset int = 6
|
2024-06-19 18:55:01 -04:00
|
|
|
|
|
|
|
// TODO: make these configurable or something
|
|
|
|
borderbg string = "5"
|
|
|
|
hoveritembg string = "13"
|
|
|
|
black string = "0"
|
|
|
|
darkblack string = "8"
|
|
|
|
white string = "7"
|
|
|
|
darkgray string = "15"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
style = lipgloss.NewStyle().
|
|
|
|
BorderStyle(lipgloss.RoundedBorder()).
|
|
|
|
BorderForeground(lipgloss.Color(borderbg))
|
|
|
|
regulartext = lipgloss.NewStyle().
|
|
|
|
Padding(0, 2)
|
|
|
|
darktext = lipgloss.NewStyle().
|
|
|
|
Foreground(lipgloss.Color(darkgray))
|
|
|
|
darkertext = lipgloss.NewStyle().
|
|
|
|
Foreground(lipgloss.Color(darkblack))
|
|
|
|
darkesttext = lipgloss.NewStyle().
|
|
|
|
Foreground(lipgloss.Color(black))
|
|
|
|
)
|
|
|
|
|
|
|
|
type model struct {
|
2024-07-15 18:32:33 -04:00
|
|
|
table table.Model
|
|
|
|
keys keyMap
|
|
|
|
selected map[string]bool
|
2024-07-21 23:54:22 -04:00
|
|
|
selectsize int64
|
2024-07-15 18:32:33 -04:00
|
|
|
readonly bool
|
2024-07-15 18:33:37 -04:00
|
|
|
once bool
|
2024-07-15 18:32:33 -04:00
|
|
|
termheight int
|
2024-07-28 21:18:16 -04:00
|
|
|
termwidth int
|
2024-07-15 18:32:33 -04:00
|
|
|
mode modes.Mode
|
|
|
|
sorting sorting.Sorting
|
|
|
|
workdir string
|
|
|
|
files files.Files
|
2024-06-19 18:55:01 -04:00
|
|
|
}
|
|
|
|
|
2024-07-15 18:33:37 -04:00
|
|
|
func newModel(fs []files.File, width, height int, readonly, preselected, once bool, workdir string, mode modes.Mode) model {
|
2024-06-19 18:55:01 -04:00
|
|
|
var (
|
2024-07-15 20:10:57 -04:00
|
|
|
// TODO: figure this out dynamically based on longest of each
|
|
|
|
fwidth int = int(math.Round(float64(width-woffset) * 0.46))
|
2024-07-15 19:32:16 -04:00
|
|
|
owidth int = int(math.Round(float64(width-woffset) * 0.25))
|
|
|
|
dwidth int = int(math.Round(float64(width-woffset) * 0.15))
|
2024-06-19 18:55:01 -04:00
|
|
|
swidth int = int(math.Round(float64(width-woffset) * 0.12))
|
2024-07-15 20:10:57 -04:00
|
|
|
cwidth int = int(math.Round(float64(width-woffset) * 0.02))
|
2024-06-19 18:55:01 -04:00
|
|
|
theight int = min(height-hoffset, len(fs))
|
|
|
|
|
|
|
|
m = model{
|
2024-07-15 18:32:33 -04:00
|
|
|
keys: defaultKeyMap(),
|
|
|
|
readonly: readonly,
|
2024-07-15 18:33:37 -04:00
|
|
|
once: once,
|
2024-07-15 18:32:33 -04:00
|
|
|
termheight: height,
|
2024-07-28 21:18:16 -04:00
|
|
|
termwidth: width,
|
2024-07-15 18:32:33 -04:00
|
|
|
mode: mode,
|
|
|
|
selected: map[string]bool{},
|
2024-07-21 23:54:22 -04:00
|
|
|
selectsize: 0,
|
2024-07-15 18:32:33 -04:00
|
|
|
files: fs,
|
2024-06-19 18:55:01 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-07-15 19:31:58 -04:00
|
|
|
if workdir != "" {
|
|
|
|
m.workdir = filepath.Clean(workdir)
|
|
|
|
}
|
|
|
|
|
2024-07-15 18:32:33 -04:00
|
|
|
rows := m.freshRows(preselected)
|
2024-06-19 18:55:01 -04:00
|
|
|
|
|
|
|
columns := []table.Column{
|
|
|
|
{Title: "filename", Width: fwidth},
|
|
|
|
{Title: "path", Width: owidth},
|
|
|
|
{Title: "modified", Width: dwidth},
|
|
|
|
{Title: "size", Width: swidth},
|
|
|
|
}
|
|
|
|
if !m.readonly {
|
|
|
|
columns = append(columns, table.Column{Title: uncheck, Width: cwidth})
|
|
|
|
} else {
|
|
|
|
columns[0].Width += cwidth
|
|
|
|
}
|
|
|
|
|
2024-07-15 18:32:33 -04:00
|
|
|
m.table = createTable(columns, rows, theight)
|
2024-06-19 18:55:01 -04:00
|
|
|
|
2024-07-03 18:34:23 -04:00
|
|
|
m.sorting = sorting.Name
|
2024-07-03 18:05:17 -04:00
|
|
|
m.sort()
|
|
|
|
|
2024-06-19 18:55:01 -04:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
type keyMap struct {
|
|
|
|
mark key.Binding
|
|
|
|
doit key.Binding
|
2024-06-19 19:36:12 -04:00
|
|
|
todo key.Binding
|
|
|
|
nada key.Binding
|
|
|
|
invr key.Binding
|
2024-06-19 21:24:57 -04:00
|
|
|
rstr key.Binding
|
|
|
|
clen key.Binding
|
2024-07-03 18:05:17 -04:00
|
|
|
sort key.Binding
|
|
|
|
rort key.Binding
|
2024-06-19 18:55:01 -04:00
|
|
|
quit key.Binding
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultKeyMap() keyMap {
|
|
|
|
return keyMap{
|
|
|
|
mark: key.NewBinding(
|
|
|
|
key.WithKeys(space),
|
2024-06-19 19:36:12 -04:00
|
|
|
key.WithHelp("space", "toggle"),
|
2024-06-19 18:55:01 -04:00
|
|
|
),
|
|
|
|
doit: key.NewBinding(
|
|
|
|
key.WithKeys("enter", "y"),
|
|
|
|
key.WithHelp("enter/y", "confirm"),
|
|
|
|
),
|
2024-06-19 19:36:12 -04:00
|
|
|
todo: key.NewBinding(
|
|
|
|
key.WithKeys("a", "ctrl+a"),
|
2024-07-28 21:07:23 -04:00
|
|
|
key.WithHelp("a", "all"),
|
2024-06-19 19:36:12 -04:00
|
|
|
),
|
|
|
|
nada: key.NewBinding(
|
|
|
|
key.WithKeys("n", "ctrl+n"),
|
2024-06-20 00:36:13 -04:00
|
|
|
key.WithHelp("n", "none"),
|
2024-06-19 19:36:12 -04:00
|
|
|
),
|
|
|
|
invr: key.NewBinding(
|
|
|
|
key.WithKeys("i", "ctrl+i"),
|
2024-06-20 00:36:13 -04:00
|
|
|
key.WithHelp("i", "invert"),
|
2024-06-19 19:36:12 -04:00
|
|
|
),
|
2024-06-19 21:24:57 -04:00
|
|
|
clen: key.NewBinding(
|
|
|
|
key.WithKeys("c"),
|
2024-06-20 00:36:13 -04:00
|
|
|
key.WithHelp("c", "clean"),
|
2024-06-19 21:24:57 -04:00
|
|
|
),
|
|
|
|
rstr: key.NewBinding(
|
|
|
|
key.WithKeys("r"),
|
2024-06-20 00:36:13 -04:00
|
|
|
key.WithHelp("r", "restore"),
|
2024-06-19 21:24:57 -04:00
|
|
|
),
|
2024-07-03 18:05:17 -04:00
|
|
|
sort: key.NewBinding(
|
|
|
|
key.WithKeys("s"),
|
2024-07-28 21:07:23 -04:00
|
|
|
key.WithHelp("s/S", "sort"),
|
2024-07-03 18:05:17 -04:00
|
|
|
),
|
|
|
|
rort: key.NewBinding(
|
|
|
|
key.WithKeys("S"),
|
2024-07-28 21:07:23 -04:00
|
|
|
key.WithHelp("S", "sort (reverse)"),
|
2024-07-03 18:05:17 -04:00
|
|
|
),
|
2024-06-19 18:55:01 -04:00
|
|
|
quit: key.NewBinding(
|
|
|
|
key.WithKeys("q", "ctrl+c"),
|
2024-06-19 19:36:12 -04:00
|
|
|
key.WithHelp("q", "quit"),
|
2024-06-19 18:55:01 -04:00
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) Init() tea.Cmd {
|
2024-07-15 18:33:37 -04:00
|
|
|
/* if m.onePage() {
|
|
|
|
m.table.SetStyles(makeUnselectedStyle())
|
|
|
|
m.unselectAll()
|
|
|
|
return tea.Quit
|
|
|
|
} */
|
2024-06-19 18:55:01 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
var cmd tea.Cmd
|
|
|
|
|
2024-07-15 18:33:37 -04:00
|
|
|
if m.once {
|
2024-07-16 14:12:37 -04:00
|
|
|
return m.quit(m.readonly)
|
2024-07-15 18:33:37 -04:00
|
|
|
}
|
|
|
|
|
2024-06-19 18:55:01 -04:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
case tea.KeyMsg:
|
|
|
|
switch {
|
|
|
|
case key.Matches(msg, m.keys.mark):
|
2024-06-29 22:54:15 -04:00
|
|
|
m.toggleItem(m.table.Cursor())
|
2024-06-19 18:55:01 -04:00
|
|
|
case key.Matches(msg, m.keys.doit):
|
2024-06-19 21:24:57 -04:00
|
|
|
if !m.readonly && m.mode != modes.Interactive {
|
2024-06-29 22:56:51 -04:00
|
|
|
return m.quit(false)
|
2024-06-19 18:55:01 -04:00
|
|
|
}
|
2024-06-19 19:36:12 -04:00
|
|
|
case key.Matches(msg, m.keys.nada):
|
2024-06-29 22:54:15 -04:00
|
|
|
m.unselectAll()
|
2024-06-19 19:36:12 -04:00
|
|
|
case key.Matches(msg, m.keys.todo):
|
2024-06-29 22:54:15 -04:00
|
|
|
m.selectAll()
|
2024-06-19 19:36:12 -04:00
|
|
|
case key.Matches(msg, m.keys.invr):
|
2024-06-29 22:54:15 -04:00
|
|
|
m.invertSelection()
|
2024-06-19 21:24:57 -04:00
|
|
|
case key.Matches(msg, m.keys.clen):
|
2024-07-29 02:31:38 -04:00
|
|
|
return m.execute(modes.Cleaning)
|
2024-06-19 21:24:57 -04:00
|
|
|
case key.Matches(msg, m.keys.rstr):
|
2024-07-29 02:31:38 -04:00
|
|
|
return m.execute(modes.Restoring)
|
2024-07-03 18:05:17 -04:00
|
|
|
case key.Matches(msg, m.keys.sort):
|
|
|
|
m.sorting = m.sorting.Next()
|
|
|
|
m.sort()
|
|
|
|
case key.Matches(msg, m.keys.rort):
|
|
|
|
m.sorting = m.sorting.Prev()
|
|
|
|
m.sort()
|
2024-06-19 18:55:01 -04:00
|
|
|
case key.Matches(msg, m.keys.quit):
|
2024-06-19 21:24:57 -04:00
|
|
|
return m.quit(true)
|
2024-06-19 18:55:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pass events along to the table
|
|
|
|
m.table, cmd = m.table.Update(msg)
|
|
|
|
return m, cmd
|
|
|
|
}
|
|
|
|
|
2024-07-29 02:36:40 -04:00
|
|
|
func (m model) View() string {
|
|
|
|
var panels []string
|
2024-06-19 18:55:01 -04:00
|
|
|
|
2024-06-19 21:24:57 -04:00
|
|
|
if m.mode != modes.Listing {
|
2024-07-29 02:36:40 -04:00
|
|
|
panels = append(panels, m.header())
|
2024-06-19 21:24:57 -04:00
|
|
|
}
|
2024-07-29 02:36:40 -04:00
|
|
|
panels = append(panels, style.Render(m.table.View()), m.footer())
|
2024-06-19 21:24:57 -04:00
|
|
|
|
2024-07-29 02:36:40 -04:00
|
|
|
return lipgloss.JoinVertical(lipgloss.Top, panels...)
|
2024-06-19 18:55:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) showHelp() string {
|
|
|
|
// TODO: maybe use bubbletea built in help
|
|
|
|
var keys []string = []string{
|
2024-07-03 18:05:17 -04:00
|
|
|
fmt.Sprintf("%s %s (%s)", darktext.Render(m.keys.sort.Help().Key), darkertext.Render(m.keys.sort.Help().Desc), m.sorting.String()),
|
2024-06-19 18:55:01 -04:00
|
|
|
fmt.Sprintf("%s %s", darktext.Render(m.keys.quit.Help().Key), darkertext.Render(m.keys.quit.Help().Desc)),
|
|
|
|
}
|
|
|
|
if !m.readonly {
|
2024-06-19 21:24:57 -04:00
|
|
|
if m.mode != modes.Interactive {
|
|
|
|
keys = append([]string{
|
|
|
|
fmt.Sprintf("%s %s", darktext.Render(m.keys.doit.Help().Key), darkertext.Render(m.keys.doit.Help().Desc)),
|
|
|
|
}, keys...)
|
|
|
|
}
|
2024-06-19 18:55:01 -04:00
|
|
|
keys = append([]string{
|
|
|
|
fmt.Sprintf("%s %s", darktext.Render(m.keys.mark.Help().Key), darkertext.Render(m.keys.mark.Help().Desc)),
|
|
|
|
}, keys...)
|
|
|
|
}
|
|
|
|
return strings.Join(keys, darkesttext.Render(" • "))
|
|
|
|
}
|
|
|
|
|
2024-06-19 21:24:57 -04:00
|
|
|
func (m model) header() string {
|
|
|
|
var (
|
2024-07-28 21:18:16 -04:00
|
|
|
right, left string
|
|
|
|
spacer_width int
|
|
|
|
keys = []string{
|
2024-06-19 21:24:57 -04:00
|
|
|
fmt.Sprintf("%s %s", darktext.Render(m.keys.rstr.Help().Key), darkertext.Render(m.keys.rstr.Help().Desc)),
|
|
|
|
fmt.Sprintf("%s %s", darktext.Render(m.keys.clen.Help().Key), darkertext.Render(m.keys.clen.Help().Desc)),
|
|
|
|
}
|
2024-06-20 00:36:13 -04:00
|
|
|
select_keys = []string{
|
|
|
|
fmt.Sprintf("%s %s", darktext.Render(m.keys.todo.Help().Key), darkertext.Render(m.keys.todo.Help().Desc)),
|
|
|
|
fmt.Sprintf("%s %s", darktext.Render(m.keys.nada.Help().Key), darkertext.Render(m.keys.nada.Help().Desc)),
|
|
|
|
fmt.Sprintf("%s %s", darktext.Render(m.keys.invr.Help().Key), darkertext.Render(m.keys.invr.Help().Desc)),
|
|
|
|
}
|
|
|
|
dot = darkesttext.Render("•")
|
|
|
|
wide_dot = darkesttext.Render(" • ")
|
2024-06-19 21:24:57 -04:00
|
|
|
)
|
|
|
|
|
2024-07-28 21:18:16 -04:00
|
|
|
right = " " // to offset from the table border
|
2024-06-19 21:24:57 -04:00
|
|
|
switch m.mode {
|
|
|
|
case modes.Interactive:
|
2024-07-28 21:18:16 -04:00
|
|
|
right += strings.Join(keys, wide_dot)
|
2024-06-19 21:24:57 -04:00
|
|
|
default:
|
2024-07-28 21:18:16 -04:00
|
|
|
right += m.mode.String()
|
2024-07-03 18:05:17 -04:00
|
|
|
if m.workdir != "" {
|
2024-07-28 21:18:16 -04:00
|
|
|
right += fmt.Sprintf(" in %s", dirs.UnExpand(m.workdir, ""))
|
2024-06-30 23:01:36 -04:00
|
|
|
}
|
2024-06-19 21:24:57 -04:00
|
|
|
}
|
2024-07-28 21:18:16 -04:00
|
|
|
right += fmt.Sprintf(" %s %s", dot, strings.Join(select_keys, wide_dot))
|
2024-06-19 21:24:57 -04:00
|
|
|
|
2024-07-28 21:18:16 -04:00
|
|
|
left = fmt.Sprintf("%d/%d %s %s", len(m.selected), len(m.table.Rows()), dot, humanize.Bytes(uint64(m.selectsize)))
|
|
|
|
|
|
|
|
// offset of 2 again because of table border
|
|
|
|
spacer_width = m.termwidth - lipgloss.Width(right) - lipgloss.Width(left) - 2
|
|
|
|
if spacer_width <= 0 {
|
|
|
|
spacer_width = 1 // always at least one space
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s%s%s", right, strings.Repeat(" ", spacer_width), left)
|
2024-06-19 21:24:57 -04:00
|
|
|
}
|
|
|
|
|
2024-06-19 18:55:01 -04:00
|
|
|
func (m model) footer() string {
|
|
|
|
return regulartext.Render(m.showHelp())
|
|
|
|
}
|
|
|
|
|
2024-06-19 21:24:57 -04:00
|
|
|
func (m model) quit(unselect_all bool) (model, tea.Cmd) {
|
|
|
|
if unselect_all {
|
2024-06-29 22:54:15 -04:00
|
|
|
m.unselectAll()
|
2024-06-29 22:56:51 -04:00
|
|
|
} else {
|
|
|
|
m.onlySelected()
|
2024-06-19 21:24:57 -04:00
|
|
|
}
|
2024-06-19 19:36:12 -04:00
|
|
|
m.table.SetStyles(makeUnselectedStyle())
|
|
|
|
return m, tea.Quit
|
|
|
|
}
|
|
|
|
|
2024-07-29 02:31:38 -04:00
|
|
|
func (m model) execute(mode modes.Mode) (model, tea.Cmd) {
|
|
|
|
if m.mode != modes.Interactive || len(m.selected) <= 0 {
|
|
|
|
var cmd tea.Cmd
|
|
|
|
return m, cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
m.mode = mode
|
|
|
|
m.onlySelected()
|
|
|
|
m.table.SetStyles(makeUnselectedStyle())
|
|
|
|
return m, tea.Quit
|
|
|
|
}
|
|
|
|
|
2024-07-15 18:32:33 -04:00
|
|
|
func (m model) selectedFiles() (outfile files.Files) {
|
|
|
|
for _, file := range m.files {
|
|
|
|
if m.selected[file.String()] {
|
|
|
|
outfile = append(outfile, file)
|
2024-07-03 18:05:17 -04:00
|
|
|
}
|
2024-07-15 18:32:33 -04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-15 18:33:37 -04:00
|
|
|
/* func (m model) onePage() bool {
|
|
|
|
x := m.termheight
|
|
|
|
y := len(m.table.Rows()) + hoffset
|
|
|
|
if x > y && m.readonly {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
} */
|
|
|
|
|
2024-07-15 18:32:33 -04:00
|
|
|
func (m *model) freshRows(preselected bool) (rows []table.Row) {
|
|
|
|
for _, f := range m.files {
|
2024-07-15 20:10:38 -04:00
|
|
|
r := newRow(f, m.workdir)
|
2024-07-03 18:05:17 -04:00
|
|
|
|
|
|
|
if !m.readonly {
|
2024-07-15 18:32:33 -04:00
|
|
|
r = append(r, getCheck(preselected))
|
2024-07-03 18:05:17 -04:00
|
|
|
}
|
2024-07-15 18:32:33 -04:00
|
|
|
if preselected {
|
|
|
|
m.selected[f.String()] = true
|
2024-07-21 23:54:22 -04:00
|
|
|
m.selectsize += f.Filesize()
|
2024-07-03 18:05:17 -04:00
|
|
|
}
|
|
|
|
rows = append(rows, r)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-29 22:56:51 -04:00
|
|
|
func (m *model) onlySelected() {
|
|
|
|
var rows = make([]table.Row, 0)
|
|
|
|
for _, row := range m.table.Rows() {
|
|
|
|
if row[4] == check {
|
|
|
|
rows = append(rows, row)
|
|
|
|
} else {
|
|
|
|
rows = append(rows, table.Row{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.table.SetRows(rows)
|
|
|
|
}
|
|
|
|
|
2024-06-29 22:54:15 -04:00
|
|
|
// updateRow updates row of `index` with `row`
|
|
|
|
func (m *model) updateRow(index int, selected bool) {
|
2024-06-19 19:36:12 -04:00
|
|
|
rows := m.table.Rows()
|
|
|
|
row := rows[index]
|
|
|
|
rows[index] = table.Row{
|
|
|
|
row[0],
|
|
|
|
row[1],
|
|
|
|
row[2],
|
|
|
|
row[3],
|
|
|
|
getCheck(selected),
|
|
|
|
}
|
|
|
|
|
|
|
|
m.table.SetRows(rows)
|
|
|
|
}
|
|
|
|
|
2024-06-29 22:54:15 -04:00
|
|
|
func (m *model) updateRows(selected bool) {
|
2024-06-19 22:33:01 -04:00
|
|
|
var newrows []table.Row
|
|
|
|
|
|
|
|
for _, row := range m.table.Rows() {
|
|
|
|
r := table.Row{
|
|
|
|
row[0],
|
|
|
|
row[1],
|
|
|
|
row[2],
|
|
|
|
row[3],
|
|
|
|
getCheck(selected),
|
|
|
|
}
|
|
|
|
newrows = append(newrows, r)
|
|
|
|
}
|
|
|
|
m.table.SetRows(newrows)
|
|
|
|
}
|
|
|
|
|
2024-06-29 22:54:15 -04:00
|
|
|
func (m *model) toggleItem(index int) (selected bool) {
|
2024-06-19 18:55:01 -04:00
|
|
|
if m.readonly {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-07-15 18:32:33 -04:00
|
|
|
name := m.files[index].String()
|
2024-07-21 23:54:22 -04:00
|
|
|
size := m.files[index].Filesize()
|
2024-07-15 18:32:33 -04:00
|
|
|
|
2024-06-19 18:55:01 -04:00
|
|
|
// select the thing
|
2024-07-15 18:32:33 -04:00
|
|
|
if v, ok := m.selected[name]; v && ok {
|
2024-06-19 18:55:01 -04:00
|
|
|
// already selected
|
2024-07-15 18:32:33 -04:00
|
|
|
delete(m.selected, name)
|
2024-06-19 18:55:01 -04:00
|
|
|
selected = false
|
2024-07-21 23:54:22 -04:00
|
|
|
m.selectsize -= size
|
2024-06-19 18:55:01 -04:00
|
|
|
} else {
|
|
|
|
// not selected
|
2024-07-15 18:32:33 -04:00
|
|
|
m.selected[name] = true
|
2024-06-19 18:55:01 -04:00
|
|
|
selected = true
|
2024-07-21 23:54:22 -04:00
|
|
|
m.selectsize += size
|
2024-06-19 18:55:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// update the rows with the state
|
2024-06-29 22:54:15 -04:00
|
|
|
m.updateRow(index, selected)
|
2024-06-19 19:36:12 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-29 22:54:15 -04:00
|
|
|
func (m *model) selectAll() {
|
2024-06-19 19:36:12 -04:00
|
|
|
if m.readonly {
|
|
|
|
return
|
2024-06-19 18:55:01 -04:00
|
|
|
}
|
|
|
|
|
2024-07-15 18:32:33 -04:00
|
|
|
m.selected = map[string]bool{}
|
2024-07-21 23:54:22 -04:00
|
|
|
m.selectsize = 0
|
2024-07-15 20:55:51 -04:00
|
|
|
for i := range m.table.Rows() {
|
2024-07-15 18:32:33 -04:00
|
|
|
m.selected[m.files[i].String()] = true
|
2024-07-21 23:54:22 -04:00
|
|
|
m.selectsize += m.files[i].Filesize()
|
2024-06-19 19:36:12 -04:00
|
|
|
}
|
2024-06-29 22:54:15 -04:00
|
|
|
m.updateRows(true)
|
2024-06-19 19:36:12 -04:00
|
|
|
}
|
2024-06-19 18:55:01 -04:00
|
|
|
|
2024-06-29 22:54:15 -04:00
|
|
|
func (m *model) unselectAll() {
|
2024-06-19 19:36:12 -04:00
|
|
|
if m.readonly {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-15 18:32:33 -04:00
|
|
|
m.selected = map[string]bool{}
|
2024-07-21 23:54:22 -04:00
|
|
|
m.selectsize = 0
|
2024-06-29 22:54:15 -04:00
|
|
|
m.updateRows(false)
|
2024-06-19 19:36:12 -04:00
|
|
|
}
|
|
|
|
|
2024-06-29 22:54:15 -04:00
|
|
|
func (m *model) invertSelection() {
|
2024-07-15 18:32:33 -04:00
|
|
|
if m.readonly {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-19 22:33:01 -04:00
|
|
|
var newrows []table.Row
|
|
|
|
|
|
|
|
for index, row := range m.table.Rows() {
|
2024-07-15 18:32:33 -04:00
|
|
|
name := m.files[index].String()
|
2024-07-21 23:54:22 -04:00
|
|
|
size := m.files[index].Filesize()
|
2024-07-15 18:32:33 -04:00
|
|
|
if v, ok := m.selected[name]; v && ok {
|
|
|
|
delete(m.selected, name)
|
2024-07-21 23:54:22 -04:00
|
|
|
m.selectsize -= size
|
2024-06-19 22:33:01 -04:00
|
|
|
newrows = append(newrows, table.Row{
|
|
|
|
row[0],
|
|
|
|
row[1],
|
|
|
|
row[2],
|
|
|
|
row[3],
|
|
|
|
getCheck(false),
|
|
|
|
})
|
|
|
|
} else {
|
2024-07-15 18:32:33 -04:00
|
|
|
m.selected[name] = true
|
2024-07-21 23:54:22 -04:00
|
|
|
m.selectsize += size
|
2024-06-19 22:33:01 -04:00
|
|
|
newrows = append(newrows, table.Row{
|
|
|
|
row[0],
|
|
|
|
row[1],
|
|
|
|
row[2],
|
|
|
|
row[3],
|
|
|
|
getCheck(true),
|
|
|
|
})
|
|
|
|
}
|
2024-06-19 19:36:12 -04:00
|
|
|
}
|
2024-06-19 22:33:01 -04:00
|
|
|
|
|
|
|
m.table.SetRows(newrows)
|
2024-06-19 18:55:01 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:05:17 -04:00
|
|
|
func (m *model) sort() {
|
|
|
|
slices.SortStableFunc(m.files, m.sorting.Sorter())
|
2024-07-15 18:32:33 -04:00
|
|
|
var rows []table.Row
|
|
|
|
for _, file := range m.files {
|
2024-07-15 20:10:38 -04:00
|
|
|
r := newRow(file, m.workdir)
|
2024-07-15 18:32:33 -04:00
|
|
|
if !m.readonly {
|
|
|
|
r = append(r, getCheck(m.selected[file.String()]))
|
|
|
|
}
|
|
|
|
rows = append(rows, r)
|
|
|
|
}
|
2024-07-03 18:05:17 -04:00
|
|
|
|
2024-07-15 18:32:33 -04:00
|
|
|
m.table.SetRows(rows)
|
|
|
|
}
|
2024-06-20 00:17:55 -04:00
|
|
|
|
2024-07-15 18:32:33 -04:00
|
|
|
func Select(fs files.Files, width, height int, readonly, preselected, once bool, workdir string, mode modes.Mode) (files.Files, modes.Mode, error) {
|
|
|
|
mdl := newModel(fs, width, height, readonly, preselected, once, workdir, mode)
|
|
|
|
endmodel, err := tea.NewProgram(mdl).Run()
|
|
|
|
if err != nil {
|
|
|
|
return fs, 0, err
|
|
|
|
}
|
|
|
|
m, ok := endmodel.(model)
|
|
|
|
if !ok {
|
|
|
|
return fs, 0, fmt.Errorf("model isn't the right type?? what has happened")
|
2024-06-19 18:55:01 -04:00
|
|
|
}
|
2024-07-15 18:32:33 -04:00
|
|
|
return m.selectedFiles(), m.mode, nil
|
2024-06-19 18:55:01 -04:00
|
|
|
}
|
|
|
|
|
2024-07-28 21:07:07 -04:00
|
|
|
func newRow(file files.File, workdir string) table.Row {
|
|
|
|
var t, b string
|
|
|
|
t = humanize.Time(file.Date())
|
|
|
|
if file.IsDir() {
|
|
|
|
b = strings.Repeat("─", 3)
|
|
|
|
} else {
|
|
|
|
b = humanize.Bytes(uint64(file.Filesize()))
|
|
|
|
}
|
|
|
|
return table.Row{
|
|
|
|
dirs.UnEscape(file.Name()),
|
|
|
|
dirs.UnExpand(filepath.Dir(file.Path()), workdir),
|
|
|
|
t,
|
|
|
|
b,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-19 18:55:01 -04:00
|
|
|
func getCheck(selected bool) (ourcheck string) {
|
|
|
|
if selected {
|
|
|
|
ourcheck = check
|
|
|
|
} else {
|
|
|
|
ourcheck = uncheck
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-15 18:32:33 -04:00
|
|
|
func createTable(columns []table.Column, rows []table.Row, height int) table.Model {
|
2024-06-19 18:55:01 -04:00
|
|
|
t := table.New(
|
|
|
|
table.WithColumns(columns),
|
|
|
|
table.WithRows(rows),
|
|
|
|
table.WithFocused(true),
|
|
|
|
table.WithHeight(height),
|
|
|
|
)
|
|
|
|
t.KeyMap = fixTableKeymap()
|
2024-07-15 18:32:33 -04:00
|
|
|
t.SetStyles(makeStyle())
|
2024-06-19 18:55:01 -04:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func fixTableKeymap() table.KeyMap {
|
|
|
|
t := table.DefaultKeyMap()
|
|
|
|
|
|
|
|
// remove spacebar from default page down keybind, but keep the rest
|
|
|
|
t.PageDown.SetKeys(
|
|
|
|
slices.DeleteFunc(t.PageDown.Keys(), func(s string) bool {
|
|
|
|
return s == space
|
|
|
|
})...,
|
|
|
|
)
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeStyle() table.Styles {
|
|
|
|
s := table.DefaultStyles()
|
|
|
|
s.Header = s.Header.
|
|
|
|
BorderStyle(lipgloss.NormalBorder()).
|
|
|
|
BorderForeground(lipgloss.Color(black)).
|
|
|
|
BorderBottom(true).
|
|
|
|
Bold(false)
|
|
|
|
s.Selected = s.Selected.
|
|
|
|
Foreground(lipgloss.Color(white)).
|
|
|
|
Background(lipgloss.Color(hoveritembg)).
|
|
|
|
Bold(false)
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
2024-06-19 19:36:12 -04:00
|
|
|
|
|
|
|
func makeUnselectedStyle() table.Styles {
|
|
|
|
style := makeStyle()
|
|
|
|
style.Selected = style.Selected.
|
|
|
|
Foreground(lipgloss.NoColor{}).
|
|
|
|
Background(lipgloss.NoColor{}).
|
|
|
|
Bold(false)
|
|
|
|
return style
|
|
|
|
}
|