Class: Terminitor::Dsl
- Inherits:
-
Object
- Object
- Terminitor::Dsl
- Defined in:
- lib/terminitor/dsl.rb
Overview
This class parses the Termfile to fit the new Ruby Dsl Syntax
Instance Method Summary collapse
-
#before(*commands, &block) ⇒ Object
runs commands before each tab in window context.
-
#initialize(path) ⇒ Dsl
constructor
A new instance of Dsl.
-
#pane(*args, &block) ⇒ Object
Generates a pane in the terminal.
-
#run(*commands) ⇒ Object
stores command in context.
-
#setup(*commands, &block) ⇒ Object
Contains all commands that will be run prior to the usual ‘workflow’ e.g bundle install, setup forks, etc …
-
#tab(*args, &block) ⇒ Object
sets command context to be run inside specific tab.
-
#to_hash ⇒ Hash
Returns yaml file as Terminitor formmatted hash.
-
#window(options = {}, &block) ⇒ Object
sets command context to be run inside a specific window.
Constructor Details
#initialize(path) ⇒ Dsl
Returns a new instance of Dsl.
6 7 8 9 10 11 12 |
# File 'lib/terminitor/dsl.rb', line 6 def initialize(path) file = File.read(path) @setup = [] @windows = { 'default' => {:tabs => {'default' =>{:commands=>[]}}}} @_context = @windows['default'] instance_eval(file) end |
Instance Method Details
#before(*commands, &block) ⇒ Object
runs commands before each tab in window context
67 68 69 70 71 72 73 74 |
# File 'lib/terminitor/dsl.rb', line 67 def before(*commands, &block) @_context[:before] ||= [] if block_given? in_context @_context[:before], &block else @_context[:before].concat(commands) end end |
#pane(*args, &block) ⇒ Object
Generates a pane in the terminal. These can be nested to create horizontal panes. Vertical panes are created with each top level nest.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/terminitor/dsl.rb', line 109 def pane(*args, &block) @_context[:panes] = {} unless @_context.has_key? :panes panes = @_context[:panes] pane_name = "pane#{panes.keys.size}" if block_given? pane_contents = panes[pane_name] = {:commands => []} if @_context.has_key? :is_first_lvl_pane # after in_context we should be able to access # @_context and @_old_context as before context = @_context old_context = @_old_context in_context pane_contents[:commands], &block clean_up_context(context, old_context) else pane_contents[:is_first_lvl_pane] = true in_context pane_contents, &block end else panes[pane_name] = { :commands => args } end end |
#run(*commands) ⇒ Object
stores command in context
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/terminitor/dsl.rb', line 46 def run(*commands) # if we are in a window context, append commands to default tab. if @_context.is_a?(Hash) && @_context[:tabs] current = @_context[:tabs]['default'][:commands] elsif @_context.is_a?(Hash) current = @_context[:commands] else current = @_context end current << commands.map do |c| c =~ /&$/ ? "(#{c})" : c end.join(" && ") end |
#setup(*commands, &block) ⇒ Object
Contains all commands that will be run prior to the usual ‘workflow’ e.g bundle install, setup forks, etc …
21 22 23 24 25 26 27 |
# File 'lib/terminitor/dsl.rb', line 21 def setup(*commands, &block) if block_given? in_context @setup, &block else @setup.concat(commands) end end |
#tab(*args, &block) ⇒ Object
sets command context to be run inside specific tab
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/terminitor/dsl.rb', line 82 def tab(*args, &block) tabs = @_context[:tabs] tab_name = "tab#{tabs.keys.size}" if block_given? tab_contents = tabs[tab_name] = {:commands => []} = {} = args.pop if args.last.is_a? Hash [:name] = args.first if args.first.is_a?(String) || args.first.is_a?(Symbol) tab_contents[:options] = unless .empty? in_context tab_contents, &block clean_up_context else tabs[tab_name] = { :commands => args} end end |
#to_hash ⇒ Hash
Returns yaml file as Terminitor formmatted hash
133 134 135 |
# File 'lib/terminitor/dsl.rb', line 133 def to_hash { :setup => @setup, :windows => @windows } end |
#window(options = {}, &block) ⇒ Object
sets command context to be run inside a specific window
35 36 37 38 39 40 |
# File 'lib/terminitor/dsl.rb', line 35 def window( = {}, &block) window_name = "window#{@windows.keys.size}" window_contents = @windows[window_name] = {:tabs => {'default' => {:commands =>[]}}} window_contents[:options] = unless .empty? in_context window_contents, &block end |