player

package module
v3.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 22, 2022 License: MIT Imports: 6 Imported by: 3

README

asciinema-player

Build Status codecov Go Report Card GoDev

asciinema-player is a library and cli-app to play terminal sessions recorded by asciinema (http://github.com/asciinema/asciinema)

Prerequisites

  • Golang >= 1.17

Installation

Library:

go get -v -u github.com/xakep666/asciinema-player

App:

go get -v -u github.com/xakep666/asciinema-player/cmd/asciinema-player

Usage

App
$ ./asciinema-player --help
  Usage of ./asciinema-player:
    -f string
          path to asciinema v2 file
    -maxWait duration
          maximum time between frames (default 2s)
    -speed float
          speed adjustment: <1 - increase, >1 - decrease (default 1)

For example you can play test session ./asciinema-player -f test.cast

asciicast

Library
frameSource, err := player.NewStreamFrameSource(reader)
if err != nil {
    return err
}

term, err := player.NewOSTerminal()
if err != nil {
    return err
}

defer term.Close()

player, err := player.NewPlayer(frameSource, terminal)
if err != nil {
    return err
}

err = player.Play()
if err != nil {
    return err
}

Library usage example is app, actually.

Examples

Renderer to GIF Web-based player for server-stored casts

License

Asciinema-player project is licensed under the terms of the MIT license. Please see LICENSE in this repository for more details.

Documentation

Index

Constants

View Source
const FormatVersion = 2

Variables

View Source
var (
	ErrUnexpectedVersion = fmt.Errorf("unexpected asciicast version")
	ErrSmallTerminal     = fmt.Errorf("terminal too small for frames")
)
View Source
var ErrNotTerminal = fmt.Errorf("stdin is not terminal")

Functions

This section is empty.

Types

type Frame

type Frame struct {
	// Time in seconds since record start.
	Time float64

	// Type of frame.
	Type FrameType

	// Data contains frame data.
	Data []byte
}

Frame represents asciinema-v2 frame. This is JSON-array with fixed size of 3 elements: [0]: frame delay in seconds (float64), [1]: frame type, [2]: frame data (escaped string).

func (*Frame) UnmarshalJSON

func (f *Frame) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

type FrameSource

type FrameSource interface {
	// Header returns asciinema-v2 header.
	Header() Header

	// Next advances to next available frame. It must return false if error occurs or there is no more frames.
	Next() bool

	// Frame returns current frame. It becomes unusable after Next call.
	Frame() Frame

	// Err returns error if it happens during iteration.
	Err() error
}

FrameSource describes frames source.

type FrameType

type FrameType string

FrameType is a type of Frame.

const (
	// InputFrame contains data sent from stdin of recorded shell.
	InputFrame FrameType = "i"

	// OutputFrame contains data written to stdout of recorded shell.
	OutputFrame FrameType = "o"
)

type FrameUnmarshalError

type FrameUnmarshalError struct {
	Description string
	Index       int
}

FrameUnmarshalError returned if frame conversion to struct failed.

func (*FrameUnmarshalError) Error

func (e *FrameUnmarshalError) Error() string
type Header struct {
	// Version is a format version. Must be 2.
	Version int `json:"version"`

	// With is a captured terminal width.
	Width int `json:"width"`

	// Height is a captured terminal height.
	Height int `json:"height"`
}

Header represents asciinema-v2 header (first line). It doesn't include unneeded fields.

type OSTerminal

type OSTerminal struct {
	// contains filtered or unexported fields
}

OSTerminal represents terminal on operating system.

func NewOSTerminal

func NewOSTerminal() (*OSTerminal, error)

NewOSTerminal constructs OSTerminal from stdin. It returns ErrNotTerminal if stdin is not terminal.

func NewOSTerminalFromFile

func NewOSTerminalFromFile(file *os.File) (*OSTerminal, error)

NewOSTerminalFromFile constructs OSTerminal from file. It returns ErrNotTerminal if file is not terminal.

func (*OSTerminal) Close

func (t *OSTerminal) Close() error

Close closes terminal (stop control loop). It doesn't close underlying file.

func (*OSTerminal) Control

func (t *OSTerminal) Control(control PlaybackControl)

func (*OSTerminal) Dimensions

func (t *OSTerminal) Dimensions() (width, height int)

func (*OSTerminal) Restore

func (t *OSTerminal) Restore() error

func (*OSTerminal) ToRaw

func (t *OSTerminal) ToRaw() error

func (*OSTerminal) Write

func (t *OSTerminal) Write(p []byte) (n int, err error)

type Option

type Option func(*options)

Option for Player.

func WithIgnoreSizeCheck

func WithIgnoreSizeCheck() Option

WithIgnoreSizeCheck turns off check that terminal can fit frames.

func WithMaxWait

func WithMaxWait(t time.Duration) Option

WithMaxWait sets minimal delay between frames. Zero or negative value are ignored.

func WithSpeed

func WithSpeed(speed float64) Option

WithSpeed sets playback speed. Values greater than 1 speeds up playback. Values between 0 and 1 slows down playback. Negative values are ignored.

type PlaybackControl

type PlaybackControl interface {
	// Pause pauses playback. If playback already paused it will continue.
	Pause()

	// Stop interrupts playback. Must be called once.
	Stop()
	// contains filtered or unexported methods
}

PlaybackControl describes playback control methods for Terminal.

type Player

type Player struct {
	// contains filtered or unexported fields
}

func NewPlayer

func NewPlayer(frameSource FrameSource, terminal Terminal, opts ...Option) (*Player, error)

func (*Player) Pause

func (p *Player) Pause()

Pause pauses playback. If playback already paused it will continue.

func (*Player) Start

func (p *Player) Start() (err error)

Start starts playback. Method blocks until Stop call.

func (*Player) Stop

func (p *Player) Stop()

Stop interrupts playback. Must be called once.

type StreamFrameSource

type StreamFrameSource struct {
	// contains filtered or unexported fields
}

StreamFrameSource reads frames from io.Reader.

func NewStreamFrameSource

func NewStreamFrameSource(reader io.Reader) (*StreamFrameSource, error)

NewStreamFrameSource constructs StreamFrameSource. It reads Header from input stream.

func (*StreamFrameSource) Err

func (s *StreamFrameSource) Err() error

Err returns error if it happens during iteration.

func (*StreamFrameSource) Frame

func (s *StreamFrameSource) Frame() Frame

Frame returns current frame. It becomes unusable after Next call.

func (*StreamFrameSource) Header

func (s *StreamFrameSource) Header() Header

Header returns asciinema-v2 header.

func (*StreamFrameSource) Next

func (s *StreamFrameSource) Next() bool

Next advances to next available frame. It must return false if error occurs or there is no more frames.

type Terminal

type Terminal interface {
	io.WriteCloser

	// Dimensions returns terminal window size.
	Dimensions() (width, height int)

	// ToRaw puts terminal to raw mode. Implementation must store previous terminal state.
	ToRaw() error

	// Restore puts terminal into state stored in ToRaw.
	Restore() error

	// Control starts "event loop" where Terminal may call methods of PlaybackControl. Method blocks until Close.
	Control(PlaybackControl)
}

Terminal is interface for terminal interaction.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL