mirror of
https://github.com/asciinema/asciinema.git
synced 2026-05-18 05:04:50 +02:00
live feedback on terminal size
This commit is contained in:
committed by
Marcin Kulik
parent
6683bdaa26
commit
d32827b103
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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...))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user