Class: Yildun::Terminal

Inherits:
Object
  • Object
show all
Defined in:
lib/yildun/terminal.rb,
sig/yildun.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command:, env: {}, cwd: Dir.pwd, columns: 80, rows: 24, scrollback_limit: 10_000, session: nil) ⇒ Terminal

Returns a new instance of Terminal.

Parameters:

  • command: (String, Array[String])
  • env: (Hash[String, String]) (defaults to: {})
  • cwd: (String) (defaults to: Dir.pwd)
  • columns: (Integer) (defaults to: 80)
  • rows: (Integer) (defaults to: 24)
  • scrollback_limit: (Integer) (defaults to: 10_000)
  • session: (Object) (defaults to: nil)


11
12
13
14
# File 'lib/yildun/terminal.rb', line 11

def initialize(command:, env: {}, cwd: Dir.pwd, columns: 80, rows: 24, scrollback_limit: 10_000, session: nil)
  @session = session || Tarazed::Session.new(command: command, env: env, cwd: cwd, columns: columns, rows: rows,
    scrollback_limit: scrollback_limit)
end

Instance Attribute Details

#session ⇒ Object (readonly)

Returns the value of attribute session.

Returns:

  • (Object)


9
10
11
# File 'lib/yildun/terminal.rb', line 9

def session
  @session
end

Instance Method Details

#close ⇒ Object

Returns:

  • (Object)


75
# File 'lib/yildun/terminal.rb', line 75

def close = @session.close

#command_output(command) ⇒ String Also known as: copy_command_output

Parameters:

  • command (Object)

Returns:

  • (String)


54
55
56
57
58
59
# File 'lib/yildun/terminal.rb', line 54

def command_output(command)
  range = command&.output_range
  return "" unless range

  screen.selection([0, range.begin], [screen.columns, range.end], history: true)
end

#commands ⇒ Array[untyped]

Returns:

  • (Array[untyped])


18
# File 'lib/yildun/terminal.rb', line 18

def commands = @session.commands

#cwd ⇒ String

Returns:

  • (String)


17
# File 'lib/yildun/terminal.rb', line 17

def cwd = @session.cwd

#failed_commands ⇒ Array[untyped]

Returns:

  • (Array[untyped])


19
# File 'lib/yildun/terminal.rb', line 19

def failed_commands = commands.select { |command| command.exit_status != 0 }

#focus(active:) ⇒ String

Parameters:

  • active: (Boolean)

Returns:

  • (String)


26
# File 'lib/yildun/terminal.rb', line 26

def focus(active:) = @session.vt.focus(active: active)

#input(bytes) ⇒ Object

Parameters:

  • bytes (String)

Returns:

  • (Object)


20
# File 'lib/yildun/terminal.rb', line 20

def input(bytes) = @session.input(bytes)

#key(name, **modifiers) ⇒ Object

Parameters:

  • name (String)
  • modifiers (Boolean)

Returns:

  • (Object)


22
# File 'lib/yildun/terminal.rb', line 22

def key(name, **modifiers) = @session.key(name, **modifiers)

#last_command ⇒ Object

Returns:

  • (Object)


52
# File 'lib/yildun/terminal.rb', line 52

def last_command = commands.last

#mouse(**event) ⇒ Object

Parameters:

  • event (Object)

Returns:

  • (Object)


23
# File 'lib/yildun/terminal.rb', line 23

def mouse(**event) = @session.mouse(**event)

#next_command ⇒ Object

Returns:

  • (Object)


47
48
49
50
# File 'lib/yildun/terminal.rb', line 47

def next_command
  @command_index = [(@command_index || -1) + 1, commands.length - 1].min
  commands[@command_index]
end

#paste(text) ⇒ Object

Parameters:

  • text (String)

Returns:

  • (Object)


21
# File 'lib/yildun/terminal.rb', line 21

def paste(text) = @session.paste(text)

#previous_command ⇒ Object

Returns:

  • (Object)


42
43
44
45
# File 'lib/yildun/terminal.rb', line 42

def previous_command
  @command_index = [(@command_index || commands.length) - 1, 0].max
  commands[@command_index]
end

#pump(timeout: 0) ⇒ Boolean

Parameters:

  • timeout: (Numeric) (defaults to: 0)

Returns:

  • (Boolean)


24
# File 'lib/yildun/terminal.rb', line 24

def pump(timeout: 0) = @session.pump(timeout: timeout)

#resize(columns:, rows:) ⇒ Object

Parameters:

  • columns: (Integer)
  • rows: (Integer)

Returns:

  • (Object)


25
# File 'lib/yildun/terminal.rb', line 25

def resize(columns:, rows:) = @session.resize(columns: columns, rows: rows)

#screen ⇒ Object

Returns:

  • (Object)


16
# File 'lib/yildun/terminal.rb', line 16

def screen = @session.screen

#search(query, regex: false) ⇒ Array[Hash[Symbol, untyped]]

Parameters:

  • query (String)
  • regex: (Boolean) (defaults to: false)

Returns:

  • (Array[Hash[Symbol, untyped]])


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/yildun/terminal.rb', line 27

def search(query, regex: false)
  return screen.search(query, regex: regex) if screen.respond_to?(:search)

  matcher = regex ? Regexp.new(query.to_s) : query.to_s
  screen.lines.each_with_index.filter_map do |text, index|
    match = regex ? matcher.match(text) : text.index(matcher)
    next unless match

    range = match.is_a?(MatchData) ? match.begin(0)...match.end(0) : match...(match + matcher.length)
    {index: index, text: text, range: range}.freeze
  end.freeze
rescue RegexpError => error
  raise ArgumentError, "invalid search pattern: #{error.message}"
end

#search_commands(query, regex: false) ⇒ Array[untyped]

Parameters:

  • query (String)
  • regex: (Boolean) (defaults to: false)

Returns:

  • (Array[untyped])


65
66
67
68
69
70
71
72
73
# File 'lib/yildun/terminal.rb', line 65

def search_commands(query, regex: false)
  matcher = regex ? Regexp.new(query.to_s) : query.to_s.downcase
  commands.select do |command|
    input = command.input.to_s
    regex ? matcher.match?(input) : input.downcase.include?(matcher)
  end
rescue RegexpError => error
  raise ArgumentError, "invalid search pattern: #{error.message}"
end

#selection(start, finish, history: false) ⇒ String

Parameters:

  • start (Array[Integer])
  • finish (Array[Integer])
  • history: (Boolean) (defaults to: false)

Returns:

  • (String)


63
# File 'lib/yildun/terminal.rb', line 63

def selection(start, finish, history: false) = screen.selection(start, finish, history: history)