From e4886dda7cb07c29acaca564df528cf8a33027b9 Mon Sep 17 00:00:00 2001 From: isthisnagee Date: Fri, 9 Sep 2022 16:05:21 -0700 Subject: [PATCH] Add config for enabling/disabling markdown output Closes https://git.nagee.dev/isthisnagee/diary/issues/11 --- cmd/delete.go | 2 +- cmd/root.go | 2 +- cmd/show.go | 2 +- cmd/util.go | 24 +++++++++++++++++------- go.mod | 33 +++++++++++++++++++++++++++++++++ main.go | 1 - 6 files changed, 53 insertions(+), 11 deletions(-) diff --git a/cmd/delete.go b/cmd/delete.go index a26ba54..8be0fc8 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -31,7 +31,7 @@ var deleteCmd = &cobra.Command{ } fmt.Println("Are you sure you want to delete the following entry? (y/n)") PrintEntry(entry) - PrintNotes(notes) + PrintNotes(notes, App.Cfg.UseMarkdownInOutput) var response string fmt.Scanln(&response) if response == "y" { diff --git a/cmd/root.go b/cmd/root.go index 871cb4a..cb175e6 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -36,7 +36,7 @@ func Execute() { func init() { cobra.OnInitialize(InitApp) - rootCmd.PersistentFlags().String("config", "", "config file (default is $HOME/.diary.toml)") + rootCmd.PersistentFlags().String("config", "", "config file (default is $HOME/config/diary/diary.toml)") viper.BindPFlag("config", rootCmd.Flags().Lookup("config")) rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/cmd/show.go b/cmd/show.go index 1689ec5..a961191 100644 --- a/cmd/show.go +++ b/cmd/show.go @@ -42,7 +42,7 @@ var showCmd = &cobra.Command{ var notes = App.Db.GetDiaryEntryNotes(entry.Id) PrintEntry(entry) fmt.Println() - PrintNotes(notes) + PrintNotes(notes, App.Cfg.UseMarkdownInOutput) }, } diff --git a/cmd/util.go b/cmd/util.go index 94d850e..038ee5f 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -16,7 +16,8 @@ import ( ) type Cfg struct { - DbPath string + DbPath string + UseMarkdownInOutput bool } type TApp struct { @@ -49,6 +50,10 @@ func initConfig() Cfg { viper.SetDefault("db_path", db_path) } + if viper.Get("use_markdown_output") == nil { + viper.SetDefault("use_markdown_output", true) + } + viper.AutomaticEnv() // read in environment variables that match if err := viper.ReadInConfig(); err != nil { @@ -59,7 +64,7 @@ func initConfig() Cfg { } } - return Cfg{viper.GetString("db_path")} + return Cfg{viper.GetString("db_path"), viper.GetBool("use_markdown_output")} } func InitApp() { @@ -100,21 +105,26 @@ func PrintEntries(entries []*model.DiaryEntry) { } } -func PrintNote(note *model.DiaryEntryNote, add_tail bool) { +func PrintNote(note *model.DiaryEntryNote, add_tail bool, use_markdown bool) { created_time := time.Unix(note.CreatedAt, 0).Format(time_fmt_str) fmt.Println("---note ( id:", note.Id, ")", created_time) fmt.Println() - body := markdown.Render(note.Body, 80, 3) - fmt.Println(string(body)) + var body string + if use_markdown { + body = string(markdown.Render(note.Body, 80, 3)) + } else { + body = note.Body + } + fmt.Println(body) if add_tail { fmt.Println("---") } } -func PrintNotes(notes []*model.DiaryEntryNote) { +func PrintNotes(notes []*model.DiaryEntryNote, use_markdown bool) { for idx, entry := range notes { var is_last = idx == len(notes)-1 - PrintNote(entry, is_last) + PrintNote(entry, is_last, use_markdown) } } diff --git a/go.mod b/go.mod index b3df44b..23aa6bf 100644 --- a/go.mod +++ b/go.mod @@ -9,3 +9,36 @@ require ( github.com/spf13/cobra v1.3.0 github.com/spf13/viper v1.10.1 ) + +require ( + github.com/MichaelMure/go-term-text v0.3.1 // indirect + github.com/alecthomas/chroma v0.7.1 // indirect + github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect + github.com/disintegration/imaging v1.6.2 // indirect + github.com/dlclark/regexp2 v1.1.6 // indirect + github.com/eliukblau/pixterm/pkg/ansimage v0.0.0-20191210081756-9fb6cf8c2f75 // indirect + github.com/fsnotify/fsnotify v1.5.1 // indirect + github.com/gomarkdown/markdown v0.0.0-20191123064959-2c17d62f5098 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect + github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/kyokomi/emoji/v2 v2.2.8 // indirect + github.com/lucasb-eyer/go-colorful v1.0.3 // indirect + github.com/magiconair/properties v1.8.5 // indirect + github.com/mattn/go-colorable v0.1.12 // indirect + github.com/mattn/go-isatty v0.0.14 // indirect + github.com/mattn/go-runewidth v0.0.12 // indirect + github.com/mitchellh/mapstructure v1.4.3 // indirect + github.com/pelletier/go-toml v1.9.4 // indirect + github.com/rivo/uniseg v0.1.0 // indirect + github.com/spf13/afero v1.6.0 // indirect + github.com/spf13/cast v1.4.1 // indirect + github.com/spf13/jwalterweatherman v1.1.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/subosito/gotenv v1.2.0 // indirect + golang.org/x/image v0.0.0-20191206065243-da761ea9ff43 // indirect + golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect + golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect + golang.org/x/text v0.3.7 // indirect + gopkg.in/ini.v1 v1.66.2 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect +) diff --git a/main.go b/main.go index 843b010..4c3af6a 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,5 @@ /* Copyright © 2021 NAME HERE - */ package main -- 2.36.3