|
| 1 | +package asciinema_parser |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +// 格式规范文档: https://github.com/asciinema/asciinema/blob/develop/doc/asciicast-v2.md |
| 10 | +type AsciiCastV2 struct { |
| 11 | + |
| 12 | + // 表示文件的格式版本 |
| 13 | + Version Version `json:"version" yaml:"version" mapstructure:"version"` |
| 14 | + |
| 15 | + // 屏幕的宽度,但是是列 |
| 16 | + Width uint `json:"width" yaml:"width" mapstructure:"width"` |
| 17 | + |
| 18 | + // 屏幕的高度,但是是行 |
| 19 | + Height uint `json:"height" yaml:"height" mapstructure:"height"` |
| 20 | + |
| 21 | + // 录制开始时间 |
| 22 | + Timestamp uint64 `json:"timestamp" yaml:"timestamp" mapstructure:"timestamp"` |
| 23 | + time time.Time |
| 24 | + |
| 25 | + // 录制持续时间 |
| 26 | + Duration float64 `json:"duration" yaml:"duration" mapstructure:"duration"` |
| 27 | + |
| 28 | + Command string `json:"command" yaml:"command" mapstructure:"command"` |
| 29 | + |
| 30 | + Title string `json:"title" yaml:"title" mapstructure:"title"` |
| 31 | + |
| 32 | + Theme *Theme `json:"theme" yaml:"theme" mapstructure:"theme"` |
| 33 | + |
| 34 | + // |
| 35 | + IdleTimeLimit float64 `json:"idle_time_limit" yaml:"idle_time_limit" mapstructure:"idle_time_limit"` |
| 36 | + |
| 37 | + // 相关环境变量 |
| 38 | + EnvMap map[string]string `json:"env" yaml:"env" mapstructure:"env"` |
| 39 | + |
| 40 | + EventStream []*Event `json:"event_stream" yaml:"event_stream" mapstructure:"event_stream"` |
| 41 | +} |
| 42 | + |
| 43 | +func (x *AsciiCastV2) GetTime() time.Time { |
| 44 | + if x.time.IsZero() { |
| 45 | + x.time = time.Unix(int64(x.Timestamp), 0) |
| 46 | + } |
| 47 | + return x.time |
| 48 | +} |
| 49 | + |
| 50 | +// ------------------------------------------------- -------------------------------------------------------------------- |
| 51 | + |
| 52 | +// Theme 可以设置一个初始化的主题 |
| 53 | +type Theme struct { |
| 54 | + |
| 55 | + // 主题的前景色 |
| 56 | + FrontColor string `json:"fg" yaml:"fg" mapstructure:"fg"` |
| 57 | + |
| 58 | + // 主题的背景色 |
| 59 | + BackgroundColor string `json:"bg" yaml:"bg" mapstructure:"bg"` |
| 60 | + |
| 61 | + // 8或者16个颜色的调色板 |
| 62 | + Palette string `json:"palette" yaml:"palette" mapstructure:"palette"` |
| 63 | +} |
| 64 | + |
| 65 | +// ------------------------------------------------- -------------------------------------------------------------------- |
| 66 | + |
| 67 | +type EventType string |
| 68 | + |
| 69 | +const ( |
| 70 | + EventTypeUnknown = "" |
| 71 | + EventTypeOutput = "o" |
| 72 | + EventTypeInput = "i" |
| 73 | +) |
| 74 | + |
| 75 | +func ParseEventType(eventType string) (EventType, error) { |
| 76 | + switch strings.ToLower(eventType) { |
| 77 | + case EventTypeOutput: |
| 78 | + return EventTypeOutput, nil |
| 79 | + case EventTypeInput: |
| 80 | + return EventTypeInput, nil |
| 81 | + default: |
| 82 | + return EventTypeUnknown, fmt.Errorf("value %s is not a valid event type", eventType) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// ------------------------------------------------- -------------------------------------------------------------------- |
| 87 | + |
| 88 | +// Event 事件流中的某一个事件 |
| 89 | +type Event struct { |
| 90 | + cast *AsciiCastV2 |
| 91 | + |
| 92 | + // 时间发生的时间距开始时间的偏移 |
| 93 | + Delay float64 `json:"delay" yaml:"delay" mapstructure:"delay"` |
| 94 | + |
| 95 | + // 事件的类型,v2的话是有output和input |
| 96 | + EventType EventType `json:"event_type" yaml:"event_type" mapstructure:"event_type"` |
| 97 | + |
| 98 | + // 事件的数据,通常是显示的内容 |
| 99 | + EventData string `json:"event_data" yaml:"event_data" mapstructure:"event_data"` |
| 100 | +} |
| 101 | + |
| 102 | +func (x *Event) GetEventTime() time.Time { |
| 103 | + return x.cast.GetTime().Add(time.Millisecond * time.Duration(x.Delay*1000)) |
| 104 | +} |
| 105 | + |
| 106 | +func (x *Event) GetAsciiCast() *AsciiCastV2 { |
| 107 | + return x.cast |
| 108 | +} |
| 109 | + |
| 110 | +// ------------------------------------------------- -------------------------------------------------------------------- |
0 commit comments