diff --git a/cmd/ru/main.go b/cmd/ru/main.go index cb4f254..2b68eff 100644 --- a/cmd/ru/main.go +++ b/cmd/ru/main.go @@ -3,10 +3,12 @@ package main import ( "flag" "fmt" + "log" "os" "time" "git.lattuga.net/blallo/ruspa/tree" + "golang.org/x/crypto/ssh/terminal" ) type UnitValue struct { @@ -42,6 +44,13 @@ func (u *UnitValue) Set(value string) error { } } +func min(a, b int) int { + if a < b { + return a + } + return b +} + func main() { var path string var depth int @@ -74,16 +83,26 @@ func main() { root = tree.NewTop(path) } root.SetUnit(unit.String()) + out := log.New(os.Stdout, "", 0) + 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 { select { case <-time.After(interval): if !firstRound { - fmt.Printf("\033[%dA", root.Depth()+2) + depth := min(root.Depth()+2, height) + out.Printf("\r\033[%dA", depth) + } else { + out.Print("\r\033[A") } - fmt.Println(root) + out.Print(root) firstRound = false if root.Complete() { return