Class: Shellfie::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/shellfie/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(columns:, rows:, title: "Terminal Session", events: [], captures: {}, exit_status: nil) ⇒ Session

Returns a new instance of Session.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/shellfie/session.rb', line 11

def initialize(columns:, rows:, title: "Terminal Session", events: [], captures: {}, exit_status: nil)
  unless columns.is_a?(Integer) && columns.between?(1, 500) && rows.is_a?(Integer) && rows.between?(1, 200)
    raise ParseError, "Invalid session dimensions"
  end

  @title = title
  @events = events
  @captures = captures
  @exit_status = exit_status
  @screen = TerminalScreen.new(columns: columns, rows: rows)
  events.each { |event| @screen.feed(event[:text].to_s) if event.fetch(:visible, true) }
end

Instance Attribute Details

#capturesObject (readonly)

Returns the value of attribute captures.



9
10
11
# File 'lib/shellfie/session.rb', line 9

def captures
  @captures
end

#eventsObject (readonly)

Returns the value of attribute events.



9
10
11
# File 'lib/shellfie/session.rb', line 9

def events
  @events
end

#exit_statusObject (readonly)

Returns the value of attribute exit_status.



9
10
11
# File 'lib/shellfie/session.rb', line 9

def exit_status
  @exit_status
end

#screenObject (readonly)

Returns the value of attribute screen.



9
10
11
# File 'lib/shellfie/session.rb', line 9

def screen
  @screen
end

Instance Method Details

#capture(name) ⇒ Object



32
33
34
# File 'lib/shellfie/session.rb', line 32

def capture(name)
  captures[name] = screen.render_lines
end

#compose_hashObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/shellfie/session.rb', line 59

def compose_hash
  frames = []
  each_snapshot do |event, snapshot|
    frames << {
      "screen" => snapshot,
      "delay" => [(event[:delay].to_f * 1_000).round, 1].max
    }
  end
  {
    "version" => 1,
    "title" => @title,
    "window" => { "width" => screen.columns * 8, "visible_lines" => screen.rows },
    "frames" => frames
  }
end

#record(text, delay: 0, visible: true, status: nil) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/shellfie/session.rb', line 24

def record(text, delay: 0, visible: true, status: nil)
  screen.feed(text) if visible
  event = { text: visible ? text : "", delay: delay, visible: visible, status: status }
  events << event
  @exit_status = status unless status.nil?
  event
end

#render_config(theme:, options: {}, animated: false, lines: nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/shellfie/session.rb', line 36

def render_config(theme:, options: {}, animated: false, lines: nil)
  base = {
    theme: theme,
    title: @title,
    window: options.fetch(:window, {}),
    font: options.fetch(:font, {}),
    animation: options.fetch(:animation, {}),
    headless: options.fetch(:headless, false)
  }
  if animated
    raise ValidationError, "Captured screens cannot be rendered as animations" if lines

    frames = []
    each_snapshot do |event, snapshot|
      frame = Frame.new(screen: snapshot, delay: [(event[:delay].to_f * 1_000).round, 1].max)
      frames << frame
    end
    Config.new(**base, frames: frames)
  else
    Config.new(**base, lines: (lines || screen.render_lines).map { |line| Line.new(output: line) })
  end
end

#to_hObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/shellfie/session.rb', line 75

def to_h
  {
    version: 1,
    title: @title,
    columns: screen.columns,
    rows: screen.rows,
    events: events,
    captures: captures,
    exit_status: exit_status
  }
end