Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>
Display notes using the user's pager
README.md | 4 ++-- commands/list.go | 18 +++++++++--------- main.go | 25 ++++++++++++++++++++++++-
diff --git a/README.md b/README.md index 5b707379dac7cc418e755628a895344de4d128a0..a46f14efcf5cd8ae4b16615d1c54fd5244453024 100644 --- a/README.md +++ b/README.md @@ -68,8 +68,8 @@ There's a lot of things I want to implement on `t`, here is some of them: - Add a improved visualization when displaying notes -- Support OS Pager when listing notes (this can be achieved by `t list -| less -r`) +- ~~Support OS Pager when listing notes (this can be achieved by `t list +| less -r`)~~ - Delete a note - Make setup through `t` (`t init` would create the notes repository, set a remote, etc). - Add PGP support for notes (this is a must) diff --git a/commands/list.go b/commands/list.go index c45ffc29b49b861f8d34277ca8d5dcd4bc2d08cb..cd5d0e84a9a220dfed0189780cf2850db120d67e 100644 --- a/commands/list.go +++ b/commands/list.go @@ -1,7 +1,6 @@ package commands import ( - "fmt" "git.sr.ht/~porcellis/t/config" "git.sr.ht/~porcellis/t/models" . "github.com/logrusorgru/aurora" @@ -17,21 +16,22 @@ panic(e) } } -func List(config config.TConfig) error { +func List(config config.TConfig) (string, error) { var ( - err error + err error + content string ) notes, err := BuildList(config) if err != nil { - return err + return "", err } - fmt.Println(Bold("\n \t Your notes\n\n")) + content += Sprintf(Bold("\n \t Your notes\n\n")) for index, note := range notes { - fmt.Printf("[#%d] %s (%s)\n", index, Bold(note.Title()), Faint(note.UpdatedAt()).BrightYellow()) + content += Sprintf("[#%d] %s (%s)\n", index, Bold(note.Title()), Faint(note.UpdatedAt()).BrightYellow()) f, err := os.Open(note.Path) check(err) @@ -39,11 +39,11 @@ b1 := make([]byte, 240) n1, err := f.Read(b1) check(err) - fmt.Printf("%s\n\n", string(b1[:n1])) - fmt.Printf("------------------------------------\n\n") + content += Sprintf("%s\n\n", string(b1[:n1])) + content += Sprintf("------------------------------------\n\n") } - return err + return content, err } func BuildList(config config.TConfig) ([]models.Note, error) { diff --git a/main.go b/main.go index 8ad0c392fb0c4f02994c3d8eb4e910c828aa7618..4379b5c3e5fc38379c447d2322de58e8b459939e 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,9 @@ "git.sr.ht/~porcellis/t/config" "git.sr.ht/~porcellis/t/models" "log" "os" + "os/exec" "strconv" + "strings" ) var Version = "0.1.0" @@ -68,7 +70,28 @@ fmt.Println(fmt.Sprintf("Note created %s", note.Name)) case "list", "l": - commands.List(*configuration) + content, err := commands.List(*configuration) + + if err != nil { + panic("We could not fetch your notes") + } + + pager := os.Getenv("PAGER") + + if pager == "" { + // This options display control chars as raw + pager = "less -r" + } + + pagerCommand := strings.Split(pager, " ") + + cmd := exec.Command(pagerCommand[0], pagerCommand[1:]...) + cmd.Stdin = strings.NewReader(content) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + cmd.Run() + case "edit", "e": var note models.Note notes, _ := commands.BuildList(*configuration)