dag-amazing 0.1

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.
Files changed (42) hide show
  1. data/LICENSE +202 -0
  2. data/README.rdoc +72 -0
  3. data/bin/amazing +19 -0
  4. data/lib/amazing.rb +24 -0
  5. data/lib/amazing/awesome.rb +46 -0
  6. data/lib/amazing/cli.rb +66 -0
  7. data/lib/amazing/cli/commands.rb +240 -0
  8. data/lib/amazing/cli/helpers.rb +159 -0
  9. data/lib/amazing/cli/initializers.rb +60 -0
  10. data/lib/amazing/config.rb +66 -0
  11. data/lib/amazing/config/dsl.rb +32 -0
  12. data/lib/amazing/config/dsl/awesome.rb +26 -0
  13. data/lib/amazing/config/dsl/awesome/widget.rb +27 -0
  14. data/lib/amazing/config/yaml.rb +11 -0
  15. data/lib/amazing/helpers/pango_markup.rb +26 -0
  16. data/lib/amazing/numeric.rb +50 -0
  17. data/lib/amazing/options.rb +93 -0
  18. data/lib/amazing/proc_file.rb +59 -0
  19. data/lib/amazing/string.rb +19 -0
  20. data/lib/amazing/widget.rb +140 -0
  21. data/lib/amazing/widgets.rb +37 -0
  22. data/lib/amazing/widgets/ac_adapter.rb +31 -0
  23. data/lib/amazing/widgets/alsa.rb +35 -0
  24. data/lib/amazing/widgets/battery.rb +51 -0
  25. data/lib/amazing/widgets/clock.rb +31 -0
  26. data/lib/amazing/widgets/cpu_info.rb +35 -0
  27. data/lib/amazing/widgets/cpu_usage.rb +50 -0
  28. data/lib/amazing/widgets/file.rb +44 -0
  29. data/lib/amazing/widgets/file_system.rb +42 -0
  30. data/lib/amazing/widgets/gmail.rb +61 -0
  31. data/lib/amazing/widgets/maildir.rb +34 -0
  32. data/lib/amazing/widgets/memory.rb +47 -0
  33. data/lib/amazing/widgets/moc.rb +80 -0
  34. data/lib/amazing/widgets/mpd.rb +146 -0
  35. data/lib/amazing/widgets/net_traffic.rb +50 -0
  36. data/lib/amazing/widgets/noop.rb +24 -0
  37. data/lib/amazing/widgets/pacman.rb +34 -0
  38. data/lib/amazing/widgets/raggle.rb +42 -0
  39. data/lib/amazing/widgets/sup.rb +38 -0
  40. data/lib/amazing/x11.rb +15 -0
  41. data/lib/amazing/x11/display_name.rb +50 -0
  42. metadata +101 -0
