Class: Cased::CLI::Recorder

Inherits:
Object
  • Object
show all
Defined in:
lib/cased/cli/recorder.rb

Constant Summary collapse

KEY =
'CASED_CLI_RECORDING'
TRUE =
'1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, env: {}) ⇒ Recorder

Returns a new instance of Recorder.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cased/cli/recorder.rb', line 19

def initialize(command, env: {})
  @command = command
  @events = []
  @width = Subprocess.check_output(%w[tput cols]).strip.to_i
  @height = Subprocess.check_output(%w[tput lines]).strip.to_i

  subprocess_env = ENV.to_h.dup
  subprocess_env[KEY] = TRUE
  subprocess_env.merge!(env)
  @writer = Cased::CLI::Asciinema::Writer.new(
    command: command,
    width: width,
    height: height,
  )

  @options = {
    stdout: Subprocess::PIPE,
    env: subprocess_env,
  }
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



11
12
13
# File 'lib/cased/cli/recorder.rb', line 11

def command
  @command
end

#eventsObject (readonly)

Returns the value of attribute events.



11
12
13
# File 'lib/cased/cli/recorder.rb', line 11

def events
  @events
end

#heightObject (readonly)

Returns the value of attribute height.



11
12
13
# File 'lib/cased/cli/recorder.rb', line 11

def height
  @height
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/cased/cli/recorder.rb', line 11

def options
  @options
end

#started_atObject (readonly)

Returns the value of attribute started_at.



11
12
13
# File 'lib/cased/cli/recorder.rb', line 11

def started_at
  @started_at
end

#widthObject (readonly)

Returns the value of attribute width.



11
12
13
# File 'lib/cased/cli/recorder.rb', line 11

def width
  @width
end

#writerObject

Returns the value of attribute writer.



12
13
14
# File 'lib/cased/cli/recorder.rb', line 12

def writer
  @writer
end

Class Method Details

.recording?Boolean

Returns if CLI session is being recorded.

Returns:

  • (Boolean)

    if CLI session is being recorded.



15
16
17
# File 'lib/cased/cli/recorder.rb', line 15

def self.recording?
  ENV[KEY] == TRUE
end

Instance Method Details

#startObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cased/cli/recorder.rb', line 40

def start
  writer.time do
    Subprocess.check_call(command, options) do |t|
      t.communicate do |stdout, _stderr|
        $stdout.write(stdout)

        writer << stdout.gsub("\n", "\r\n")
      end
    end
  end
end