feat: finish add and remove author bindings and tui elements

This commit is contained in:
Theis
2024-10-10 22:36:29 +02:00
parent a78cdf37f9
commit 55fe196536
4 changed files with 84 additions and 5 deletions
+7 -3
View File
@@ -5,6 +5,7 @@ package tui
import ( import (
"fmt" "fmt"
"main/src_code/go_src/cmd/utils"
"os" "os"
"strings" "strings"
@@ -179,7 +180,7 @@ func (m model_ca) View() string {
return b.String() return b.String()
} }
func Entry_CA() { func Entry_CA() string{
m, err := tea.NewProgram(initialModel()).Run() m, err := tea.NewProgram(initialModel()).Run()
if err != nil { if err != nil {
fmt.Printf("could not start program: %s\n", err) fmt.Printf("could not start program: %s\n", err)
@@ -201,6 +202,7 @@ func Entry_CA() {
defer f.Close() defer f.Close()
sb := strings.Builder{} sb := strings.Builder{}
sb.WriteRune('\n')
sb.WriteString(fmt.Sprintf("%s|%s|%s|%s", sb.WriteString(fmt.Sprintf("%s|%s|%s|%s",
m.(model_ca).inputs[0].Value(), m.(model_ca).inputs[0].Value(),
@@ -216,11 +218,13 @@ func Entry_CA() {
sb.WriteString(fmt.Sprintf(";;%s", m.(model_ca).inputs[4].Value())) sb.WriteString(fmt.Sprintf(";;%s", m.(model_ca).inputs[4].Value()))
} }
sb.WriteRune('\n') //sb.WriteRune('\n')
if _, err = f.WriteString(sb.String()); err != nil { if _, err = f.WriteString(sb.String()); err != nil {
panic(err) panic(err)
} }
utils.Define_users(utils.Find_authorfile())
return m.(model_ca).inputs[0].Value()
} }
return ""
} }
+20 -2
View File
@@ -33,12 +33,15 @@ var selected = map[string]item{}
var negation = false var negation = false
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
} }
func newListKeyMap() *listKeyMap { func newListKeyMap() *listKeyMap {
@@ -63,6 +66,10 @@ func newListKeyMap() *listKeyMap {
key.WithKeys("C"), key.WithKeys("C"),
key.WithHelp("C", "Create new author"), key.WithHelp("C", "Create new author"),
), ),
deleteAuthor: key.NewBinding(
key.WithKeys("D"),
key.WithHelp("D", "Delete author"),
),
} }
} }
@@ -174,7 +181,19 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// group code goes here // group code goes here
case key.Matches(msg, m.keys.createAuthor): case key.Matches(msg, m.keys.createAuthor):
Entry_CA() author := Entry_CA()
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 return m, tea.ClearScreen
} }
// extra key options // extra key options
@@ -206,7 +225,6 @@ func (m model) View() string {
//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{}
dupProtect := map[string]string{}
listKeys := newListKeyMap() listKeys := newListKeyMap()
@@ -1,8 +1,11 @@
package utils package utils
import ( import (
"bufio"
"bytes"
"fmt" "fmt"
"os" "os"
"regexp"
) )
// Author file utils is a package that contains functions that are used to read // Author file utils is a package that contains functions that are used to read
@@ -42,3 +45,53 @@ func CheckAuthorFile() string {
// This string output is mostly for convenience can mostly be ignored // This string output is mostly for convenience can mostly be ignored
return authorfile return authorfile
} }
func DeleteOneAuthor(author string) {
//author_file := Find_authorfile()
author_file := "author_file"
// open author_file
file, err := os.OpenFile(author_file, os.O_RDWR, 0644)
if err != nil {
fmt.Println("Error opening file: ", err)
return
}
defer file.Close()
// create regex to capture author line
regexp, err := regexp.Compile(fmt.Sprintf("^(.+\\|%s\\|.+|%s\\|.+\\|.+)$",author,author))
if err != nil {
fmt.Println("Error compiling regex: ", err)
return
}
var b []byte
buf := bytes.NewBuffer(b)
// create a scanner for the file
scanner := bufio.NewScanner(file)
// write the header to the buffer
scanner.Scan()
buf.WriteString(scanner.Text() + "\n")
// check if author matches the regex and skip
for scanner.Scan() {
line := scanner.Text()
if regexp.MatchString(line) {
continue
}
buf.WriteString(line + "\n")
}
// remove the last newline character
buf.Truncate(buf.Len()-1)
file.Truncate(0)
file.Seek(0,0)
buf.WriteTo(file)
RemoveUser(author)
}
+4
View File
@@ -67,3 +67,7 @@ func Define_users(author_file string) {
os.Exit(2) os.Exit(2)
} }
} }
func RemoveUser(short string) {
delete(Users, short)
}