Class: Yatte::TabManager

Inherits:
Object
  • Object
show all
Defined in:
lib/yatte/tab_manager.rb

Defined Under Namespace

Classes: Tab

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTabManager

Returns a new instance of TabManager.



10
11
12
13
# File 'lib/yatte/tab_manager.rb', line 10

def initialize
  @tabs = []
  @active_index = 0
end

Instance Attribute Details

#active_indexObject (readonly)

Returns the value of attribute active_index.



8
9
10
# File 'lib/yatte/tab_manager.rb', line 8

def active_index
  @active_index
end

#tabsObject (readonly)

Returns the value of attribute tabs.



8
9
10
# File 'lib/yatte/tab_manager.rb', line 8

def tabs
  @tabs
end

Instance Method Details

#activate(index) ⇒ Object



55
56
57
58
59
# File 'lib/yatte/tab_manager.rb', line 55

def activate(index)
  return if @tabs.empty?
  @active_index = index.clamp(0, @tabs.length - 1)
  active
end

#activeObject



71
72
73
# File 'lib/yatte/tab_manager.rb', line 71

def active
  @tabs[@active_index]
end

#close(index) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/yatte/tab_manager.rb', line 47

def close(index)
  @tabs.delete_at(index)
  return nil if @tabs.empty?

  @active_index = [index, @tabs.length - 1].min
  active
end

#countObject



75
76
77
# File 'lib/yatte/tab_manager.rb', line 75

def count
  @tabs.length
end

#next_tabObject



61
62
63
64
# File 'lib/yatte/tab_manager.rb', line 61

def next_tab
  return if @tabs.length <= 1
  @active_index = (@active_index + 1) % @tabs.length
end

#open(filename) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/yatte/tab_manager.rb', line 15

def open(filename)
  existing = @tabs.index { |t| t.filename == filename }
  if existing
    @active_index = existing
    return active
  end

  tab = create_tab(filename)
  if filename && File.exist?(filename)
    lines, error = FileIO.read(filename)
    if lines
      tab.buffer = Buffer.new(lines)
      tab.buffer.undo_stack = tab.undo_stack
      tab.cursor = Cursor.new(tab.buffer)
      tab.buffer.mark_clean
    else
      return error
    end
  end

  @tabs << tab
  @active_index = @tabs.length - 1
  active
end

#open_emptyObject



40
41
42
43
44
45
# File 'lib/yatte/tab_manager.rb', line 40

def open_empty
  tab = create_tab(nil)
  @tabs << tab
  @active_index = @tabs.length - 1
  active
end

#prev_tabObject



66
67
68
69
# File 'lib/yatte/tab_manager.rb', line 66

def prev_tab
  return if @tabs.length <= 1
  @active_index = (@active_index - 1) % @tabs.length
end