package main import ( "fmt" "strconv" "strings" "time" "github.com/dustin/go-humanize" ) func printerSimple(ents chan Ent) { for ent := range ents { fmt.Println(nameJoin(ent.name)) } } func printerMachine(ents chan Ent) { for ent := range ents { fmt.Println( strconv.FormatUint(uint64(ent.size), 10), time.Unix(int64(ent.mtime), 0).Format("2006-01-02T15:04:05"), nameJoin(ent.name), ) } } type TreePrintEnt struct { ent Ent isLast bool } func laster(ents chan Ent, trees chan TreePrintEnt) { entPrev := <-ents for ent := range ents { tree := TreePrintEnt{ent: entPrev} if len(ent.name) < len(entPrev.name) { tree.isLast = true } trees <- tree entPrev = ent } trees <- TreePrintEnt{ent: entPrev} close(trees) } func printerTree(ents chan Ent) { trees := make(chan TreePrintEnt, 1<<10) go laster(ents, trees) first := true var box string for ent := range trees { if first { fmt.Printf( "%s\t[%s]\n", nameJoin(ent.ent.name), humanize.IBytes(uint64(ent.ent.size)), ) first = false continue } if ent.isLast { box = "└" } else { box = "├" } fmt.Printf("%s%s %s\t[%s] %s\n", strings.Repeat("│ ", len(ent.ent.name)-2), box, nameJoin(ent.ent.name), humanize.IBytes(uint64(ent.ent.size)), time.Unix(ent.ent.mtime, 0).Format("2006-01-02"), ) } }