Class: Terminitor::AbstractCore

Inherits:
Object
  • Object
show all
Defined in:
lib/terminitor/abstract_core.rb

Overview

This AbstractCore defines the basic methods that the Core should inherit

Direct Known Subclasses

CmdCore, ItermCore, KonsoleCore, MacCore, TerminatorCore

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ AbstractCore

set the terminal object, windows, and load the Termfile.

Parameters:

  • path (String)

    to termfile



8
9
10
# File 'lib/terminitor/abstract_core.rb', line 8

def initialize(path)
  @termfile = load_termfile(path)
end

Instance Attribute Details

#termfileObject

Returns the value of attribute termfile.



4
5
6
# File 'lib/terminitor/abstract_core.rb', line 4

def termfile
  @termfile
end

#terminalObject

Returns the value of attribute terminal.



4
5
6
# File 'lib/terminitor/abstract_core.rb', line 4

def terminal
  @terminal
end

#windowsObject

Returns the value of attribute windows.



4
5
6
# File 'lib/terminitor/abstract_core.rb', line 4

def windows
  @windows
end

#working_dirObject

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_windowObject

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>

Parameters:

  • command (String)

    to be ran.

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

    hash



81
82
# File 'lib/terminitor/abstract_core.rb', line 81

def execute_command(cmd, options = {})
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

Parameters:

  • path (String)

    to termfile

Returns:

  • (Hash)

    returns a hash containing termfile details



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.

Parameters:

  • options (Hash) (defaults to: nil)

    hash.



86
87
88
# File 'lib/terminitor/abstract_core.rb', line 86

def open_tab(options = nil)
  @working_dir = Dir.pwd # pass in current directory.
end

#open_window(options = nil) ⇒ Object

Opens a new window and returns the tab object.

Parameters:

  • options (Hahs) (defaults to: nil)

    hash.



96
97
98
# File 'lib/terminitor/abstract_core.rb', line 96

def open_window(options = 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’]

Parameters:

  • name (String)

    of window

  • Hash (Hash)

    of window’s content extracted from Termfile

  • Hash (Hash)

    of options



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, options = {})
  window_options = 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_options = tab_content[:options]
    tab_name    = tab_options[:name] if tab_options
    if first_tab && !options[:default]
      first_tab = false
      combined_options = (window_options.to_a + tab_options.to_a).inject([]) {|arr, pair| arr += pair }
      window_options = Hash[*combined_options] # safe merge
      tab = window_options.empty? ? open_window(nil) : open_window(window_options)
    else
      tab = ( tab_key == 'default' ? active_window : open_tab(tab_options) ) # 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
  set_delayed_options
end

#set_delayed_optionsObject

For options which should be set after all tabs have been opened



101
102
103
# File 'lib/terminitor/abstract_core.rb', line 101

def set_delayed_options
  @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