Class: TermDump::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/termdump/session.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Session

Optional Configures:

  • terminal: The type of terminal

  • new_window: The key sequence used to open a new window

  • new_tab: The key sequence used to open a new tab

  • new_vsplit: The key sequence used to open a new vsplit

  • new_hsplit: The key sequence used to open a new hsplit



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/termdump/session.rb', line 11

def initialize config={}
  if config.nil? || !config.has_key?('terminal')
    raise ArgumentError.new(
      "You need to set 'terminal' in the configure file")
  end
  begin
    terminal = config['terminal']
    require_relative "terminal/#{terminal}"
  rescue LoadError
    puts "Load with terminal #{terminal} error:"
    puts "Not support #{terminal} yet!"
    exit 0
  end

  @terminal = Terminal.new(config)
  @node_queue = []
  @support_split = Terminal.public_method_defined?(:vsplit) && 
    Terminal.public_method_defined?(:hsplit)
  @support_tab = Terminal.public_method_defined?(:tab)

  if config.has_key?('delay') && config['delay'] == config['delay'].to_i
    @terminal.delay = config['delay']
  end
end

Instance Method Details

#enqueue(type, name, attributes) ⇒ Object



42
43
44
45
# File 'lib/termdump/session.rb', line 42

def enqueue type, name, attributes
  @node_queue.push(Node.new(type, name, 
                            attributes['cwd'], attributes['command']))
end

#execObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/termdump/session.rb', line 76

def exec
  current_tab = @node_queue.first
  @terminal.exec current_tab.cwd, current_tab.command
  @node_queue.shift
  @node_queue.each do |node|
    case node.type
    when :window, :tab, :vsplit, :hsplit
      @terminal.method(node.type).call(node.name, node.cwd, node.command)
    end
  end
end

#fallbackObject



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/termdump/session.rb', line 62

def fallback
  unless @support_split
    @node_queue.each_index do |i|
      if @node_queue[i].type == :vsplit || @node_queue[i].type == :hsplit
        @node_queue[i].type = :tab
      end
    end
  end
  unless @support_tab
    @node_queue.each_index { |i|
      @node_queue[i].type = :window if @node_queue[i].type == :tab }
  end
end

#replay(task) ⇒ Object



36
37
38
39
40
# File 'lib/termdump/session.rb', line 36

def replay task
  scan task
  fallback
  exec
end

#scan(node) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/termdump/session.rb', line 47

def scan node
  node.each_pair do |k, v|
    if k.start_with?('tab')
      enqueue :tab, k, v
    elsif k.start_with?('vsplit')
      enqueue :vsplit, k, v
    elsif k.start_with?('hsplit')
      enqueue :hsplit, k, v
    elsif k.start_with?('window')
      enqueue :window, k, v
    end
    scan v if v.is_a?(Hash)
  end
end