This commit is contained in:
Marcin Kulik
2016-02-21 16:58:39 +01:00
parent 6b7d69e1db
commit 259f858d4f
7 changed files with 81 additions and 12 deletions

View File

@@ -8,6 +8,7 @@
* `-q/--quiet` option added to `rec` command
* Fixed handling of partial UTF-8 sequences in recorded stdout
* Final "exit" is now removed from recorded stdout
* Longer operations like uploading/downloading show "spinner"
## 1.1.1 (2015-06-21)

View File

@@ -7,7 +7,7 @@ import (
)
type Player interface {
Play(string, uint) error
Play(*Asciicast, uint) error
}
type AsciicastPlayer struct {
@@ -18,12 +18,7 @@ func NewPlayer() Player {
return &AsciicastPlayer{Terminal: terminal.NewTerminal()}
}
func (r *AsciicastPlayer) Play(path string, maxWait uint) error {
asciicast, err := Load(path)
if err != nil {
return err
}
func (r *AsciicastPlayer) Play(asciicast *Asciicast, maxWait uint) error {
for _, frame := range asciicast.Stdout {
delay := frame.Delay
if maxWait > 0 && delay > float64(maxWait) {

View File

@@ -1,6 +1,9 @@
package commands
import "github.com/asciinema/asciinema/asciicast"
import (
"github.com/asciinema/asciinema/asciicast"
"github.com/asciinema/asciinema/util"
)
type PlayCommand struct {
Player asciicast.Player
@@ -12,6 +15,17 @@ func NewPlayCommand() *PlayCommand {
}
}
func (c *PlayCommand) Execute(filename string, maxWait uint) error {
return c.Player.Play(filename, maxWait)
func (c *PlayCommand) Execute(url string, maxWait uint) error {
var cast *asciicast.Asciicast
var err error
util.WithSpinner(500, func() {
cast, err = asciicast.Load(url)
})
if err != nil {
return err
}
return c.Player.Play(cast, maxWait)
}

View File

@@ -49,7 +49,12 @@ func (c *RecordCommand) Execute(command, title string, assumeYes bool, maxWait u
util.ReadLine()
}
url, warn, err := c.API.UploadAsciicast(filename)
var url, warn string
var err error
util.WithSpinner(0, func() {
url, warn, err = c.API.UploadAsciicast(filename)
})
if warn != "" {
util.Warningf(warn)

View File

@@ -18,7 +18,12 @@ func NewUploadCommand(api api.API) *UploadCommand {
}
func (c *UploadCommand) Execute(filename string) error {
url, warn, err := c.API.UploadAsciicast(filename)
var url, warn string
var err error
util.WithSpinner(0, func() {
url, warn, err = c.API.UploadAsciicast(filename)
})
if warn != "" {
util.Warningf(warn)

14
main.go
View File

@@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"os/signal"
"strconv"
"strings"
@@ -92,6 +93,10 @@ func environment() map[string]string {
return env
}
func showCursorBack() {
fmt.Fprintf(os.Stdout, "\x1b[?25h")
}
func main() {
env := environment()
@@ -100,6 +105,15 @@ func main() {
os.Exit(1)
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
showCursorBack()
os.Exit(1)
}()
defer showCursorBack()
cfg, err := util.GetConfig(env)
if err != nil {
fmt.Println(err)

35
util/spinner.go Normal file
View File

@@ -0,0 +1,35 @@
package util
import (
"fmt"
"os"
"time"
)
var spinner = []rune("▉▊▋▌▍▎▏▎▍▌▋▊▉")
func WithSpinner(delay int, f func()) {
stopChan := make(chan struct{})
go func() {
<-time.After(time.Duration(delay) * time.Millisecond)
i := 0
fmt.Fprintf(os.Stdout, "\x1b[?25l") // hide cursor
for {
select {
case <-stopChan:
return
case <-time.After(100 * time.Millisecond):
fmt.Fprintf(os.Stdout, "\r%c", spinner[i])
i = (i + 1) % len(spinner)
}
}
}()
f()
close(stopChan)
fmt.Fprintf(os.Stdout, "\r\x1b[K\x1b[?25h") // clear line and show cursor back
}