asciinema_parser

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2023 License: MIT Imports: 9 Imported by: 0

README

asciinema parser

English Document 中文文档

1. What is this? What problem was solved?

This website https://asciinema.org/ is record and share your terminal session website, it defines a set of Ascii Cast format files to store screen content, this library is used to parse Ascii Cast files,support v1 and v2 two versions.

2. API code examples

2.1 Check the version of the screen recording file

package main

import (
	"context"
	"fmt"
	asciinema_parser "github.com/golang-infrastructure/go-asciinema-parser"
)

func main() {

	asciiCastV2String := `{"version": 2, "width": 80, "height": 24, "timestamp": 1504467315, "title": "Demo", "env": {"TERM": "xterm-256color", "SHELL": "/bin/zsh"}}
[0.248848, "o", "\u001b[1;31mHello \u001b[32mWorld!\u001b[0m\n"]
[1.001376, "o", "That was ok\rThis is better."]
[2.143733, "o", " "]
[6.541828, "o", "Bye!"]`

	version, err := asciinema_parser.DetectVersion(context.Background(), asciiCastV2String)
	if err != nil {
		panic(err)
	}
	fmt.Println(version) // Output: 2

}

2.2 Parse V1 format of the screen recording software

package main

import (
	"context"
	"fmt"
	asciinema_parser "github.com/golang-infrastructure/go-asciinema-parser"
)

func main() {

	asciiCastV1String := `{
  "version": 1,
  "width": 80,
  "height": 24,
  "duration": 1.515658,
  "command": "/bin/zsh",
  "title": "",
  "env": {
    "TERM": "xterm-256color",
    "SHELL": "/bin/zsh"
  },
  "stdout": [
    [
      0.248848,
      "\u001b[1;31mHello \u001b[32mWorld!\u001b[0m\n"
    ],
    [
      1.001376,
      "I am \rThis is on the next line."
    ]
  ]
}`

	v1, err := asciinema_parser.ParseV1(context.Background(), []byte(asciiCastV1String))
	if err != nil {
		panic(err)
	}
	fmt.Println(fmt.Sprintf("%v", v1))

}

2.3 Parse V2 format of the screen recording software

package main

import (
	"context"
	"fmt"
	asciinema_parser "github.com/golang-infrastructure/go-asciinema-parser"
)

func main() {

	asciiCastV2String := `{"version": 2, "width": 80, "height": 24, "timestamp": 1504467315, "title": "Demo", "env": {"TERM": "xterm-256color", "SHELL": "/bin/zsh"}}
[0.248848, "o", "\u001b[1;31mHello \u001b[32mWorld!\u001b[0m\n"]
[1.001376, "o", "That was ok\rThis is better."]
[2.143733, "o", " "]
[6.541828, "o", "Bye!"]`

	v2, err := asciinema_parser.ParseV2(context.Background(), []byte(asciiCastV2String))
	if err != nil {
		panic(err)
	}
	fmt.Println(fmt.Sprintf("%v", v2))

}

Documentation

Index

Constants

