feat: add temp add command

This commit is contained in:
Slug-Boi
2024-10-24 20:47:32 +02:00
parent d3820826d1
commit b0a5125113
+22 -5
View File
@@ -12,6 +12,7 @@ import (
"github.com/charmbracelet/bubbles/list" "github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
"github.com/inancgumus/screen"
) )
const listHeight = 14 const listHeight = 14
@@ -32,6 +33,7 @@ type item string
var selected = map[string]item{} var selected = map[string]item{}
var negation = false var negation = false
var ignore = false
var dupProtect = map[string]string{} var dupProtect = map[string]string{}
@@ -42,6 +44,7 @@ type listKeyMap struct {
selectOne key.Binding selectOne key.Binding
createAuthor key.Binding createAuthor key.Binding
deleteAuthor key.Binding deleteAuthor key.Binding
tempAdd key.Binding
} }
func newListKeyMap() *listKeyMap { func newListKeyMap() *listKeyMap {
@@ -70,10 +73,13 @@ func newListKeyMap() *listKeyMap {
key.WithKeys("D"), key.WithKeys("D"),
key.WithHelp("D", "Delete author"), key.WithHelp("D", "Delete author"),
), ),
tempAdd: key.NewBinding(
key.WithKeys("T"),
key.WithHelp("T", "Add temporary author"),
),
} }
} }
// TODO: Try and add filtering later down the line // TODO: Try and add filtering later down the line
func (i item) FilterValue() string { return string(i) } func (i item) FilterValue() string { return string(i) }
@@ -88,7 +94,6 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list
return return
} }
str := fmt.Sprintf("%d. %s", index+1, i) str := fmt.Sprintf("%d. %s", index+1, i)
//TODO: add negation style where all items are flipped in selection //TODO: add negation style where all items are flipped in selection
@@ -114,7 +119,6 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list
} }
} }
fmt.Fprint(w, fn(str)) fmt.Fprint(w, fn(str))
} }
@@ -144,7 +148,6 @@ func toggleNegation() {
} }
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.WindowSizeMsg: case tea.WindowSizeMsg:
m.list.SetWidth(msg.Width) m.list.SetWidth(msg.Width)
@@ -180,6 +183,18 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, m.keys.groupSelect): case key.Matches(msg, m.keys.groupSelect):
// group code goes here // group code goes here
case key.Matches(msg, m.keys.tempAdd):
screen.Clear()
screen.MoveTopLeft()
tempAuthr := Entry_TA()
if tempAuthr != "" {
split := strings.Split(tempAuthr, ":")
item_str := split[0] + " - " + split[1]
dupProtect[item_str] = tempAuthr
m.list.InsertItem(len(m.list.Items())+1, item(item_str))
}
return m, tea.ClearScreen
case key.Matches(msg, m.keys.createAuthor): case key.Matches(msg, m.keys.createAuthor):
author := Entry_CA() author := Entry_CA()
if author != "" { if author != "" {
@@ -279,7 +294,9 @@ func Entry() []string {
// Assert the final tea.Model to our local model and print the choice. // Assert the final tea.Model to our local model and print the choice.
output := []string{} output := []string{}
if len(selected) == 0 {
os.Exit(0)
}
for i := range selected { for i := range selected {
short := dupProtect[i] short := dupProtect[i]
if negation { if negation {