Class: AsciinemaWin::Asciicast::Header
- Inherits:
-
Object
- Object
- AsciinemaWin::Asciicast::Header
- Defined in:
- lib/asciinema_win/asciicast.rb
Overview
Recording header with metadata
Instance Attribute Summary collapse
-
#command ⇒ String?
Command that was recorded.
-
#duration ⇒ Float?
Total duration in seconds.
-
#env ⇒ Hash<String, String>
Captured environment variables.
-
#height ⇒ Integer
Terminal height in rows.
-
#idle_time_limit ⇒ Float?
Maximum idle time between frames.
-
#theme ⇒ Hash?
Terminal color theme.
-
#timestamp ⇒ Integer?
Unix timestamp of recording start.
-
#title ⇒ String?
Recording title.
-
#version ⇒ Integer
Format version (always 2).
-
#width ⇒ Integer
Terminal width in columns.
Class Method Summary collapse
-
.from_json(json_str) ⇒ Header
Parse header from JSON string.
Instance Method Summary collapse
-
#initialize(width:, height:, timestamp: nil, duration: nil, idle_time_limit: nil, command: nil, title: nil, env: nil, theme: nil) ⇒ Header
constructor
Create a new header.
-
#to_h ⇒ Hash
Header as a hash.
-
#to_json(*_args) ⇒ String
Serialize header to JSON string.
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
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 = || Time.now.to_i @duration = duration @idle_time_limit = idle_time_limit @command = command @title = title @env = env || {} @theme = theme end |
Instance Attribute Details
#command ⇒ String?
Returns Command that was recorded.
65 66 67 |
# File 'lib/asciinema_win/asciicast.rb', line 65 def command @command end |
#duration ⇒ Float?
Returns Total duration in seconds.
59 60 61 |
# File 'lib/asciinema_win/asciicast.rb', line 59 def duration @duration end |
#env ⇒ Hash<String, String>
Returns Captured environment variables.
71 72 73 |
# File 'lib/asciinema_win/asciicast.rb', line 71 def env @env end |
#height ⇒ Integer
Returns Terminal height in rows.
53 54 55 |
# File 'lib/asciinema_win/asciicast.rb', line 53 def height @height end |
#idle_time_limit ⇒ Float?
Returns Maximum idle time between frames.
62 63 64 |
# File 'lib/asciinema_win/asciicast.rb', line 62 def idle_time_limit @idle_time_limit end |
#theme ⇒ Hash?
Returns Terminal color theme.
74 75 76 |
# File 'lib/asciinema_win/asciicast.rb', line 74 def theme @theme end |
#timestamp ⇒ Integer?
Returns Unix timestamp of recording start.
56 57 58 |
# File 'lib/asciinema_win/asciicast.rb', line 56 def @timestamp end |
#title ⇒ String?
Returns Recording title.
68 69 70 |
# File 'lib/asciinema_win/asciicast.rb', line 68 def title @title end |
#version ⇒ Integer
Returns Format version (always 2).
47 48 49 |
# File 'lib/asciinema_win/asciicast.rb', line 47 def version @version end |
#width ⇒ Integer
Returns 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
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.}" end |
Instance Method Details
#to_h ⇒ Hash
Returns 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
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 |