diff --git a/.gitignore b/.gitignore index c171cef..d578cba 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /superhighway84 /*.log /orbit*.db +/newsgroups diff --git a/superhighway84.go b/superhighway84.go index eb5f8d0..4eaccac 100644 --- a/superhighway84.go +++ b/superhighway84.go @@ -44,7 +44,7 @@ func main() { var articles []models.Article - TUI := tui.Init(&EMBEDFS) + TUI := tui.Init(&EMBEDFS, logger) TUI.ArticlesDatasource = &articles db, err := database.NewDatabase(ctx, cfg.ConnectionString, cfg.CachePath, logger) diff --git a/tui/helpers.go b/tui/helpers.go index 2726eb4..b4b9ea9 100644 --- a/tui/helpers.go +++ b/tui/helpers.go @@ -11,14 +11,13 @@ import ( "time" "github.com/mrusme/superhighway84/models" - "github.com/rivo/tview" ) func MillisecondsToDate(ms int64) (string) { return time.Unix(0, ms * int64(time.Millisecond)).Format("Mon Jan _2 15:04:05 2006") } -func OpenArticle(app *tview.Application, article *models.Article) (models.Article, error) { +func (t *TUI) OpenArticle(article *models.Article) (models.Article, error) { tmpFile, err := ioutil.TempFile(os.TempDir(), "article-*.txt") if err != nil { return *article, err @@ -26,7 +25,9 @@ func OpenArticle(app *tview.Application, article *models.Article) (models.Articl defer os.Remove(tmpFile.Name()) - tmpContent := []byte(fmt.Sprintf("Subject: %s\n= = = = = =\n%s", article.Subject, article.Body)) + tmpContent := []byte(fmt.Sprintf( + "Subject: %s\nNewsgroup: %s\n= = = = = =\n%s", + article.Subject, article.Newsgroup, article.Body)) if _, err = tmpFile.Write(tmpContent); err != nil { return *article, err } @@ -35,7 +36,7 @@ func OpenArticle(app *tview.Application, article *models.Article) (models.Articl return *article, err } - wasSuspended := app.Suspend(func() { + wasSuspended := t.App.Suspend(func() { cmd := exec.Command(os.Getenv("EDITOR"), tmpFile.Name()) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout @@ -55,16 +56,31 @@ func OpenArticle(app *tview.Application, article *models.Article) (models.Articl return *article, err } - content := strings.Split(string(tmpContent), "\n= = = = = =\n") + content := strings.SplitAfterN(string(tmpContent), "\n= = = = = =\n", 2) if len(content) != 2 { return *article, errors.New("Document malformatted") } + newArticle := *article + headerPart := strings.TrimSpace(content[0]) - subject := strings.TrimPrefix(headerPart, "Subject: ") - // TODO: Perform more validations - if len(subject) <= 1 { - return *article, errors.New("Invalid subject") + headers := strings.Split(headerPart, "\n") + + for _, header := range headers { + splitHeader := strings.SplitAfterN(header, ":", 2) + if len(splitHeader) < 2 { + continue + } + + headerName := strings.ToLower(strings.TrimSpace(splitHeader[0])) + headerValue := strings.TrimSpace(splitHeader[1]) + + switch(headerName) { + case "subject:": + newArticle.Subject = headerValue + case "newsgroup:": + newArticle.Newsgroup = headerValue + } } body := strings.TrimSpace(content[1]) @@ -72,9 +88,6 @@ func OpenArticle(app *tview.Application, article *models.Article) (models.Articl if len(body) <= 1 { return *article, errors.New("Invalid body") } - - newArticle := *article - newArticle.Subject = subject newArticle.Body = body return newArticle, nil diff --git a/tui/mainscreen.go b/tui/mainscreen.go index e995e3a..ab3e0b3 100644 --- a/tui/mainscreen.go +++ b/tui/mainscreen.go @@ -196,7 +196,7 @@ func(mainscreen *Mainscreen) selectHandler(item string)(func(int, string, string case "group": mainscreen.Refresh() case "article": - OpenArticle(mainscreen.T.App, mainscreen.ArticlesList[index]) + mainscreen.T.OpenArticle(mainscreen.ArticlesList[index]) } } } @@ -210,7 +210,7 @@ func(mainscreen *Mainscreen) submitNewArticle(group string) { // TODO: newArticle.Organisation = newArticle.Body = "" - updatedNewArticle, err := OpenArticle(mainscreen.T.App, newArticle) + updatedNewArticle, err := mainscreen.T.OpenArticle(newArticle) if err != nil { mainscreen.T.ShowErrorModal(err.Error()) return @@ -248,7 +248,7 @@ func(mainscreen *Mainscreen) replyToArticle(article *models.Article) { // TODO: newArticle.Organisation = newArticle.Body = fmt.Sprintf("\nOn %s %s wrote:\n> %s", MillisecondsToDate(article.Date), article.From, strings.Replace(article.Body, "\n", "\n> ", -1)) - updatedNewArticle, err := OpenArticle(mainscreen.T.App, newArticle) + updatedNewArticle, err := mainscreen.T.OpenArticle(newArticle) if err != nil { mainscreen.T.ShowErrorModal(err.Error()) return diff --git a/tui/tui.go b/tui/tui.go index a0c97f7..1660b86 100644 --- a/tui/tui.go +++ b/tui/tui.go @@ -9,6 +9,7 @@ import ( "github.com/gdamore/tcell/v2" "github.com/mrusme/superhighway84/models" "github.com/rivo/tview" + "go.uber.org/zap" ) type TUI struct { @@ -24,6 +25,8 @@ type TUI struct { CallbackRefreshArticles func() (error) CallbackSubmitArticle func(article *models.Article) (error) + + Logger *zap.Logger } type View interface { @@ -40,7 +43,7 @@ type ModalButton struct { Callback func() } -func Init(embedfs *embed.FS) (*TUI) { +func Init(embedfs *embed.FS, logger *zap.Logger) (*TUI) { t := new(TUI) tview.Styles = tview.Theme{ @@ -58,6 +61,7 @@ func Init(embedfs *embed.FS) (*TUI) { } t.App = tview.NewApplication() + t.Logger = logger logoBytes, err := embedfs.ReadFile("superhighway84.jpeg") if err != nil {