Class: DockDriver::WorkspacePager

Inherits:
Object
  • Object
show all
Extended by:
Configurability, Loggability
Includes:
Observable, Singleton
Defined in:
lib/dock_driver/workspace_pager.rb

Overview

Provides a list of workspaces as numbered rectangles that shows the number and presence of workspaces, the focused workspace, and any workspaces marked urgent. Example:

<%= pager %>

Constant Summary collapse

CONFIG_DEFAULTS =

Defaults for Configurability

{
  :format => "^ca(1,i3-msg workspace %s)^fg(%s)^%s(14x14)^p(-10)" + 
  "^ib(1)^fg(%s)%s^fg()^p(6)^fg()^ca()",
  :fg => '#808080',
  :bg => '#303030',
  :focused_fg => '#ffffff',
  :focused_bg => '#505050',
  :urgent_fg => '#800000',
  :urgent_bg => '#F00000'
}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWorkspacePager

A boring constructor.



72
73
74
75
76
77
# File 'lib/dock_driver/workspace_pager.rb', line 72

def initialize
  self.log.debug "WorkspacePager::new"
  @workspaces = []
  @thread = nil
  @i3 = nil
end

Class Attribute Details

.bgObject

A Foreground color for normal workspace squares.



45
46
47
# File 'lib/dock_driver/workspace_pager.rb', line 45

def bg
  @bg
end

.configObject

The loaded config



39
40
41
# File 'lib/dock_driver/workspace_pager.rb', line 39

def config
  @config
end

.fgObject

A Foreground color for normal workspace squares.



43
44
45
# File 'lib/dock_driver/workspace_pager.rb', line 43

def fg
  @fg
end

.focused_bgObject

A Foreground color for focused workspace squares.



49
50
51
# File 'lib/dock_driver/workspace_pager.rb', line 49

def focused_bg
  @focused_bg
end

.focused_fgObject

A Foreground color for focused workspace squares.



47
48
49
# File 'lib/dock_driver/workspace_pager.rb', line 47

def focused_fg
  @focused_fg
end

.formatObject

The format string for dzen2.



41
42
43
# File 'lib/dock_driver/workspace_pager.rb', line 41

def format
  @format
end

.urgent_bgObject

A Foreground color for urgent workspace squares.



53
54
55
# File 'lib/dock_driver/workspace_pager.rb', line 53

def urgent_bg
  @urgent_bg
end

.urgent_fgObject

A Foreground color for urgent workspace squares.



51
52
53
# File 'lib/dock_driver/workspace_pager.rb', line 51

def urgent_fg
  @urgent_fg
end

Instance Attribute Details

#workspacesObject

A list of workspaces.



69
70
71
# File 'lib/dock_driver/workspace_pager.rb', line 69

def workspaces
  @workspaces
end

Class Method Details

.configure(section) ⇒ Object

Merge in what Configurability finds.



57
58
59
60
61
62
63
64
65
66
# File 'lib/dock_driver/workspace_pager.rb', line 57

def self::configure( section )
  self.config = CONFIG_DEFAULTS.merge( section || {} )
  self.format = self.config[:format]
  self.fg = self.config[:fg]
  self.bg = self.config[:bg]
  self.focused_fg = self.config[:focused_fg]
  self.focused_bg = self.config[:focused_bg]
  self.urgent_fg = self.config[:urgent_fg]
  self.urgent_bg = self.config[:urgent_bg]
end

Instance Method Details

#i3Object

Lazily connect to i3.



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dock_driver/workspace_pager.rb', line 85

def i3
  if @i3.nil?
    @i3 = I3.new
    @i3.subscribe_workspaces
    self.update
  end
  return @i3
rescue Exception => e
  self.log.debug "The pager was unable to connect to i3 over IPC."
  self.log.debug e
end

#threadObject

Lazily create the thread to update the workspaces as necessary.



80
81
82
# File 'lib/dock_driver/workspace_pager.rb', line 80

def thread
  return @thread ||= Thread.new { loop { wait_for_event } }
end

#to_sObject

Return the pager as a formatted string for dzen2.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/dock_driver/workspace_pager.rb', line 105

def to_s
  return '' unless self.workspaces
  return self.workspaces.inject( '' ) do |str,space|
    if space['focused']
      fg = self.class.focused_fg
      bg = self.class.focused_bg
      op = 'r' # a filled rectangle
    elsif space['urgent']
      fg = self.class.urgent_fg
      bg = self.class.urgent_bg
      op = 'r' # a filled rectangle
    else
      fg = self.class.fg
      bg = self.class.bg
      op = 'ro' # a rectangle outline
    end
    str += self.class.format % 
      [space['num'], bg, op, fg, space['num']]
  end
end

#updateObject

Update the workspace list.



98
99
100
101
102
# File 'lib/dock_driver/workspace_pager.rb', line 98

def update
  self.workspaces = self.i3.get_workspaces.sort { |a,b| a['num'] <=> b['num'] }
  self.changed
  self.notify_observers
end