Class: Terminitor::AbstractCore
- Inherits:
-
Object
- Object
- Terminitor::AbstractCore
- Defined in:
- lib/terminitor/abstract_core.rb
Overview
This AbstractCore defines the basic methods that the Core should inherit
Direct Known Subclasses
Instance Attribute Summary collapse
-
#termfile ⇒ Object
Returns the value of attribute termfile.
-
#terminal ⇒ Object
Returns the value of attribute terminal.
-
#windows ⇒ Object
Returns the value of attribute windows.
-
#working_dir ⇒ Object
Returns the value of attribute working_dir.
Instance Method Summary collapse
-
#active_window ⇒ Object
Returns the current window.
-
#execute_command(cmd, options = {}) ⇒ Object
Executes the Command should use the :in key to interact with terminal object.
-
#initialize(path) ⇒ AbstractCore
constructor
set the terminal object, windows, and load the Termfile.
-
#load_termfile(path) ⇒ Hash
Loads commands via the termfile and returns them as a hash if it matches legacy yaml, parse as yaml, else use new dsl.
-
#open_tab(options = nil) ⇒ Object
Opens a new tab and returns itself.
-
#open_window(options = nil) ⇒ Object
Opens a new window and returns the tab object.
-
#process! ⇒ Object
Executes the Termfile.
-
#run_in_window(window_name, window_content, options = {}) ⇒ Object
this command will run commands in the designated window run_in_window ‘window1’, => [‘ls’,‘ok’].
-
#set_delayed_options ⇒ Object
For options which should be set after all tabs have been opened.
-
#setup! ⇒ Object
Run the setup block in Termfile.
Constructor Details
#initialize(path) ⇒ AbstractCore
set the terminal object, windows, and load the Termfile.
8 9 10 |
# File 'lib/terminitor/abstract_core.rb', line 8 def initialize(path) @termfile = load_termfile(path) end |
Instance Attribute Details
#termfile ⇒ Object
Returns the value of attribute termfile.
4 5 6 |
# File 'lib/terminitor/abstract_core.rb', line 4 def termfile @termfile end |
#terminal ⇒ Object
Returns the value of attribute terminal.
4 5 6 |
# File 'lib/terminitor/abstract_core.rb', line 4 def terminal @terminal end |
#windows ⇒ Object
Returns the value of attribute windows.
4 5 6 |
# File 'lib/terminitor/abstract_core.rb', line 4 def windows @windows end |
#working_dir ⇒ Object
Returns the value of attribute working_dir.
4 5 6 |
# File 'lib/terminitor/abstract_core.rb', line 4 def working_dir @working_dir end |
Instance Method Details
#active_window ⇒ Object
Returns the current window
91 92 |
# File 'lib/terminitor/abstract_core.rb', line 91 def active_window end |
#execute_command(cmd, options = {}) ⇒ Object
Executes the Command should use the :in key to interact with terminal object. execute_command ‘cd /path/to’, => #<TerminalObject>
81 82 |
# File 'lib/terminitor/abstract_core.rb', line 81 def execute_command(cmd, = {}) end |
#load_termfile(path) ⇒ Hash
Loads commands via the termfile and returns them as a hash if it matches legacy yaml, parse as yaml, else use new dsl
68 69 70 |
# File 'lib/terminitor/abstract_core.rb', line 68 def load_termfile(path) File.extname(path) == '.yml' ? Terminitor::Yaml.new(path).to_hash : Terminitor::Dsl.new(path).to_hash end |
#open_tab(options = nil) ⇒ Object
Opens a new tab and returns itself.
86 87 88 |
# File 'lib/terminitor/abstract_core.rb', line 86 def open_tab( = nil) @working_dir = Dir.pwd # pass in current directory. end |
#open_window(options = nil) ⇒ Object
Opens a new window and returns the tab object.
96 97 98 |
# File 'lib/terminitor/abstract_core.rb', line 96 def open_window( = nil) @working_dir = Dir.pwd # pass in current directory. end |
#process! ⇒ Object
Executes the Termfile
20 21 22 23 24 25 26 |
# File 'lib/terminitor/abstract_core.rb', line 20 def process! @working_dir = Dir.pwd term_windows = @termfile[:windows] run_in_window('default', term_windows['default'], :default => true) unless term_windows['default'][:tabs].empty? term_windows.delete('default') term_windows.each_pair { |window_name, window_content| run_in_window(window_name, window_content) } end |
#run_in_window(window_name, window_content, options = {}) ⇒ Object
this command will run commands in the designated window run_in_window ‘window1’, => [‘ls’,‘ok’]
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/terminitor/abstract_core.rb', line 33 def run_in_window(window_name, window_content, = {}) = window_content[:options] first_tab = true window_content[:tabs].keys.sort.each do |tab_key| tab_content = window_content[:tabs][tab_key] # Open window on first 'tab' statement # first tab is already opened in the new window, so first tab should be # opened as a new tab in default window only = tab_content[:options] tab_name = [:name] if if first_tab && ![:default] first_tab = false = (.to_a + .to_a).inject([]) {|arr, pair| arr += pair } = Hash[*] # safe merge tab = .empty? ? open_window(nil) : open_window() else tab = ( tab_key == 'default' ? active_window : open_tab() ) # give us the current window if its default, else open a tab. end # append our before block commands. tab_content[:commands].insert(0, window_content[:before]).flatten! if window_content[:before] # clean up prompt tab_content[:commands].insert(0, 'clear') if tab_name || !@working_dir.to_s.empty? # add title to tab # we use \[ and \] here so that the title characters do not get included in the command history tab_content[:commands].insert(0, "PS1=\"$PS1\\[\\e]2;#{tab_name}\\a\\]\"") if tab_name tab_content[:commands].insert(0, "cd \"#{@working_dir}\"") unless @working_dir.to_s.empty? tab_content[:commands].each { |cmd| execute_command(cmd, :in => tab) } end end |
#set_delayed_options ⇒ Object
For options which should be set after all tabs have been opened
101 102 103 |
# File 'lib/terminitor/abstract_core.rb', line 101 def @working_dir = Dir.pwd # not nil end |
#setup! ⇒ Object
Run the setup block in Termfile
13 14 15 16 17 |
# File 'lib/terminitor/abstract_core.rb', line 13 def setup! @working_dir = Dir.pwd commands = @termfile[:setup].insert(0, "cd #{@working_dir}") commands.each { |cmd| execute_command(cmd, :in => active_window) } end |