Compare commits
No commits in common. "644c71f7d991422dfe6fef6c72883d805f09d37a" and "d00a7fa2c04ada007f74d828a14b122f8863975c" have entirely different histories.
644c71f7d9
...
d00a7fa2c0
2 changed files with 7 additions and 72 deletions
|
@ -3,12 +3,9 @@ package main
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.lattuga.net/blallo/ruspa/tree"
|
"git.lattuga.net/blallo/ruspa/tree"
|
||||||
"golang.org/x/crypto/ssh/terminal"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type UnitValue struct {
|
type UnitValue struct {
|
||||||
|
@ -44,38 +41,15 @@ func (u *UnitValue) Set(value string) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func min(a, b int) int {
|
|
||||||
if a < b {
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var path string
|
var path string
|
||||||
var depth int
|
var depth int
|
||||||
var root tree.Node
|
var root tree.Node
|
||||||
var interval time.Duration
|
|
||||||
var unit = &UnitValue{unit: "KB"}
|
var unit = &UnitValue{unit: "KB"}
|
||||||
cli := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
|
flag.StringVar(&path, "path", ".", "Path from where to start the walk from")
|
||||||
cli.IntVar(&depth, "depth", 0, "Depth to display")
|
flag.IntVar(&depth, "depth", 0, "Depth to display")
|
||||||
cli.Var(unit, "unit", "Unit in which to report size")
|
flag.Var(unit, "unit", "Unit in which to report size")
|
||||||
cli.DurationVar(&interval, "interval", 100*time.Millisecond, "The update interval")
|
flag.Parse()
|
||||||
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) {
|
if depth == 0 || !tree.AnyDirectoryDownThere(path) {
|
||||||
root = tree.NewSingle(path)
|
root = tree.NewSingle(path)
|
||||||
|
@ -83,27 +57,13 @@ func main() {
|
||||||
root = tree.NewTop(path)
|
root = tree.NewTop(path)
|
||||||
}
|
}
|
||||||
root.SetUnit(unit.String())
|
root.SetUnit(unit.String())
|
||||||
out := log.New(os.Stdout, "", 0)
|
|
||||||
|
|
||||||
go root.Spawn(depth)
|
go root.Spawn(depth)
|
||||||
_, height, err := terminal.GetSize(int(os.Stdin.Fd()))
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintln(os.Stderr, "Could not get terminal size")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
firstRound := true
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-time.After(interval):
|
case <-time.After(500 * time.Millisecond):
|
||||||
if !firstRound {
|
fmt.Printf("\033[H\033[2J")
|
||||||
depth := min(root.Depth()+2, height)
|
fmt.Println(root)
|
||||||
out.Printf("\r\033[%dA", depth)
|
|
||||||
} else {
|
|
||||||
out.Print("\r\033[A")
|
|
||||||
}
|
|
||||||
out.Print(root)
|
|
||||||
firstRound = false
|
|
||||||
if root.Complete() {
|
if root.Complete() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,6 @@ type Node interface {
|
||||||
Level() int
|
Level() int
|
||||||
Name() string
|
Name() string
|
||||||
Complete() bool
|
Complete() bool
|
||||||
Depth() int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////
|
//////////////////////////////
|
||||||
|
@ -129,14 +128,6 @@ func (t *Top) Complete() bool {
|
||||||
return true
|
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 {
|
func (t *Top) String() string {
|
||||||
var out string
|
var out string
|
||||||
var lines []string
|
var lines []string
|
||||||
|
@ -289,14 +280,6 @@ func (i *Intermediate) Complete() bool {
|
||||||
return true
|
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 {
|
func (i *Intermediate) String() string {
|
||||||
var lines []string
|
var lines []string
|
||||||
out := fmt.Sprintf("(%s) %s\n", fmtSize(i.Size(), i.GetUnit()), i.Name())
|
out := fmt.Sprintf("(%s) %s\n", fmtSize(i.Size(), i.GetUnit()), i.Name())
|
||||||
|
@ -426,10 +409,6 @@ func (b *Bottom) Complete() bool {
|
||||||
return b.complete
|
return b.complete
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bottom) Depth() int {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bottom) String() string {
|
func (b *Bottom) String() string {
|
||||||
return fmt.Sprintf("(%s) %s", fmtSize(b.Size(), b.GetUnit()), b.Name())
|
return fmt.Sprintf("(%s) %s", fmtSize(b.Size(), b.GetUnit()), b.Name())
|
||||||
}
|
}
|
||||||
|
@ -497,10 +476,6 @@ func (s *Single) Complete() bool {
|
||||||
return s.complete
|
return s.complete
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Single) Depth() int {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Single) String() string {
|
func (s *Single) String() string {
|
||||||
return fmt.Sprintf("(%s) %s\n", fmtSize(s.Size(), s.GetUnit()), s.Name())
|
return fmt.Sprintf("(%s) %s\n", fmtSize(s.Size(), s.GetUnit()), s.Name())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue