mirror of
https://github.com/asciinema/asciinema.git
synced 2025-12-15 19:28:00 +01:00
Spinner
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
14
main.go
@@ -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
35
util/spinner.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user