Browse Source

Use the max height of the terminal

Blallo 4 years ago
parent
commit
644c71f7d9
1 changed files with 21 additions and 2 deletions
  1. 21 2
      cmd/ru/main.go

+ 21 - 2
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