t

ref: master

commands/list.go


 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
70
package commands

import (
	"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) (string, error) {
	var (
		err     error
		content string
	)

	notes, err := BuildList(config)

	if err != nil {
		return "", err
	}

	content += Sprintf(Bold("\n \t Your notes\n\n"))

	for index, note := range notes {
		content += Sprintf("[#%d] %s (%s)\n", index, Bold(note.Title()), Faint(note.UpdatedAt()).BrightYellow())

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

		b1 := make([]byte, 240)
		n1, err := f.Read(b1)
		check(err)
		content += Sprintf("%s\n\n", string(b1[:n1]))
		content += Sprintf("------------------------------------\n\n")
	}

	return content, 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
}