Class: Yildun::Terminal
- Inherits:
-
Object
- Object
- Yildun::Terminal
- Defined in:
- lib/yildun/terminal.rb,
sig/yildun.rbs
Instance Attribute Summary collapse
-
#session ⇒ Object
readonly
Returns the value of attribute session.
Instance Method Summary collapse
- #close ⇒ Object
- #command_output(command) ⇒ String (also: #copy_command_output)
- #commands ⇒ Array[untyped]
- #cwd ⇒ String
- #failed_commands ⇒ Array[untyped]
- #focus(active:) ⇒ String
-
#initialize(command:, env: {}, cwd: Dir.pwd, columns: 80, rows: 24, scrollback_limit: 10_000, session: nil) ⇒ Terminal
constructor
A new instance of Terminal.
- #input(bytes) ⇒ Object
- #key(name, **modifiers) ⇒ Object
- #last_command ⇒ Object
- #mouse(**event) ⇒ Object
- #next_command ⇒ Object
- #paste(text) ⇒ Object
- #previous_command ⇒ Object
- #pump(timeout: 0) ⇒ Boolean
- #resize(columns:, rows:) ⇒ Object
- #screen ⇒ Object
- #search(query, regex: false) ⇒ Array[Hash[Symbol, untyped]]
- #search_commands(query, regex: false) ⇒ Array[untyped]
- #selection(start, finish, history: false) ⇒ String
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.
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.
9 10 11 |
# File 'lib/yildun/terminal.rb', line 9 def session @session end |
Instance Method Details
#close ⇒ Object
75 |
# File 'lib/yildun/terminal.rb', line 75 def close = @session.close |
#command_output(command) ⇒ String Also known as: copy_command_output
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]
18 |
# File 'lib/yildun/terminal.rb', line 18 def commands = @session.commands |
#cwd ⇒ String
17 |
# File 'lib/yildun/terminal.rb', line 17 def cwd = @session.cwd |
#failed_commands ⇒ Array[untyped]
19 |
# File 'lib/yildun/terminal.rb', line 19 def failed_commands = commands.select { |command| command.exit_status != 0 } |
#focus(active:) ⇒ String
26 |
# File 'lib/yildun/terminal.rb', line 26 def focus(active:) = @session.vt.focus(active: active) |
#input(bytes) ⇒ Object
20 |
# File 'lib/yildun/terminal.rb', line 20 def input(bytes) = @session.input(bytes) |
#key(name, **modifiers) ⇒ Object
22 |
# File 'lib/yildun/terminal.rb', line 22 def key(name, **modifiers) = @session.key(name, **modifiers) |
#last_command ⇒ Object
52 |
# File 'lib/yildun/terminal.rb', line 52 def last_command = commands.last |
#mouse(**event) ⇒ Object
23 |
# File 'lib/yildun/terminal.rb', line 23 def mouse(**event) = @session.mouse(**event) |
#next_command ⇒ 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
21 |
# File 'lib/yildun/terminal.rb', line 21 def paste(text) = @session.paste(text) |
#previous_command ⇒ 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
24 |
# File 'lib/yildun/terminal.rb', line 24 def pump(timeout: 0) = @session.pump(timeout: timeout) |
#resize(columns:, rows:) ⇒ Object
25 |
# File 'lib/yildun/terminal.rb', line 25 def resize(columns:, rows:) = @session.resize(columns: columns, rows: rows) |
#screen ⇒ Object
16 |
# File 'lib/yildun/terminal.rb', line 16 def screen = @session.screen |
#search(query, regex: false) ⇒ 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.}" end |
#search_commands(query, regex: false) ⇒ 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.}" end |
#selection(start, finish, history: false) ⇒ String
63 |
# File 'lib/yildun/terminal.rb', line 63 def selection(start, finish, history: false) = screen.selection(start, finish, history: history) |