@@ -0,0 +1,93 @@
1
+ # Copyright 2008 Dag Odenhall <dag.odenhall@gmail.com>
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'optparse'
16
+
17
+ module Amazing
18
+
19
+ # Parse and manage command line options
20
+ class Options < Hash
21
+ def initialize(args=ARGV)
22
+ @args = args
23
+ initialize_defaults
24
+ initialize_parser
25
+ end
26
+
27
+ def parse(args=@args)
28
+ @parser.parse!(args)
29
+ end
30
+
31
+ def help
32
+ @parser.help
33
+ end
34
+
35
+ private
36
+
37
+ def initialize_defaults
38
+ self[:config] = Dir["#{ENV["HOME"]}/.amazing/config.{rb,yml,yaml}"][0]
39
+ self[:loglevel] = "info"
40
+ self[:include] = []
41
+ self[:autoinclude] = true
42
+ self[:update] = []
43
+ end
44
+
45
+ def initialize_parser
46
+ @parser = OptionParser.new do |opts|
47
+ opts.on("-c", "--config FILE", "Configuration file (~/.amazing/config.{rb,yml,yaml})") do |config|
48
+ self[:config] = config
49
+ end
50
+
51
+ opts.on("-g", "--scaffold [AWESOMERC]", "Generate a scaffold config") do |awesomerc|
52
+ self[:scaffold] = awesomerc || File.expand_path("~/.awesomerc")
53
+ end
54
+
55
+ opts.on("-l", "--log-level LEVEL", "Severity threshold (info)") do |level|
56
+ self[:loglevel] = level
57
+ end
58
+
59
+ opts.on("-s", "--stop", "Stop the running amazing process") do
60
+ self[:stop] = true
61
+ end
62
+
63
+ opts.on("-i", "--include SCRIPT", "Include a widgets script") do |script|
64
+ self[:include] << script
65
+ end
66
+
67
+ opts.on("--no-auto-include", "Don't auto include from ~/.amazing/widgets/") do
68
+ self[:autoinclude] = false
69
+ end
70
+
71
+ opts.on("-u", "--update [WIDGET]", "Update a widget and exit") do |widget|
72
+ if widget
73
+ self[:update] << widget
74
+ else
75
+ self[:update] = :all
76
+ end
77
+ end
78
+
79
+ opts.on("-w", "--list-widgets [WIDGET]", "List available widgets or options and fields for a widget") do |widget|
80
+ self[:listwidgets] = widget || true
81
+ end
82
+
83
+ opts.on("-t", "--test-widget WIDGET [OPTIONS]", "Dump field values for a widget configured with inline YAML") do |widget|
84
+ self[:test] = widget
85
+ end
86
+
87
+ opts.on("-h", "--help", "You're looking at it") do
88
+ self[:help] = true
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,59 @@
1
+ # Copyright 2008 Dag Odenhall <dag.odenhall@gmail.com>
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Amazing
16
+
17
+ # Parse a /proc file
18
+ #
19
+ # cpuinfo = ProcFile.parse_file("cpuinfo")
20
+ # cpuinfo[1]["model name"]
21
+ # #=> "AMD Turion(tm) 64 X2 Mobile Technology TL-50"
22
+ class ProcFile
23
+ include Enumerable
24
+
25
+ def self.parse_file(file)
26
+ file = File.expand_path(file, "/proc")
27
+ new(File.new(file))
28
+ end
29
+
30
+ def initialize(string_or_io)
31
+ case string_or_io
32
+ when String
33
+ content = string_or_io
34
+ when IO
35
+ content = string_or_io.read
36
+ string_or_io.close
37
+ end
38
+ @list = [{}]
39
+ content.each_line do |line|
40
+ if sep = line.index(":")
41
+ @list[-1][line[0..sep-1].strip] = line[sep+1..-1].strip
42
+ else
43
+ @list << {}
44
+ end
45
+ end
46
+ @list.pop if @list[-1].empty?
47
+ end
48
+
49
+ def each
50
+ @list.each do |section|
51
+ yield section
52
+ end
53
+ end
54
+
55
+ def [](section)
56
+ @list[section]
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,19 @@
1
+ # Copyright 2008 Dag Odenhall <dag.odenhall@gmail.com>
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ class String
16
+ def camel_case
17
+ split(/[\s_-]/).map {|t| t[0].chr.upcase + t[1..-1] unless t.empty? }.join
18
+ end
19
+ end
@@ -0,0 +1,140 @@
1
+ # Copyright 2008 Dag Odenhall <dag.odenhall@gmail.com>
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'amazing/helpers/pango_markup'
16
+ require 'erb'
17
+
18
+ module Amazing
19
+
20
+ # Raised by widgets, and is then rescued and logged
21
+ class WidgetError < Exception
22
+ end
23
+
24
+ # Parent class for widget construction, example:
25
+ #
26
+ # class Clock < Widget
27
+ # description "Displays date and time"
28
+ # dependency "some/library", "how to get the library (url, gem name...)"
29
+ # option :time_format, "Time format as described in DATE(1)", "%R"
30
+ # field :time, "Formatted time"
31
+ # default "@time"
32
+ #
33
+ # init do
34
+ # @time = Time.now.strftime(@time_format)
35
+ # raise WidgetError, "An error occured!" if some_error?
36
+ # end
37
+ # end
38
+ class Widget
39
+ include Helpers::PangoMarkup
40
+ include ERB::Util
41
+
42
+ def initialize(opts={})
43
+ self.class.dependencies.each do |name, description|
44
+ begin
45
+ require name
46
+ rescue LoadError
47
+ raise WidgetError, "Missing dependency #{name.inspect}#{if description then " [#{description}]" end}"
48
+ end
49
+ end
50
+ self.class.options.each do |key, value|
51
+ instance_variable_set "@#{key}".to_sym, value[:default]
52
+ end
53
+ opts.each do |key, value|
54
+ instance_variable_set "@#{key}".to_sym, value
55
+ end
56
+ self.class.fields.each do |key, value|
57
+ instance_variable_set "@#{key}".to_sym, value[:default]
58
+ end
59
+ self.class.init.each do |block|
60
+ instance_eval(&block)
61
+ end
62
+ @default = case self.class.default
63
+ when Proc
64
+ instance_eval(&self.class.default)
65
+ when String
66
+ instance_eval(self.class.default)
67
+ end
68
+ end
69
+
70
+ def self.description(description=nil)
71
+ if description
72
+ @description = description
73
+ else
74
+ @description
75
+ end
76
+ end
77
+
78
+ def self.dependency(name, description=nil)
79
+ @dependencies ||= {}
80
+ @dependencies[name] = description
81
+ end
82
+
83
+ def self.dependencies
84
+ @dependencies || {}
85
+ end
86
+
87
+ def self.option(name, description=nil, default=nil)
88
+ @options ||= {}
89
+ @options[name] = {:description => description, :default => default}
90
+ end
91
+
92
+ def self.options
93
+ @options || {}
94
+ end
95
+
96
+ def self.field(name, description=nil, default=nil)
97
+ @fields ||= {}
98
+ @fields[name] = {:description => description, :default => default}
99
+ end
100
+
101
+ def self.fields
102
+ @fields || {}
103
+ end
104
+
105
+ def self.default(format=nil, &block) # :yields:
106
+ if format
107
+ @default = format
108
+ elsif block
109
+ @default = block
110
+ else
111
+ @default
112
+ end
113
+ end
114
+
115
+ def self.init(&block) # :yields:
116
+ if block
117
+ @init ||= []
118
+ @init << block
119
+ else
120
+ @init
121
+ end
122
+ end
123
+
124
+ def formatize(format=nil)
125
+ ERB.new(case format
126
+ when Proc
127
+ instance_eval(&format)
128
+ when String
129
+ instance_eval(format)
130
+ else
131
+ case self.class.default
132
+ when Proc
133
+ instance_eval(&self.class.default)
134
+ when String
135
+ instance_eval(self.class.default)
136
+ end
137
+ end.to_s).result(binding())
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,37 @@
1
+ # Copyright 2008 Dag Odenhall <dag.odenhall@gmail.com>
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'amazing/widgets/ac_adapter'
16
+ require 'amazing/widgets/alsa'
17
+ require 'amazing/widgets/battery'
18
+ require 'amazing/widgets/clock'
19
+ require 'amazing/widgets/cpu_info'
20
+ require 'amazing/widgets/cpu_usage'
21
+ require 'amazing/widgets/file'
22
+ require 'amazing/widgets/file_system'
23
+ require 'amazing/widgets/gmail'
24
+ require 'amazing/widgets/maildir'
25
+ require 'amazing/widgets/memory'
26
+ require 'amazing/widgets/moc'
27
+ require 'amazing/widgets/mpd'
28
+ require 'amazing/widgets/net_traffic'
29
+ require 'amazing/widgets/noop'
30
+ require 'amazing/widgets/pacman'
31
+ require 'amazing/widgets/raggle'
32
+ require 'amazing/widgets/sup'
33
+
34
+ module Amazing
35
+ module Widgets
36
+ end
37
+ end
@@ -0,0 +1,31 @@
1
+ # Copyright 2008 Dag Odenhall <dag.odenhall@gmail.com>
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'amazing/widget'
16
+ require 'amazing/proc_file'
17
+
18
+ module Amazing
19
+ module Widgets
20
+ class AcAdapter < Widget
21
+ description "AC adapter status"
22
+ field :online, "Online status"
23
+ default { @online ? "online" : "offline" }
24
+
25
+ init do
26
+ state = ProcFile.parse_file(Dir["/proc/acpi/ac_adapter/*/state"][0])[0]["state"]
27
+ @online = {"on-line" => true, "off-line" => false}[state]
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,35 @@
1
+ # Copyright 2008 Dag Odenhall <dag.odenhall@gmail.com>
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'amazing/widget'
16
+
17
+ module Amazing
18
+ module Widgets
19
+ class Alsa < Widget
20
+ description "Various data for the ALSA mixer"
21
+ option :mixer, "ALSA mixer name", "Master"
22
+ field :volume, "Volume in percentage", 0
23
+ default { @volume }
24
+
25
+ init do
26
+ IO.popen("amixer get #@mixer", IO::RDONLY) do |am|
27
+ out = am.read
28
+ volumes = out.scan(/\[(\d+)%\]/).flatten
29
+ volumes.each {|vol| @volume += vol.to_i }
30
+ @volume = @volume / volumes.size
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,51 @@
1
+ # Copyright 2008 Dag Odenhall <dag.odenhall@gmail.com>
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'amazing/widget'
16
+ require 'amazing/proc_file'
17
+
18
+ module Amazing
19
+ module Widgets
20
+ class Battery < Widget
21
+ description "Remaining battery power in percentage"
22
+ option :battery, "Battery number", 1
23
+ field :state, "Charging state, :charged, :charging or :discharging"
24
+ field :percentage, "Power percentage"
25
+ default { @percentage.to_i }
26
+
27
+ init do
28
+ batinfo = ProcFile.parse_file("acpi/battery/BAT#@battery/info")[0]
29
+ batstate = ProcFile.parse_file("acpi/battery/BAT#@battery/state")[0]
30
+ remaining = batstate["remaining capacity"].to_i
31
+ lastfull = batinfo["last full capacity"].to_i
32
+ @state = batstate["charging state"].to_sym
33
+ @percentage = (remaining * 100) / lastfull.to_f
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def charged?
40
+ @state == :charged
41
+ end
42
+
43
+ def charging?
44
+ @state == :charging
45
+ end
46
+
47
+ def discharging?
48
+ @state == :discharging
49
+ end
50
+ end
51
+ end