2021-12-26 03:02:56 +00:00
|
|
|
package tui
|
|
|
|
|
|
|
|
import (
|
2021-12-26 04:18:23 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-12-26 03:02:56 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2021-12-26 04:18:23 +00:00
|
|
|
"strings"
|
2021-12-26 03:02:56 +00:00
|
|
|
|
|
|
|
"github.com/mrusme/superhighway84/models"
|
|
|
|
"github.com/rivo/tview"
|
|
|
|
)
|
|
|
|
|
|
|
|
func OpenArticle(app *tview.Application, article *models.Article) (models.Article, error) {
|
|
|
|
tmpFile, err := ioutil.TempFile(os.TempDir(), "article-*.txt")
|
|
|
|
if err != nil {
|
|
|
|
return *article, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer os.Remove(tmpFile.Name())
|
|
|
|
|
2021-12-26 04:18:23 +00:00
|
|
|
tmpContent := []byte(fmt.Sprintf("Subject: %s\n= = = = = =\n%s", article.Subject, article.Body))
|
2021-12-26 03:02:56 +00:00
|
|
|
if _, err = tmpFile.Write(tmpContent); err != nil {
|
|
|
|
return *article, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpFile.Close(); err != nil {
|
|
|
|
return *article, err
|
|
|
|
}
|
|
|
|
|
|
|
|
wasSuspended := app.Suspend(func() {
|
|
|
|
cmd := exec.Command(os.Getenv("EDITOR"), tmpFile.Name())
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
if wasSuspended == false {
|
|
|
|
return *article, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpContent, err = os.ReadFile(tmpFile.Name())
|
|
|
|
if err != nil {
|
|
|
|
return *article, err
|
|
|
|
}
|
|
|
|
|
2021-12-26 04:18:23 +00:00
|
|
|
content := strings.Split(string(tmpContent), "\n= = = = = =\n")
|
|
|
|
if len(content) != 2 {
|
|
|
|
return *article, errors.New("Document malformatted")
|
|
|
|
}
|
|
|
|
|
|
|
|
headerPart := strings.TrimSpace(content[0])
|
|
|
|
subject := strings.TrimPrefix(headerPart, "Subject: ")
|
|
|
|
if len(subject) <= 1 {
|
|
|
|
return *article, errors.New("Invalid subject")
|
|
|
|
}
|
|
|
|
|
|
|
|
body := strings.TrimSpace(content[1])
|
|
|
|
// TODO: Perform more validations
|
|
|
|
if len(body) <= 1 {
|
|
|
|
return *article, errors.New("Invalid body")
|
|
|
|
}
|
|
|
|
|
2021-12-26 03:02:56 +00:00
|
|
|
newArticle := *article
|
2021-12-26 04:18:23 +00:00
|
|
|
newArticle.Subject = subject
|
|
|
|
newArticle.Body = body
|
2021-12-26 03:02:56 +00:00
|
|
|
|
|
|
|
return newArticle, nil
|
|
|
|
}
|
|
|
|
|