mirror of
https://github.com/asciinema/asciinema.git
synced 2025-12-16 19:58:03 +01:00
Spinner
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
* `-q/--quiet` option added to `rec` command
|
* `-q/--quiet` option added to `rec` command
|
||||||
* Fixed handling of partial UTF-8 sequences in recorded stdout
|
* Fixed handling of partial UTF-8 sequences in recorded stdout
|
||||||
* Final "exit" is now removed from recorded stdout
|
* Final "exit" is now removed from recorded stdout
|
||||||
|
* Longer operations like uploading/downloading show "spinner"
|
||||||
|
|
||||||
## 1.1.1 (2015-06-21)
|
## 1.1.1 (2015-06-21)
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Player interface {
|
type Player interface {
|
||||||
Play(string, uint) error
|
Play(*Asciicast, uint) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type AsciicastPlayer struct {
|
type AsciicastPlayer struct {
|
||||||
@@ -18,12 +18,7 @@ func NewPlayer() Player {
|
|||||||
return &AsciicastPlayer{Terminal: terminal.NewTerminal()}
|
return &AsciicastPlayer{Terminal: terminal.NewTerminal()}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *AsciicastPlayer) Play(path string, maxWait uint) error {
|
func (r *AsciicastPlayer) Play(asciicast *Asciicast, maxWait uint) error {
|
||||||
asciicast, err := Load(path)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, frame := range asciicast.Stdout {
|
for _, frame := range asciicast.Stdout {
|
||||||
delay := frame.Delay
|
delay := frame.Delay
|
||||||
if maxWait > 0 && delay > float64(maxWait) {
|
if maxWait > 0 && delay > float64(maxWait) {
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import "github.com/asciinema/asciinema/asciicast"
|
import (
|
||||||
|
"github.com/asciinema/asciinema/asciicast"
|
||||||
|
"github.com/asciinema/asciinema/util"
|
||||||
|
)
|
||||||
|
|
||||||
type PlayCommand struct {
|
type PlayCommand struct {
|
||||||
Player asciicast.Player
|
Player asciicast.Player
|
||||||
@@ -12,6 +15,17 @@ func NewPlayCommand() *PlayCommand {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *PlayCommand) Execute(filename string, maxWait uint) error {
|
func (c *PlayCommand) Execute(url string, maxWait uint) error {
|
||||||
return c.Player.Play(filename, maxWait)
|
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()
|
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 != "" {
|
if warn != "" {
|
||||||
util.Warningf(warn)
|
util.Warningf(warn)
|
||||||
|
|||||||
@@ -18,7 +18,12 @@ func NewUploadCommand(api api.API) *UploadCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *UploadCommand) Execute(filename string) error {
|
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 != "" {
|
if warn != "" {
|
||||||
util.Warningf(warn)
|
util.Warningf(warn)
|
||||||
|
|||||||
14
main.go
14
main.go
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -92,6 +93,10 @@ func environment() map[string]string {
|
|||||||
return env
|
return env
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func showCursorBack() {
|
||||||
|
fmt.Fprintf(os.Stdout, "\x1b[?25h")
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
env := environment()
|
env := environment()
|
||||||
|
|
||||||
@@ -100,6 +105,15 @@ func main() {
|
|||||||
os.Exit(1)
|
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)
|
cfg, err := util.GetConfig(env)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
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