You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
diary/cmd/util.go

79 lines
1.5 KiB

package cmd
import (
"errors"
"fmt"
markdown "github.com/MichaelMure/go-term-markdown"
"github.com/fatih/color"
"isthisnagee.com/tools/diary/model"
"log"
"os"
"path"
"strconv"
"time"
)
var App *model.App
func InitApp() {
home_dir, err := os.UserHomeDir()
if err != nil {
log.Fatal(err.Error())
}
var db_path = path.Join(home_dir, ".diary.sql")
if _, err := os.Stat(db_path); errors.Is(err, os.ErrNotExist) {
_, err := os.Create(db_path)
if err != nil {
log.Fatal(err.Error())
}
}
if err != nil {
log.Fatal(err.Error())
}
var app = model.NewApp(db_path)
App = &app
}
var fmt_str = "%-10s %-20s %s\n"
var time_fmt_str = "2006/01/02 03:04pm"
func PrintEntryHeader() {
fmt.Printf(
fmt_str+"\n", "Id", "Created time", "Title",
)
}
func PrintEntry(entry *model.DiaryEntry) {
created_time := time.Unix(entry.CreatedAt, 0).Format(time_fmt_str)
titleColor := color.New(color.FgCyan).Add(color.Underline).SprintFunc()
fmt.Printf(
fmt_str, strconv.FormatInt(entry.Id, 10), created_time, titleColor(entry.Title),
)
}
func PrintEntries(entries []*model.DiaryEntry) {
PrintEntryHeader()
for _, entry := range entries {
PrintEntry(entry)
}
}
func PrintNote(note *model.DiaryEntryNote) {
created_time := time.Unix(note.CreatedAt, 0).Format(time_fmt_str)
fmt.Println("---note created_at ", created_time)
fmt.Println()
body := markdown.Render(note.Body, 80, 3)
fmt.Println(string(body))
fmt.Println("---")
}
func PrintNotes(notes []*model.DiaryEntryNote) {
for _, entry := range notes {
PrintNote(entry)
}
}