Class: Consular::Terminator

Inherits:
Core
  • Object
show all
Defined in:
lib/consular/terminator.rb,
lib/consular/terminator/version.rb

Overview

Consular core for interacting with Terminator.

Since we don’t have any real API to work with and just send keypresses via XTEST, we don’t have tab references either. Instead we just count tabs and return tab indexes.

Largely adapted from github.com/achiu/consular-osx

Defined Under Namespace

Modules: Version

Constant Summary collapse

VERSION =
[
  Version::MAJOR, Version::MINOR, Version::PATCH, Version::BUILD
].compact.join('.')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Terminator

Initialize

Parameters:

  • path (String)

    path to Termfile.



40
41
42
43
# File 'lib/consular/terminator.rb', line 40

def initialize(path)
  super
  @tabidx = nil
end

Class Method Details

.valid_system?Boolean

Check to see if current system is valid for this core

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/consular/terminator.rb', line 20

def valid_system?
  if (RbConfig::CONFIG['host_os'] =~ /linux/) != nil
    if !(xdotool = `which xdotool`.chomp).empty?
      begin
        return File::Stat.new(xdotool).executable?
      rescue
        return false
      end
    end
  end
  return false
end

Instance Method Details

#execute_command(cmd, options = {}) ⇒ Object

Execute the given command in the context of the active window.

Examples:

@osx.execute_command 'ps aux', :in => @tab_object

Parameters:

  • cmd (String)

    The command to execute.

  • options (Hash) (defaults to: {})

    Additional options to pass into appscript for the context.



156
157
158
# File 'lib/consular/terminator.rb', line 156

def execute_command(cmd, options = {})
  run_in_active_terminator cmd, options
end

#execute_window(content, options = {}) ⇒ Object

Executes the commands for each designated window. .run_windows will iterate through each of the tabs in sorted order to execute the tabs in the order they were set. The logic follows this:

If the content is for the 'default' window,
then use the current active window and generate the commands.

If the content is for a new window,
then generate a new window and activate the windows.

Otherwise, open a new tab and execute the commands.

Examples:

@core.execute_window contents, :default => true
@core.execute_window contents, :default => true

Parameters:

  • content (Hash)

    The hash contents of the window from the Termfile.

  • options (Hash) (defaults to: {})

    Addional options to pass. You can use:

    :default - Whether this is being run as the default window.
    


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/consular/terminator.rb', line 86

def execute_window(content, options = {})
  window_options = content[:options]
  _contents      = content[:tabs]
  _first_run     = true

  _contents.keys.sort.each do |key|
    _content = _contents[key]
    _options = content[:options]
    _name    = options[:name]

    _tab =
    if _first_run && !options[:default]
      open_window options.merge(window_options)
    else
      key == 'default' ? nil : open_tab(_options)
    end

    _first_run = false
    commands = prepend_befores _content[:commands], content[:before]
    commands = set_title _name, commands
    commands.each { |cmd| execute_command cmd, :in => _tab }
  end

end

#open_tab(options = nil) ⇒ Object

Opens a new tab and return the last instantiated tab(itself).

Parameters:

  • options (Hash) (defaults to: nil)

    Options to further customize the window. You can use:

    :settings -
    :selected -
    

Returns:

  • Returns a refernce to the last instantiated tab of the window.



172
173
174
175
176
177
# File 'lib/consular/terminator.rb', line 172

def open_tab(options = nil)
  send_keypress(active_terminator_window, "ctrl+shift+t")
  # FIXME: horribly hacky but can't come up with any way
  # to truly make sure tab has opened.
  sleep 1
end

#open_window(options = nil) ⇒ Object

Opens a new window and returns its last instantiated tab.(The first ‘tab’ in the window).

Parameters:

  • options (Hash) (defaults to: nil)

    Options to further customize the window. You can use:

    :bound        - Set the bounds of the windows
    :visible      - Set whether or not the current window is visible
    :miniaturized - Set whether or not the window is minimized
    

Returns:

  • Returns a refernce to the last instantiated tab of the window.



193
194
195
196
197
198
199
200
201
# File 'lib/consular/terminator.rb', line 193

def open_window(options = nil)
  windowid_before = active_terminator_window
  send_keypress(active_terminator_window, "ctrl+shift+i")
  # wait for the active window to change -> new window opened
  while active_terminator_window == windowid_before
    sleep 1
  end
  return (@tabidx = 0)
end

#prepend_befores(commands, befores = nil) ⇒ Array<String>

Prepends the :before commands to the current context’s commands if it exists.

Parameters:

  • commands (Array<String>)

    The current tab commands

  • befores (Array<String>) (defaults to: nil)

    The current window’s :befores

Returns:

  • (Array<String>)

    The current context commands with the :before commands prepended



136
137
138
139
140
141
142
# File 'lib/consular/terminator.rb', line 136

def prepend_befores(commands, befores = nil)
  unless befores.nil? || befores.empty?
    commands.insert(0, befores).flatten!
  else
    commands
  end
end

#process!Object

Method called by runner to execute Termfile.



55
56
57
58
59
60
# File 'lib/consular/terminator.rb', line 55

def process!
  windows = @termfile[:windows]
  default = windows.delete('default')
  execute_window(default, :default => true) unless default[:tabs].empty?
  windows.each_pair { |_, cont| execute_window(cont) }
end

#set_title(title, commands) ⇒ Object

Prepend a title setting command prior to the other commands.

Parameters:

  • title (String)

    The title to set for the context of the commands.

  • commands (Array<String>)

    The context of commands to preprend to.



119
120
121
122
# File 'lib/consular/terminator.rb', line 119

def set_title(title, commands)
  cmd = "PS1=\"$PS1\\[\\e]2;#{title}\\a\\]\""
  title ? commands.insert(0, cmd) : commands
end

#setup!Object

Method called by runner to Execute Termfile setup.



48
49
50
# File 'lib/consular/terminator.rb', line 48

def setup!
  @termfile[:setup].each { |cmd| execute_command(cmd, :in => active_window) }
end