2020-03-01 23:09:00 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2020-03-08 21:43:01 +01:00
|
|
|
"log"
|
2020-03-08 20:58:13 +01:00
|
|
|
"os"
|
2020-03-01 23:09:00 +01:00
|
|
|
"time"
|
2020-03-05 14:32:50 +01:00
|
|
|
|
|
|
|
"git.lattuga.net/blallo/ruspa/tree"
|
2020-03-08 21:43:01 +01:00
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
2020-03-01 23:09:00 +01:00
|
|
|
)
|
|
|
|
|
2020-03-03 14:22:12 +01:00
|
|
|
type UnitValue struct {
|
|
|
|
unit string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UnitValue) String() string {
|
|
|
|
return u.unit
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UnitValue) Set(value string) error {
|
|
|
|
switch value {
|
|
|
|
case "B":
|
|
|
|
u.unit = "B"
|
|
|
|
return nil
|
|
|
|
case "KB":
|
|
|
|
u.unit = "KB"
|
|
|
|
return nil
|
|
|
|
case "MB":
|
|
|
|
u.unit = "MB"
|
|
|
|
return nil
|
|
|
|
case "GB":
|
|
|
|
u.unit = "GB"
|
|
|
|
return nil
|
|
|
|
case "TB":
|
|
|
|
u.unit = "TB"
|
|
|
|
return nil
|
|
|
|
case "PB":
|
|
|
|
u.unit = "PB"
|
|
|
|
return nil
|
|
|
|
default:
|
2020-03-05 14:32:50 +01:00
|
|
|
return tree.ErrUnknownUnit
|
2020-03-03 14:22:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 21:43:01 +01:00
|
|
|
func min(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2020-03-01 23:09:00 +01:00
|
|
|
func main() {
|
|
|
|
var path string
|
|
|
|
var depth int
|
2020-03-07 17:23:06 +01:00
|
|
|
var root tree.Node
|
2020-03-08 20:58:13 +01:00
|
|
|
var interval time.Duration
|
2020-03-03 14:22:12 +01:00
|
|
|
var unit = &UnitValue{unit: "KB"}
|
2020-03-08 20:58:13 +01:00
|
|
|
cli := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
|
|
|
|
cli.IntVar(&depth, "depth", 0, "Depth to display")
|
|
|
|
cli.Var(unit, "unit", "Unit in which to report size")
|
|
|
|
cli.DurationVar(&interval, "interval", 100*time.Millisecond, "The update interval")
|
|
|
|
cli.Usage = func() {
|
|
|
|
fmt.Fprintf(cli.Output(), "Usage:\n%s [opts] [PATH]\n\n PATH: the root path to start from. Defaults to $PWD.\n\nopts:\n", os.Args[0])
|
|
|
|
cli.PrintDefaults()
|
|
|
|
}
|
|
|
|
cli.Parse(os.Args[1:])
|
|
|
|
|
|
|
|
switch narg := cli.NArg(); narg {
|
|
|
|
case 0:
|
|
|
|
path = "."
|
|
|
|
case 1:
|
|
|
|
path = cli.Args()[0]
|
|
|
|
default:
|
|
|
|
fmt.Fprintln(os.Stderr, "Too many arguments")
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2020-03-01 23:09:00 +01:00
|
|
|
|
2020-03-07 17:23:06 +01:00
|
|
|
if depth == 0 || !tree.AnyDirectoryDownThere(path) {
|
|
|
|
root = tree.NewSingle(path)
|
2020-03-05 14:32:50 +01:00
|
|
|
} else {
|
2020-03-07 17:23:06 +01:00
|
|
|
root = tree.NewTop(path)
|
2020-03-05 14:32:50 +01:00
|
|
|
}
|
2020-03-07 17:23:06 +01:00
|
|
|
root.SetUnit(unit.String())
|
2020-03-08 21:43:01 +01:00
|
|
|
out := log.New(os.Stdout, "", 0)
|
|
|
|
|
2020-03-07 17:23:06 +01:00
|
|
|
go root.Spawn(depth)
|
2020-03-08 21:43:01 +01:00
|
|
|
_, height, err := terminal.GetSize(int(os.Stdin.Fd()))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "Could not get terminal size")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-03-07 17:23:06 +01:00
|
|
|
|
2020-03-08 20:58:13 +01:00
|
|
|
firstRound := true
|
2020-03-07 17:23:06 +01:00
|
|
|
for {
|
2020-03-01 23:09:00 +01:00
|
|
|
select {
|
2020-03-08 20:58:13 +01:00
|
|
|
case <-time.After(interval):
|
|
|
|
if !firstRound {
|
2020-03-08 21:43:01 +01:00
|
|
|
depth := min(root.Depth()+2, height)
|
|
|
|
out.Printf("\r\033[%dA", depth)
|
|
|
|
} else {
|
|
|
|
out.Print("\r\033[A")
|
2020-03-08 20:58:13 +01:00
|
|
|
}
|
2020-03-08 21:43:01 +01:00
|
|
|
out.Print(root)
|
2020-03-08 20:58:13 +01:00
|
|
|
firstRound = false
|
2020-03-07 17:23:06 +01:00
|
|
|
if root.Complete() {
|
|
|
|
return
|
|
|
|
}
|
2020-03-01 23:09:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|