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