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
@@ -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)
}
+4
View File
@@ -67,3 +67,7 @@ func Define_users(author_file string) {
os.Exit(2)
}
}
func RemoveUser(short string) {
delete(Users, short)
}