feat: add temp add command

This commit is contained in:
Slug-Boi
2024-10-24 20:47:32 +02:00
parent d3820826d1
commit b0a5125113
+75 -58
View File
@@ -12,18 +12,19 @@ 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
var ( var (
titleStyle = lipgloss.NewStyle().MarginLeft(2) titleStyle = lipgloss.NewStyle().MarginLeft(2)
itemStyle = lipgloss.NewStyle().PaddingLeft(4) itemStyle = lipgloss.NewStyle().PaddingLeft(4)
selectedItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("170")) selectedItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("170"))
highlightStyle = lipgloss.NewStyle().PaddingLeft(4).Background(lipgloss.Color("236")).Foreground(lipgloss.Color("17")) highlightStyle = lipgloss.NewStyle().PaddingLeft(4).Background(lipgloss.Color("236")).Foreground(lipgloss.Color("17"))
selectedHighlightStyle = lipgloss.NewStyle().PaddingLeft(2).Background(lipgloss.Color("236")).Foreground(lipgloss.Color("170")) selectedHighlightStyle = lipgloss.NewStyle().PaddingLeft(2).Background(lipgloss.Color("236")).Foreground(lipgloss.Color("170"))
paginationStyle = list.DefaultStyles().PaginationStyle.PaddingLeft(4) paginationStyle = list.DefaultStyles().PaginationStyle.PaddingLeft(4)
helpStyle = list.DefaultStyles().HelpStyle.PaddingLeft(4).PaddingBottom(1) helpStyle = list.DefaultStyles().HelpStyle.PaddingLeft(4).PaddingBottom(1)
//quitTextStyle = lipgloss.NewStyle().Margin(1, 0, 2, 4) //quitTextStyle = lipgloss.NewStyle().Margin(1, 0, 2, 4)
) )
@@ -32,16 +33,18 @@ 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{}
type listKeyMap struct { type listKeyMap struct {
selectAll key.Binding selectAll key.Binding
negation key.Binding negation key.Binding
groupSelect key.Binding groupSelect key.Binding
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,11 +73,14 @@ 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) }
type itemDelegate struct{} type itemDelegate struct{}
@@ -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
@@ -98,7 +103,7 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list
fn = func(s ...string) string { fn = func(s ...string) string {
base := strings.Join(s, " ") base := strings.Join(s, " ")
if negation { if negation {
base = base + " ^" base = base + " ^"
} }
if index == m.Index() { if index == m.Index() {
return selectedHighlightStyle.Render("> " + base + " [X]") return selectedHighlightStyle.Render("> " + base + " [X]")
@@ -114,13 +119,12 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list
} }
} }
fmt.Fprint(w, fn(str)) fmt.Fprint(w, fn(str))
} }
type model struct { type model struct {
list list.Model list list.Model
keys *listKeyMap keys *listKeyMap
quitting bool quitting bool
} }
@@ -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)
@@ -157,44 +160,56 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
// Handle keys from keyList (help menu) // Handle keys from keyList (help menu)
switch { switch {
case key.Matches(msg, m.keys.negation): case key.Matches(msg, m.keys.negation):
i, ok := m.list.SelectedItem().(item) i, ok := m.list.SelectedItem().(item)
if ok { if ok {
negation = true negation = true
selectToggle(i) selectToggle(i)
} }
case key.Matches(msg, m.keys.selectOne): case key.Matches(msg, m.keys.selectOne):
i, ok := m.list.SelectedItem().(item) i, ok := m.list.SelectedItem().(item)
if ok { if ok {
selectToggle(i) selectToggle(i)
} }
case key.Matches(msg, m.keys.selectAll): case key.Matches(msg, m.keys.selectAll):
//TODO: maybe look at behavior of this when auth are already selected //TODO: maybe look at behavior of this when auth are already selected
negation = false negation = false
for _, i := range m.list.Items() { for _, i := range m.list.Items() {
selectToggle(i.(item)) selectToggle(i.(item))
} }
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.createAuthor): case key.Matches(msg, m.keys.tempAdd):
author := Entry_CA() screen.Clear()
if author != "" { screen.MoveTopLeft()
item_str := utils.Users[author].Username + " - " + utils.Users[author].Email tempAuthr := Entry_TA()
dupProtect[item_str] = author if tempAuthr != "" {
m.list.InsertItem(len(m.list.Items())+1,item(item_str)) split := strings.Split(tempAuthr, ":")
} item_str := split[0] + " - " + split[1]
return m, tea.ClearScreen dupProtect[item_str] = tempAuthr
case key.Matches(msg, m.keys.deleteAuthor): m.list.InsertItem(len(m.list.Items())+1, item(item_str))
author_str := string(m.list.SelectedItem().(item)) }
author := dupProtect[author_str] return m, tea.ClearScreen
utils.DeleteOneAuthor(author)
delete(dupProtect, author_str) case key.Matches(msg, m.keys.createAuthor):
m.list.RemoveItem(m.list.Index()) author := Entry_CA()
return m, tea.ClearScreen if author != "" {
item_str := utils.Users[author].Username + " - " + utils.Users[author].Email
dupProtect[item_str] = author
m.list.InsertItem(len(m.list.Items())+1, item(item_str))
}
return m, tea.ClearScreen
case key.Matches(msg, m.keys.deleteAuthor):
author_str := string(m.list.SelectedItem().(item))
author := dupProtect[author_str]
utils.DeleteOneAuthor(author)
delete(dupProtect, author_str)
m.list.RemoveItem(m.list.Index())
return m, tea.ClearScreen
} }
// extra key options // extra key options
switch keypress := msg.String(); keypress { switch keypress := msg.String(); keypress {
@@ -222,7 +237,7 @@ func (m model) View() string {
return "\n" + m.list.View() return "\n" + m.list.View()
} }
//TODO: pass list in as a param to allow for group selection using same template // TODO: pass list in as a param to allow for group selection using same template
func Entry() []string { func Entry() []string {
items := []list.Item{} items := []list.Item{}
@@ -252,20 +267,20 @@ func Entry() []string {
l.Styles.Title = titleStyle l.Styles.Title = titleStyle
l.Styles.PaginationStyle = paginationStyle l.Styles.PaginationStyle = paginationStyle
l.AdditionalShortHelpKeys = // Add help keys (main page) l.AdditionalShortHelpKeys = // Add help keys (main page)
func() []key.Binding { func() []key.Binding {
return []key.Binding{ return []key.Binding{
listKeys.selectOne, listKeys.selectOne,
}
} }
}
l.AdditionalFullHelpKeys = // Add help keys (help menu) l.AdditionalFullHelpKeys = // Add help keys (help menu)
func() []key.Binding { func() []key.Binding {
return []key.Binding{ return []key.Binding{
listKeys.selectAll, listKeys.selectAll,
listKeys.negation, listKeys.negation,
listKeys.groupSelect, listKeys.groupSelect,
listKeys.createAuthor, listKeys.createAuthor,
}
} }
}
l.Styles.HelpStyle = helpStyle l.Styles.HelpStyle = helpStyle
m := model{list: l, keys: listKeys} m := model{list: l, keys: listKeys}
@@ -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 {