Class: Termup::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/termup/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(project) ⇒ Base

Returns a new instance of Base.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/termup/base.rb', line 5

def initialize(project)
  @handler = Termup::Handler.new

  config = YAML.load(File.read("#{TERMUP_DIR}/#{project}.yml"))
  @tabs = config['tabs']

  # Config file compatibility checking
  if @tabs.is_a?(Array) and @tabs.first.is_a?(Hash)
    abort 'YAML syntax for config has been changed. See https://github.com/kenn/termup for details.'
  end

  @options = config['options'] || {}
  @iterm_options = @options['iterm']

  # Split panes for iTerm 2
  split_panes if @handler.iterm? and @iterm_options

  # Setting up tabs / panes
  @tabs.each_with_index do |(tabname, values), index|
    # Set tab title
    @handler.set_property(:name, tabname)

    # Run commands
    (advanced_iterm? ? values['commands'] : values).each do |command|
      @handler.run command
    end

    # Layout
    if advanced_iterm?
      values['properties'].each do |key, value|
        @handler.set_property(key, value)
      end if values['properties']

      values['layout'].each do |command|
        layout command
      end if values['layout']
    else
      # Move to next
      if @iterm_options
        layout :goto_next_pane
      else
        if index < @tabs.size - 1
          layout :new_tab
          sleep 0.01 # Allow some time to complete opening a new tab
        else
          layout :goto_next_tab # Back home
        end
      end
    end
  end
end

Instance Method Details

#advanced_iterm?Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/termup/base.rb', line 73

def advanced_iterm?
  unless defined?(@advanced_iterm)
    @advanced_iterm = case @tabs.values.first
    when Hash   then true
    when Array  then false
    else
      abort 'invalid YAML format'
    end
    abort 'advanced config only supported for iTerm' if @advanced_iterm and !@handler.iterm?
  end
  @advanced_iterm
end

#layout(command) ⇒ Object



86
87
88
# File 'lib/termup/base.rb', line 86

def layout(command)
  @handler.layout(command)
end

#split_panesObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/termup/base.rb', line 57

def split_panes
  width, height = @iterm_options['width'], @iterm_options['height']
  return unless width and height

  (width - 1).times do
    layout :split_vertically
  end
  layout :goto_next_pane # Back home
  width.times do
    (height - 1).times do
      layout :split_horizontally
    end
    layout :goto_next_pane # Move to next, or back home
  end
end