~porcellis/t

ref: structure t/commands/list.go -rw-r--r-- 1.2 KiB
76f0d055 — Pedro Lucas Porcellis Generate a simple and nice doc 1 year, 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package commands

import (
	"fmt"
	"git.sr.ht/~porcellis/t/config"
	"git.sr.ht/~porcellis/t/models"
	. "github.com/logrusorgru/aurora"
	"io/ioutil"
	"os"
	"path"
	"sort"
)

func check(e error) {
	if e != nil {
		panic(e)
	}
}

func List(config config.TConfig) error {
	var (
		err error
	)

	notes, err := BuildList(config)

	if err != nil {
		return err
	}

	fmt.Println(Bold("\n \t Your notes\n\n"))

	for _, note := range notes {
		fmt.Printf("%s (%s)\n", Bold(note.Title()), Faint(note.UpdatedAt()).BrightYellow())

		f, err := os.Open(note.Path)
		check(err)

		b1 := make([]byte, 35)
		n1, err := f.Read(b1)
		check(err)
		fmt.Printf("%s\n\n", string(b1[:n1]))
	}

	return err
}

func BuildList(config config.TConfig) ([]models.Note, error) {
	var (
		err   error
		notes []models.Note
	)

	files, err := ioutil.ReadDir(config.BasePath)

	sort.Slice(files, func(index, aux int) bool {
		return files[index].ModTime().After(files[aux].ModTime())
	})

	for _, entry := range files {
		if !entry.IsDir() {
			note := models.Note{Name: entry.Name(), Path: path.Join(config.BasePath, entry.Name()), ModTime: entry.ModTime()}

			notes = append(notes, note)
		}
	}

	return notes, err
}