Improve cli experience
This commit is contained in:
parent
d00a7fa2c0
commit
36dbfead9c
2 changed files with 52 additions and 6 deletions
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"git.lattuga.net/blallo/ruspa/tree"
|
||||
|
@ -45,11 +46,27 @@ func main() {
|
|||
var path string
|
||||
var depth int
|
||||
var root tree.Node
|
||||
var interval time.Duration
|
||||
var unit = &UnitValue{unit: "KB"}
|
||||
flag.StringVar(&path, "path", ".", "Path from where to start the walk from")
|
||||
flag.IntVar(&depth, "depth", 0, "Depth to display")
|
||||
flag.Var(unit, "unit", "Unit in which to report size")
|
||||
flag.Parse()
|
||||
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)
|
||||
}
|
||||
|
||||
if depth == 0 || !tree.AnyDirectoryDownThere(path) {
|
||||
root = tree.NewSingle(path)
|
||||
|
@ -59,11 +76,15 @@ func main() {
|
|||
root.SetUnit(unit.String())
|
||||
go root.Spawn(depth)
|
||||
|
||||
firstRound := true
|
||||
for {
|
||||
select {
|
||||
case <-time.After(500 * time.Millisecond):
|
||||
fmt.Printf("\033[H\033[2J")
|
||||
case <-time.After(interval):
|
||||
if !firstRound {
|
||||
fmt.Printf("\033[%dA", root.Depth()+2)
|
||||
}
|
||||
fmt.Println(root)
|
||||
firstRound = false
|
||||
if root.Complete() {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -35,6 +35,7 @@ type Node interface {
|
|||
Level() int
|
||||
Name() string
|
||||
Complete() bool
|
||||
Depth() int
|
||||
}
|
||||
|
||||
//////////////////////////////
|
||||
|
@ -128,6 +129,14 @@ func (t *Top) Complete() bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func (t *Top) Depth() int {
|
||||
var depth int
|
||||
for _, child := range t.tree {
|
||||
depth += child.Depth()
|
||||
}
|
||||
return depth
|
||||
}
|
||||
|
||||
func (t *Top) String() string {
|
||||
var out string
|
||||
var lines []string
|
||||
|
@ -280,6 +289,14 @@ func (i *Intermediate) Complete() bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func (i *Intermediate) Depth() int {
|
||||
var depth int
|
||||
for _, child := range i.tree {
|
||||
depth += child.Depth()
|
||||
}
|
||||
return depth
|
||||
}
|
||||
|
||||
func (i *Intermediate) String() string {
|
||||
var lines []string
|
||||
out := fmt.Sprintf("(%s) %s\n", fmtSize(i.Size(), i.GetUnit()), i.Name())
|
||||
|
@ -409,6 +426,10 @@ func (b *Bottom) Complete() bool {
|
|||
return b.complete
|
||||
}
|
||||
|
||||
func (b *Bottom) Depth() int {
|
||||
return 1
|
||||
}
|
||||
|
||||
func (b *Bottom) String() string {
|
||||
return fmt.Sprintf("(%s) %s", fmtSize(b.Size(), b.GetUnit()), b.Name())
|
||||
}
|
||||
|
@ -476,6 +497,10 @@ func (s *Single) Complete() bool {
|
|||
return s.complete
|
||||
}
|
||||
|
||||
func (s *Single) Depth() int {
|
||||
return 1
|
||||
}
|
||||
|
||||
func (s *Single) String() string {
|
||||
return fmt.Sprintf("(%s) %s\n", fmtSize(s.Size(), s.GetUnit()), s.Name())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue