r3status 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5a0f7b146cec610563175f09c27651110f1cc009
4
+ data.tar.gz: 1ff0569ec99476d1c1e1f7be73dd85fb274cd264
5
+ SHA512:
6
+ metadata.gz: ebe6cdeb3ce62d85c12777276c336bd21fa1d237cb2bb287e92d341c762f770b0d9a12baf212a7200ef1cd8c155a54dd6d55017d45a9befc67f51c46a3c81465
7
+ data.tar.gz: aec9b989dd1df12877ddcd254959f5f40ef1f0c621304ff111d6f262684b4006960b3863f05048b390b3164e050153c4f14c8376edbcff7561fb4f9d8da7e245
@@ -0,0 +1,23 @@
1
+ module R3Status
2
+ class BatteryBlock < Block
3
+ attr_accessor :path
4
+
5
+ def initialize(**args)
6
+ args = {colors: {charging: '#6DBB45', discharging: '#F4C91D'},
7
+ formats: {charging: 'Bat(charge) %{capacity}%',
8
+ default: 'Bat %{capacity}%'}}.merge(args)
9
+ super(args)
10
+ end
11
+
12
+ def update
13
+ cap, st = capacity, status
14
+
15
+ @full_text = formats[st] % {val: cap, capacity: cap, status: st}
16
+ @text_color = colors[st]
17
+ end
18
+
19
+ private
20
+ def status; `cat #{path}status`.chomp.downcase.to_sym end
21
+ def capacity; `cat #{path}capacity`.chomp end
22
+ end
23
+ end
@@ -0,0 +1,28 @@
1
+ module R3Status
2
+ class Block
3
+ DEFAULT_COLOR = '#ffffff'
4
+ DEFAULT_FORMAT = '%{val}'
5
+
6
+ attr_accessor :full_text, :text_color, :name, :formats, :colors
7
+
8
+ def initialize(**args)
9
+ args.each do |k, v|
10
+ v.default = v[:default] if (v.is_a? Hash) && (v.key? :default)
11
+ send "#{k}=", v
12
+ end
13
+
14
+ self.colors ||= {}
15
+ self.formats ||= Hash.new(DEFAULT_FORMAT)
16
+ end
17
+
18
+ def update; end
19
+ def clicked(button, x, y); end
20
+
21
+ def to_s(prefix: nil, postfix: nil)
22
+ %({"full_text": "#{prefix}#{full_text}#{postfix}",
23
+ "color": "#{text_color || DEFAULT_COLOR}", "name": "#{name}",
24
+ "separator": false})
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,11 @@
1
+ require_relative './block.rb'
2
+
3
+ require_relative './static_block.rb'
4
+
5
+ require_relative './time_block.rb'
6
+
7
+ require_relative './volume_block.rb'
8
+
9
+ require_relative './battery_block.rb'
10
+
11
+ require_relative './keyboard_layout_block.rb'
@@ -0,0 +1,17 @@
1
+ module R3Status
2
+ class KeyboardLayoutBlock < Block
3
+ def initialize(**args)
4
+ super(args)
5
+ end
6
+
7
+ def update
8
+ sym = language_symbol
9
+ @full_text = formats[sym] % {val: sym, sym: sym}
10
+ @text_color = colors[sym]
11
+ end
12
+
13
+ def language_symbol
14
+ `xkblayout-state print %s`.chomp.to_sym
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module R3Status
2
+ class StaticBlock < Block
3
+ attr_accessor :clicked_block
4
+
5
+ def initialize(**args, &block)
6
+ @clicked_block = block
7
+
8
+ super(args)
9
+ end
10
+
11
+ def clicked(button, x, y)
12
+ return if clicked_block.nil?
13
+ args = [self, button, x, y].take(clicked_block.arity)
14
+ clicked_block.call(*args)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ module R3Status
2
+ class TimeBlock < Block
3
+ attr_accessor :format
4
+
5
+ def initialize(format: "%H:%m @ %e/%M/%Y", **args)
6
+ @format = format
7
+
8
+ super()
9
+ end
10
+ def update
11
+ self.full_text = Time.now.strftime(format)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,48 @@
1
+ module R3Status
2
+ class VolumeBlock < Block
3
+ attr_accessor :format, :format_muted, :default_color, :color_muted, :step
4
+
5
+ def initialize(**args)
6
+ args = {formats: {muted: " %{volume}%", unmuted: " %{volume}%"},
7
+ colors: {muted: '#7CCdCD', unmuted: '#ffffff'}, step: 5,
8
+ name: "volume_master"}.merge(args)
9
+
10
+ super(args)
11
+ end
12
+
13
+ def update
14
+ vol = volume
15
+ muted = muted? ? :muted : :unmuted
16
+ @text_color = colors[muted]
17
+ @full_text = formats[muted] % {val: vol, volume: vol}
18
+ end
19
+
20
+ def clicked button, x, y
21
+ case button
22
+ when 1, 4
23
+ increase
24
+ when 2
25
+ toggle_mute
26
+ when 3, 5
27
+ decrease
28
+ end
29
+ end
30
+
31
+ private
32
+ def toggle_mute
33
+ `amixer -q set Master toggle`
34
+ end
35
+ def increase
36
+ `amixer -c 0 set Master #{step}+ unmute`
37
+ end
38
+ def decrease
39
+ `amixer -c 0 set Master #{step}- unmute`
40
+ end
41
+ def volume
42
+ `amixer -c 0 get Master | grep Mono: | cut -d " " -f6 | tr -d [,],%`.to_i
43
+ end
44
+ def muted?
45
+ `amixer -c 0 get Master | grep Mono: | cut -d " " -f8 | tr -d [,]`.chomp == "off"
46
+ end
47
+ end
48
+ end
data/lib/r3status.rb ADDED
@@ -0,0 +1,63 @@
1
+ require_relative './r3status/blocks.rb'
2
+ require 'json'
3
+
4
+ module R3Status
5
+ def loop_with_interval secs
6
+ loop do
7
+ start = Time.now
8
+ yield
9
+ interval = secs - (Time.now - start)
10
+ sleep(interval) if interval > 0
11
+ end
12
+ end
13
+
14
+ class StatusLine
15
+ attr_reader :blocks, :prefix, :postfix, :update_interval
16
+
17
+ def initialize(prefix: " ", postfix: nil, update_interval: 0.4)
18
+ @blocks = []
19
+ @prefix = prefix
20
+ @postfix = postfix
21
+ @update_interval = update_interval
22
+ end
23
+
24
+ def << block
25
+ @blocks << block
26
+ end
27
+
28
+ def transmission
29
+ @blocks.map { |i| i.to_s(postfix: @postfix, prefix: @prefix) }.
30
+ reject { |i| i.nil? }.inject { |i, j| i << ",#{j}"}
31
+ end
32
+ def parse str
33
+ return if str.length < 2
34
+ obj = nil
35
+
36
+ str = str.sub(/\A\,/ , "")
37
+ File.open("input.txt", 'a') {|f| f.puts str }
38
+ begin
39
+ obj = JSON.parse(str)
40
+ rescue Exception => e
41
+ File.open("log.txt", 'a') {|f| f.puts e.to_s }
42
+ return
43
+ end
44
+ block = @blocks.find { |b| b.name == obj["name"] }
45
+ block.clicked(obj["button"], obj["x"], obj["y"]) unless block.nil?
46
+ end
47
+ def run
48
+
49
+ fork do
50
+ loop do
51
+ s = gets.chomp
52
+ parse s
53
+ end
54
+ end
55
+
56
+ puts "{\"version\":1, \"click_events\":true}["
57
+ loop_with_interval(0.2) do
58
+ @blocks.each {|b| b.update }
59
+ puts "[#{transmission}],"
60
+ end
61
+ end
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: r3status
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gilad Naaman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: r3status is ruby implmentation of the i3bar protocol, and is an alternative
14
+ to the default i3status.
15
+ email: gilad.doom@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/r3status.rb
21
+ - lib/r3status/battery_block.rb
22
+ - lib/r3status/block.rb
23
+ - lib/r3status/blocks.rb
24
+ - lib/r3status/keyboard_layout_block.rb
25
+ - lib/r3status/static_block.rb
26
+ - lib/r3status/time_block.rb
27
+ - lib/r3status/volume_block.rb
28
+ homepage: https://rubygems.org/gems/example
29
+ licenses:
30
+ - LGPL3
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.2.2
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: i3status replacement written in ruby.
52
+ test_files: []