diff --git a/src_code/go_src/cmd/tui/tui_create_author.go b/src_code/go_src/cmd/tui/tui_create_author.go index a3cb08b..0643d4d 100644 --- a/src_code/go_src/cmd/tui/tui_create_author.go +++ b/src_code/go_src/cmd/tui/tui_create_author.go @@ -5,6 +5,7 @@ package tui import ( "fmt" + "main/src_code/go_src/cmd/utils" "os" "strings" @@ -179,7 +180,7 @@ func (m model_ca) View() string { return b.String() } -func Entry_CA() { +func Entry_CA() string{ m, err := tea.NewProgram(initialModel()).Run() if err != nil { fmt.Printf("could not start program: %s\n", err) @@ -201,6 +202,7 @@ func Entry_CA() { defer f.Close() sb := strings.Builder{} + sb.WriteRune('\n') sb.WriteString(fmt.Sprintf("%s|%s|%s|%s", 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.WriteRune('\n') + //sb.WriteRune('\n') if _, err = f.WriteString(sb.String()); err != nil { panic(err) } + utils.Define_users(utils.Find_authorfile()) + return m.(model_ca).inputs[0].Value() } - + return "" } \ No newline at end of file diff --git a/src_code/go_src/cmd/tui/tui_list.go b/src_code/go_src/cmd/tui/tui_list.go index 6c68b7c..e343258 100644 --- a/src_code/go_src/cmd/tui/tui_list.go +++ b/src_code/go_src/cmd/tui/tui_list.go @@ -33,12 +33,15 @@ var selected = map[string]item{} var negation = false +var dupProtect = map[string]string{} + type listKeyMap struct { selectAll key.Binding negation key.Binding groupSelect key.Binding selectOne key.Binding createAuthor key.Binding + deleteAuthor key.Binding } func newListKeyMap() *listKeyMap { @@ -63,6 +66,10 @@ func newListKeyMap() *listKeyMap { key.WithKeys("C"), 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 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 } // 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 func Entry() []string { items := []list.Item{} - dupProtect := map[string]string{} listKeys := newListKeyMap() diff --git a/src_code/go_src/cmd/utils/author_file_utils.go b/src_code/go_src/cmd/utils/author_file_utils.go index a189f42..c672300 100644 --- a/src_code/go_src/cmd/utils/author_file_utils.go +++ b/src_code/go_src/cmd/utils/author_file_utils.go @@ -1,8 +1,11 @@ package utils import ( + "bufio" + "bytes" "fmt" "os" + "regexp" ) // 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 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) +} diff --git a/src_code/go_src/cmd/utils/user_util.go b/src_code/go_src/cmd/utils/user_util.go index 55bdaab..1b3477b 100644 --- a/src_code/go_src/cmd/utils/user_util.go +++ b/src_code/go_src/cmd/utils/user_util.go @@ -67,3 +67,7 @@ func Define_users(author_file string) { os.Exit(2) } } + +func RemoveUser(short string) { + delete(Users, short) +} \ No newline at end of file