Sha256: c5e63891b343046caffa90a03da71b4bd2be5493fdae3d7b2aa357d25f0e26e6

Contents?: true

Size: 1.31 KB

Versions: 11

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require 'subprocess'

module Cased
  module CLI
    class Recorder
      KEY = 'CASED_CLI_RECORDING'
      TRUE = '1'

      attr_reader :command
      attr_reader :events
      attr_reader :started_at
      attr_reader :width
      attr_reader :height
      attr_reader :options
      attr_accessor :writer

      # @return [Boolean] if CLI session is being recorded.
      def self.recording?
        ENV[KEY] == TRUE
      end

      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

      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
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
cased-ruby-0.5.2 lib/cased/cli/recorder.rb
cased-ruby-0.5.1 lib/cased/cli/recorder.rb
cased-ruby-0.5.0 lib/cased/cli/recorder.rb
cased-ruby-0.4.8 lib/cased/cli/recorder.rb
cased-ruby-0.4.7 lib/cased/cli/recorder.rb
cased-ruby-0.4.6 lib/cased/cli/recorder.rb
cased-ruby-0.4.5 lib/cased/cli/recorder.rb
cased-ruby-0.4.4 lib/cased/cli/recorder.rb
cased-ruby-0.4.3 lib/cased/cli/recorder.rb
cased-ruby-0.4.2 lib/cased/cli/recorder.rb
cased-ruby-0.4.1 lib/cased/cli/recorder.rb