Class: Terminitor::Dsl

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

Overview

This class parses the Termfile to fit the new Ruby Dsl Syntax

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Dsl

Returns a new instance of Dsl.

Parameters:

  • path (String)

    to termfile



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

Examples:

window do
  before { run 'whoami' }
end

Parameters:

  • Array (Array<String>)

    of commands

  • (Proc)


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.

Examples:

pane "top"
pane { pane "uptime" }

Parameters:

  • Array (Array<String>)

    of comamnds

  • (Proc)


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

Examples:

run 'brew update'

Parameters:

  • Array (Array<String>)

    of commands



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 …

Examples:

setup "bundle install", "brew update"
setup { run('bundle install') }

Parameters:

  • array (Array<String>)

    of commands

  • (Proc)


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

Examples:

tab(:name => 'new tab', :settings => 'Grass') { run 'mate .' }
tab 'ls', 'gitx'

Parameters:

  • Array (Array<String>)

    of commands

  • (Proc)


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 => []}

    options = {}
    options = args.pop          if args.last.is_a? Hash
    options[:name] = args.first if args.first.is_a?(String) || args.first.is_a?(Symbol)

    tab_contents[:options] = options unless options.empty?

    in_context tab_contents, &block
    clean_up_context
  else
    tabs[tab_name] = { :commands => args}
  end
end

#to_hashHash

Returns yaml file as Terminitor formmatted hash

Returns:

  • (Hash)

    Return hash format of Termfile



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

Examples:

window(:name => 'new window', :size => [80,30], :position => [9, 100]) { tab('ls','gitx') }
window { tab('ls', 'gitx') }

Parameters:

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

    hash.

  • (Proc)


35
36
37
38
39
40
# File 'lib/terminitor/dsl.rb', line 35

def window(options = {}, &block)
  window_name     = "window#{@windows.keys.size}"
  window_contents = @windows[window_name] = {:tabs => {'default' => {:commands =>[]}}}
  window_contents[:options] = options unless options.empty?
  in_context window_contents, &block
end