]> Sergey Matveev's repositories - glocate.git/blob - printers.go
Use database's directory for temporary files
[glocate.git] / printers.go
1 package main
2
3 import (
4         "fmt"
5         "strconv"
6         "strings"
7         "time"
8
9         "github.com/dustin/go-humanize"
10 )
11
12 func printerSimple(ents chan Ent) {
13         for ent := range ents {
14                 fmt.Println(nameJoin(ent.name))
15         }
16 }
17
18 func printerMachine(ents chan Ent) {
19         for ent := range ents {
20                 fmt.Println(
21                         strconv.FormatUint(uint64(ent.size), 10),
22                         time.Unix(int64(ent.mtime), 0).Format("2006-01-02T15:04:05"),
23                         nameJoin(ent.name),
24                 )
25         }
26 }
27
28 type TreePrintEnt struct {
29         ent    Ent
30         isLast bool
31 }
32
33 func laster(ents chan Ent, trees chan TreePrintEnt) {
34         entPrev := <-ents
35         for ent := range ents {
36                 tree := TreePrintEnt{ent: entPrev}
37                 if len(ent.name) < len(entPrev.name) {
38                         tree.isLast = true
39                 }
40                 trees <- tree
41                 entPrev = ent
42         }
43         trees <- TreePrintEnt{ent: entPrev}
44         close(trees)
45 }
46
47 func printerTree(ents chan Ent) {
48         trees := make(chan TreePrintEnt, 1<<10)
49         go laster(ents, trees)
50         first := true
51         var box string
52         for ent := range trees {
53                 if first {
54                         fmt.Printf(
55                                 "%s\t[%s]\n", nameJoin(ent.ent.name),
56                                 humanize.IBytes(uint64(ent.ent.size)),
57                         )
58                         first = false
59                         continue
60                 }
61                 if ent.isLast {
62                         box = "└"
63                 } else {
64                         box = "├"
65                 }
66                 fmt.Printf("%s%s %s\t[%s] %s\n",
67                         strings.Repeat("│ ", len(ent.ent.name)-2), box,
68                         nameJoin(ent.ent.name), humanize.IBytes(uint64(ent.ent.size)),
69                         time.Unix(ent.ent.mtime, 0).Format("2006-01-02"),
70                 )
71         }
72 }