Class: Barabara::Modules::WM

Inherits:
Object
  • Object
show all
Includes:
Wisper::Publisher
Defined in:
lib/barabara/modules/wm.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWM

Returns a new instance of WM.



8
9
10
11
12
13
# File 'lib/barabara/modules/wm.rb', line 8

def initialize
  @tag_icons = GlobalConfig.config.module_config('wm')['tag_icons'] || {}
  @colors = GlobalConfig.config.colors
  @monitors = GlobalConfig.config.monitors
  @cmd, @wm = detect_wm(GlobalConfig.config.session)
end

Instance Attribute Details

#cmdObject (readonly)

Returns the value of attribute cmd.



26
27
28
# File 'lib/barabara/modules/wm.rb', line 26

def cmd
  @cmd
end

#wmObject (readonly)

Returns the value of attribute wm.



26
27
28
# File 'lib/barabara/modules/wm.rb', line 26

def wm
  @wm
end

Class Method Details

.get_monitors(wmname = ) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'lib/barabara/modules/wm.rb', line 124

def self.get_monitors(wmname = ENV['XDG_SESSION_DESKTOP'])
  case wmname
  when 'herbstluftwm'
    `herbstclient list_monitors`.scan(/^\d+/)
  when 'bspwm'
    `bspc query -M --names`.split("\n")
  else
    raise NameError, 'Unknown WM!'
  end
end

Instance Method Details

#detect_wm(wmname) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/barabara/modules/wm.rb', line 15

def detect_wm(wmname)
  case wmname
  when 'herbstluftwm'
    ['herbstclient --idle', 'hlwm']
  when 'bspwm'
    ['bspc subscribe report', 'bspwm']
  else
    raise NameError, 'Unknown WM!'
  end
end

#hlwm_taglineObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/barabara/modules/wm.rb', line 73

def hlwm_tagline
  tagline = {}
  @monitors.each do |monitor|
    # Switch the font to glyphs:
    tagline[monitor] = '%{T2}'
    # Read the tag list:
    tags = `herbstclient tag_status #{monitor}`.chomp.split("\t").drop(1)
    tagline[monitor] << parse_hlwm_tags(tags, monitor) << '%{T1}'
  end
  tagline
end

#parse_bspwm_tags(tags) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/barabara/modules/wm.rb', line 98

def parse_bspwm_tags(tags)
  tagline = {}
  monitor = ''
  tags.each do |tag|
    next if tag[0] =~ /[LTG]/

    status, name = tag.slice!(0), tag
    if status.casecmp?('m')
      monitor = name
      tagline[monitor] = ''
      next unless @monitors.count > 1

      vars = { name: name }.merge(tag_color(status))
      tagline[monitor] << format('%%{B%<bg>s}%%{F%<fg>s}'\
                                 '%%{A:bspc monitor -f %<name>s:}'\
                                 ' %<name>s %%{A}', vars)
    else
      vars = { name: name, icon: @tag_icons[name] || name }.merge(tag_color(status))
      tagline[monitor] << format('%%{B%<bg>s}%%{F%<fg>s}'\
                                 '%%{A:bspc desktop -f %<name>s:}'\
                                 ' %<icon>s %%{A}', vars)
    end
  end
  tagline
end

#parse_hlwm_tags(tags, monitor) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/barabara/modules/wm.rb', line 85

def parse_hlwm_tags(tags, monitor)
  line = ''
  tags.each do |tag|
    status, name = tag.slice!(0), tag
    vars = { monitor: monitor, tag: name,
             icon: @tag_icons[name] }.merge(tag_color(status))
    line << format('%%{B%<bg>s}%%{F%<fg>s}%%{A:herbstclient chain .-. '\
                   'focus_monitor %<monitor>s .-. use %<tag>s:}'\
                   ' %<icon>s %%{A}', vars)
  end
  line
end

#parse_line(line) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/barabara/modules/wm.rb', line 28

def parse_line(line)
  case @wm
  when 'hlwm'
    command, *args = line.split("\t")
    if command == 'tag_changed'
      publish(:event, 'tagline', hlwm_tagline)
    else
      publish(:event, command, args)
    end
  when 'bspwm'
    # We remove first char here, because it's insignificant in our case.
    tags = line[1..-1].split(':')
    publish(:event, 'tagline', parse_bspwm_tags(tags))
  end
end

#tag_color(status) ⇒ Object



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

def tag_color(status)
  ## Tag statuses:
  #  '#' -- Tag is active and focused on current monitor;
  #  '+' -- Tag is active on current monitor,
  #         but another monitor is focused;
  #  ':' -- Tag is not active, but contains windows;
  #  '!' -- Tag contains an urgent window.
  case status
  when /[#OF]/ then { bg: @colors[:ac_winbr], fg: @colors[:se_text] }
  when /[+M]/ then { bg: '#9CA668', fg: @colors[:se_text] }
  when /[:o]/ then { bg: @colors[:in_framebr], fg: @colors[:ac_text] }
  when /[!uU]/ then { bg: @colors[:ur_winbr], fg: @colors[:se_text] }
  when /[-%m]/ then { bg: @colors[:in_text], fg: @colors[:in_framebr] }
  else { bg: @colors[:in_framebr], fg: @colors[:in_text] }
  end
end

#watchObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/barabara/modules/wm.rb', line 44

def watch
  if @wm == 'hlwm'
    publish(:event, 'tagline', hlwm_tagline)
  else
    parse_line(`bspc wm -g`.chomp)
  end
  PTY.spawn(@cmd) do |read, _write, pid|
    read.each { |line| parse_line(line.chomp) }
    Process.wait pid
  end
end