View Source
const (
	EventTypeUnknown = ""
	EventTypeOutput  = "o"
	EventTypeInput   = "i"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AsciiCastV1

type AsciiCastV1 struct {
	Version Version `json:"version"`

	Width        uint              `json:"width" yaml:"width" mapstructure:"width"`
	Height       uint              `json:"height" yaml:"height" mapstructure:"height"`
	Duration     float64           `json:"duration" yaml:"duration" mapstructure:"duration"`
	Command      string            `json:"command" yaml:"command" mapstructure:"command"`
	Title        string            `json:"title" yaml:"title" mapstructure:"title"`
	Env          map[string]string `json:"env" yaml:"env" mapstructure:"env"`
	StdoutFrames []Frame           `json:"stdout" yaml:"stdout" mapstructure:"stdout"`
}

AsciiCastV1 v1格式的规范文档: https://github.com/asciinema/asciinema/blob/develop/doc/asciicast-v1.md

func ParseV1

func ParseV1(ctx context.Context, asciiCastV1Bytes []byte) (*AsciiCastV1, error)

ParseV1 解析V1版本的Ascii Cast 文件 https://github.com/asciinema/asciinema/blob/develop/doc/asciicast-v1.md

func (*AsciiCastV1) Check

func (x *AsciiCastV1) Check() error

type AsciiCastV2

type AsciiCastV2 struct {

	// 表示文件的格式版本
	Version Version `json:"version" yaml:"version" mapstructure:"version"`

	// 屏幕的宽度,但是是列
	Width uint `json:"width" yaml:"width" mapstructure:"width"`

	// 屏幕的高度,但是是行
	Height uint `json:"height" yaml:"height" mapstructure:"height"`

	// 录制开始时间
	Timestamp uint64 `json:"timestamp" yaml:"timestamp" mapstructure:"timestamp"`

	// 录制持续时间
	Duration float64 `json:"duration" yaml:"duration" mapstructure:"duration"`

	Command string `json:"command" yaml:"command" mapstructure:"command"`

	Title string `json:"title" yaml:"title" mapstructure:"title"`

	Theme *Theme `json:"theme" yaml:"theme" mapstructure:"theme"`

	//
	IdleTimeLimit float64 `json:"idle_time_limit" yaml:"idle_time_limit" mapstructure:"idle_time_limit"`

	// 相关环境变量
	EnvMap map[string]string `json:"env" yaml:"env" mapstructure:"env"`

	EventStream []*Event `json:"event_stream" yaml:"event_stream" mapstructure:"event_stream"`
	// contains filtered or unexported fields
}

格式规范文档: https://github.com/asciinema/asciinema/blob/develop/doc/asciicast-v2.md

func ParseV2

func ParseV2(ctx context.Context, asciiCastV2Bytes []byte) (*AsciiCastV2, error)

ParseV2 解析V2版本的Ascii Cast文件 v2格式规范的文档: https://github.com/asciinema/asciinema/blob/develop/doc/asciicast-v2.md

func (*AsciiCastV2) GetTime

func (x *AsciiCastV2) GetTime() time.Time

type Event

type Event struct {

	// 时间发生的时间距开始时间的偏移
	Delay float64 `json:"delay" yaml:"delay" mapstructure:"delay"`

	// 事件的类型,v2的话是有output和input
	EventType EventType `json:"event_type" yaml:"event_type" mapstructure:"event_type"`

	// 事件的数据,通常是显示的内容
	EventData string `json:"event_data" yaml:"event_data" mapstructure:"event_data"`
	// contains filtered or unexported fields
}

Event 事件流中的某一个事件

func (*Event) GetAsciiCast

func (x *Event) GetAsciiCast() *AsciiCastV2

func (*Event) GetEventTime

func (x *Event) GetEventTime() time.Time

type EventType

type EventType string

func ParseEventType

func ParseEventType(eventType string) (EventType, error)

type Frame

type Frame []any

func (Frame) GetData

func (x Frame) GetData() string

func (Frame) GetDataE

func (x Frame) GetDataE() (string, error)

func (Frame) GetDelay

func (x Frame) GetDelay() float64

func (Frame) GetDelayE

func (x Frame) GetDelayE() (float64, error)

type Theme

type Theme struct {

	// 主题的前景色
	FrontColor string `json:"fg" yaml:"fg" mapstructure:"fg"`

	// 主题的背景色
	BackgroundColor string `json:"bg" yaml:"bg" mapstructure:"bg"`

	// 8或者16个颜色的调色板
	Palette string `json:"palette" yaml:"palette" mapstructure:"palette"`
}

Theme 可以设置一个初始化的主题

type Version

type Version uint
const (
	VersionUnknown Version = 0
	Version1       Version = 1
	Version2       Version = 2
)

func DetectVersion

func DetectVersion(ctx context.Context, asciiCastString string) (Version, error)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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