Class: AsciinemaWin::Asciicast::Header

Inherits:
Object
  • Object
show all
Defined in:
lib/asciinema_win/asciicast.rb

Overview

Recording header with metadata

Examples:

Create a header

header = Header.new(
  width: 120,
  height: 30,
  title: "Demo Recording"
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width:, height:, timestamp: nil, duration: nil, idle_time_limit: nil, command: nil, title: nil, env: nil, theme: nil) ⇒ Header

Create a new header

Parameters:

  • width (Integer)

    Terminal width

  • height (Integer)

    Terminal height

  • timestamp (Integer, nil) (defaults to: nil)

    Unix timestamp (defaults to now)

  • duration (Float, nil) (defaults to: nil)

    Recording duration

  • idle_time_limit (Float, nil) (defaults to: nil)

    Max idle time

  • command (String, nil) (defaults to: nil)

    Recorded command

  • title (String, nil) (defaults to: nil)

    Recording title

  • env (Hash, nil) (defaults to: nil)

    Environment variables

  • theme (Hash, nil) (defaults to: nil)

    Color theme



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/asciinema_win/asciicast.rb', line 87

def initialize(
  width:,
  height:,
  timestamp: nil,
  duration: nil,
  idle_time_limit: nil,
  command: nil,
  title: nil,
  env: nil,
  theme: nil
)
  @version = VERSION
  @width = width
  @height = height
  @timestamp = timestamp || Time.now.to_i
  @duration = duration
  @idle_time_limit = idle_time_limit
  @command = command
  @title = title
  @env = env || {}
  @theme = theme
end

Instance Attribute Details

#commandString?

Returns Command that was recorded.

Returns:

  • (String, nil)

    Command that was recorded



65
66
67
# File 'lib/asciinema_win/asciicast.rb', line 65

def command
  @command
end

#durationFloat?

Returns Total duration in seconds.

Returns:

  • (Float, nil)

    Total duration in seconds



59
60
61
# File 'lib/asciinema_win/asciicast.rb', line 59

def duration
  @duration
end

#envHash<String, String>

Returns Captured environment variables.

Returns:

  • (Hash<String, String>)

    Captured environment variables



71
72
73
# File 'lib/asciinema_win/asciicast.rb', line 71

def env
  @env
end

#heightInteger

Returns Terminal height in rows.

Returns:

  • (Integer)

    Terminal height in rows



53
54
55
# File 'lib/asciinema_win/asciicast.rb', line 53

def height
  @height
end

#idle_time_limitFloat?

Returns Maximum idle time between frames.

Returns:

  • (Float, nil)

    Maximum idle time between frames



62
63
64
# File 'lib/asciinema_win/asciicast.rb', line 62

def idle_time_limit
  @idle_time_limit
end

#themeHash?

Returns Terminal color theme.

Returns:

  • (Hash, nil)

    Terminal color theme



74
75
76
# File 'lib/asciinema_win/asciicast.rb', line 74

def theme
  @theme
end

#timestampInteger?

Returns Unix timestamp of recording start.

Returns:

  • (Integer, nil)

    Unix timestamp of recording start



56
57
58
# File 'lib/asciinema_win/asciicast.rb', line 56

def timestamp
  @timestamp
end

#titleString?

Returns Recording title.

Returns:

  • (String, nil)

    Recording title



68
69
70
# File 'lib/asciinema_win/asciicast.rb', line 68

def title
  @title
end

#versionInteger

Returns Format version (always 2).

Returns:

  • (Integer)

    Format version (always 2)



47
48
49
# File 'lib/asciinema_win/asciicast.rb', line 47

def version
  @version
end

#widthInteger

Returns Terminal width in columns.

Returns:

  • (Integer)

    Terminal width in columns



50
51
52
# File 'lib/asciinema_win/asciicast.rb', line 50

def width
  @width
end

Class Method Details

.from_json(json_str) ⇒ Header

Parse header from JSON string

Parameters:

  • json_str (String)

    JSON string

Returns:

Raises:

  • (FormatError)

    If JSON is invalid or required fields are missing



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/asciinema_win/asciicast.rb', line 137

def self.from_json(json_str)
  data = JSON.parse(json_str)

  unless data.is_a?(Hash)
    raise FormatError, "Header must be a JSON object"
  end

  version = data["version"]
  unless version == VERSION
    raise FormatError, "Unsupported asciicast version: #{version}. Expected: #{VERSION}"
  end

  width = data["width"]
  height = data["height"]

  unless width.is_a?(Integer) && width > 0
    raise FormatError, "Invalid width: #{width}"
  end

  unless height.is_a?(Integer) && height > 0
    raise FormatError, "Invalid height: #{height}"
  end

  new(
    width: width,
    height: height,
    timestamp: data["timestamp"],
    duration: data["duration"]&.to_f,
    idle_time_limit: data["idle_time_limit"]&.to_f,
    command: data["command"],
    title: data["title"],
    env: data["env"] || {},
    theme: data["theme"]
  )
rescue JSON::ParserError => e
  raise FormatError, "Invalid header JSON: #{e.message}"
end

Instance Method Details

#to_hHash

Returns Header as a hash.

Returns:

  • (Hash)

    Header as a hash



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/asciinema_win/asciicast.rb', line 176

def to_h
  {
    version: @version,
    width: @width,
    height: @height,
    timestamp: @timestamp,
    duration: @duration,
    idle_time_limit: @idle_time_limit,
    command: @command,
    title: @title,
    env: @env,
    theme: @theme
  }
end

#to_json(*_args) ⇒ String

Serialize header to JSON string

Returns:

  • (String)

    JSON representation



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/asciinema_win/asciicast.rb', line 113

def to_json(*_args)
  data = {
    "version" => @version,
    "width" => @width,
    "height" => @height
  }

  # Add optional fields only if present
  data["timestamp"] = @timestamp if @timestamp
  data["duration"] = @duration if @duration
  data["idle_time_limit"] = @idle_time_limit if @idle_time_limit
  data["command"] = @command if @command
  data["title"] = @title if @title
  data["env"] = @env unless @env.empty?
  data["theme"] = @theme if @theme

  JSON.generate(data)
end