live feedback on terminal size

This commit is contained in:
Cole Mickens
2016-02-03 22:49:49 -08:00
committed by Marcin Kulik
parent 6683bdaa26
commit d32827b103
2 changed files with 50 additions and 4 deletions

View File

@@ -1,10 +1,20 @@
package asciicast
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/asciinema/asciinema/terminal"
"github.com/asciinema/asciinema/util"
)
const (
optimalCols = 120
optimalRows = 30
)
type Recorder interface {
Record(string, string, string, float64, bool, map[string]string) error
}
@@ -17,17 +27,49 @@ func NewRecorder() Recorder {
return &AsciicastRecorder{Terminal: terminal.NewTerminal()}
}
func (r *AsciicastRecorder) checkTerminalSize() chan<- bool {
rows, cols, _ := r.Terminal.Size()
doneChan := make(chan bool)
optimalSize := fmt.Sprintf("%s%dx%d%s", "\x1b[0m\x1b[32m", optimalCols, optimalRows, "\x1b[0m\x1b[33m")
go func() {
winch := make(chan os.Signal, 1)
signal.Notify(winch, syscall.SIGWINCH)
defer signal.Stop(winch)
defer close(winch)
defer close(doneChan)
for {
select {
case <-winch:
newRows, newCols, _ := r.Terminal.Size()
if cols != newCols || rows != newRows {
cols, rows = newCols, newRows
currentSize := fmt.Sprintf("%dx%d", cols, rows)
if cols == optimalCols && rows == optimalRows {
currentSize = fmt.Sprintf("%s%dx%d%s", "\x1b[0m\x1b[32m", cols, rows, "\x1b[0m\x1b[33m")
}
util.ReplaceWarningf("Current terminal size is %s. Optimal is %s.", currentSize, optimalSize)
}
case <-doneChan:
return
}
}
}()
return doneChan
}
func (r *AsciicastRecorder) Record(path, command, title string, maxWait float64, assumeYes bool, env map[string]string) error {
// TODO: touch savePath to ensure writing is possible
rows, cols, _ := r.Terminal.Size()
if rows > 30 || cols > 120 {
util.Warningf("Current terminal size is %vx%v.", cols, rows)
util.Warningf("It may be too big to be properly replayed on smaller screens.")
if rows != optimalRows || cols != optimalCols {
if !assumeYes {
doneChan := r.checkTerminalSize()
util.Warningf("Current terminal size is %vx%v. Optimal is %vx%v", cols, rows, optimalCols, optimalRows)
util.Warningf("You can now resize it. Press <Enter> to start recording.")
util.ReadLine()
doneChan <- true
}
}

View File

@@ -17,6 +17,10 @@ func Printf(s string, args ...interface{}) {
fmt.Fprintf(loggerOutput, "\x1b[32m~ %v\x1b[0m\n", fmt.Sprintf(s, args...))
}
func ReplaceWarningf(s string, args ...interface{}) {
fmt.Fprintf(loggerOutput, "\r\x1b[33m~ %v\x1b[0m", fmt.Sprintf(s, args...))
}
func Warningf(s string, args ...interface{}) {
fmt.Fprintf(loggerOutput, "\x1b[33m~ %v\x1b[0m\n", fmt.Sprintf(s, args...))
}