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

62 lines
1.1 KiB

package cmd
import (
"errors"
"fmt"
"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"
func PrintEntryHeader() {
fmt.Printf(
fmt_str+"\n", "Id", "Created time", "Title",
)
}
func PrintEntry(entry *model.DiaryEntry) {
created_time := time.Unix(entry.CreatedAt, 0).Format("2006/01/02 03:04pm")
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)
}
}