Use the max height of the terminal

This commit is contained in:
Blallo 2020-03-08 21:43:01 +01:00
parent 36dbfead9c
commit 644c71f7d9
No known key found for this signature in database
GPG key ID: 0CBE577C9B72DC3F

View file

@ -3,10 +3,12 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"log"
"os" "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 {
@ -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() { func main() {
var path string var path string
var depth int var depth int
@ -74,16 +83,26 @@ 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 firstRound := true
for { for {
select { select {
case <-time.After(interval): case <-time.After(interval):
if !firstRound { 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 firstRound = false
if root.Complete() { if root.Complete() {
return return