ruspa/cmd/ru/main.go

94 lines
1.7 KiB
Go
Raw Normal View History

2020-03-01 23:09:00 +01:00
package main
import (
"flag"
"fmt"
2020-03-08 20:58:13 +01:00
"os"
2020-03-01 23:09:00 +01:00
"time"
"git.lattuga.net/blallo/ruspa/tree"
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:
return tree.ErrUnknownUnit
2020-03-03 14:22:12 +01:00
}
}
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)
} else {
2020-03-07 17:23:06 +01:00
root = tree.NewTop(path)
}
2020-03-07 17:23:06 +01:00
root.SetUnit(unit.String())
go root.Spawn(depth)
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 {
fmt.Printf("\033[%dA", root.Depth()+2)
}
2020-03-07 17:23:06 +01:00
fmt.Println(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
}
}
}