]> Sergey Matveev's repositories - glocate.git/blob - printers.go
Unify copyright comment format
[glocate.git] / printers.go
1 // glocate -- ZFS-diff-friendly locate-like utility
2 // Copyright (C) 2022-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package main
17
18 import (
19         "fmt"
20         "strconv"
21         "strings"
22         "time"
23
24         "github.com/dustin/go-humanize"
25 )
26
27 func printerSimple(ents chan Ent) {
28         for ent := range ents {
29                 fmt.Println(nameJoin(ent.name))
30         }
31 }
32
33 func printerMachine(ents chan Ent) {
34         for ent := range ents {
35                 fmt.Println(
36                         strconv.FormatUint(uint64(ent.size), 10),
37                         time.Unix(int64(ent.mtime), 0).Format("2006-01-02T15:04:05"),
38                         nameJoin(ent.name),
39                 )
40         }
41 }
42
43 type TreePrintEnt struct {
44         ent    Ent
45         isLast bool
46 }
47
48 func laster(ents chan Ent, trees chan TreePrintEnt) {
49         entPrev := <-ents
50         for ent := range ents {
51                 tree := TreePrintEnt{ent: entPrev}
52                 if len(ent.name) < len(entPrev.name) {
53                         tree.isLast = true
54                 }
55                 trees <- tree
56                 entPrev = ent
57         }
58         trees <- TreePrintEnt{ent: entPrev}
59         close(trees)
60 }
61
62 func printerTree(ents chan Ent) {
63         trees := make(chan TreePrintEnt, 1<<10)
64         go laster(ents, trees)
65         first := true
66         var box string
67         for ent := range trees {
68                 if first {
69                         fmt.Printf(
70                                 "%s\t[%s]\n", nameJoin(ent.ent.name),
71                                 humanize.IBytes(uint64(ent.ent.size)),
72                         )
73                         first = false
74                         continue
75                 }
76                 if ent.isLast {
77                         box = "└"
78                 } else {
79                         box = "├"
80                 }
81                 fmt.Printf("%s%s %s\t[%s] %s\n",
82                         strings.Repeat("│ ", len(ent.ent.name)-2), box,
83                         nameJoin(ent.ent.name), humanize.IBytes(uint64(ent.ent.size)),
84                         time.Unix(ent.ent.mtime, 0).Format("2006-01-02"),
85                 )
86         }
87 }