|
|
|
@ -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
|
|
|
|
|