Sublets » History » Version 22
« Previous -
Version 22/66
(diff) -
Next » -
Current version
Anonymous, 09/03/2009 11:56 PM
Sublets\015\012\015\012Sublets are small Ruby scripts that provide the data for the statusbar. They are run in an embedded Ruby interpreter and are well included in the main loop of subtle.\015\012\015\012A compending list of the classes with it's functionality can be found in rdoc and informations about the available unit tests in the development section.\015\012\015\012h2. Types\015\012\015\012Currently there are two types of sublets:\015\012* sublets that are updated by given interval in seconds (default 60s)\015\012* sublets that are updated when a file is modified (Via inotify) or via socket\015\012\015\012The sublet data must be of type String, everything else will be ignored.\015\012\015\012h2. Customization\015\012\015\012The color of the ouput of a Sublets can be changed in this way:\015\012\015\012class Colorful < Subtle::Sublet\015\012 attr_accessor :red, :green, :blue\015\012\015\012 def initialize\015\012 @red = Subtle::Color.new("#ff0000")\015\012 @green = Subtle::Color.new("#00ff00")\015\012 @blue = Subtle::Color.new("#0000ff")\015\012 end\015\012\015\012 def run\015\012 self.data = @red + "su" + @green + "bt" + @blue + "le"\015\012 end\015\012end
\015\012\015\012There is also a way to add a X bitmap to a sublet:\015\012\015\012class Iconized < Subtle::Sublet\015\012 attr_accessor :icon\015\012\015\012 def initialize\015\012 @icon = Subtle::Icon.new("/usr/share/icons/subtle.xbm"\015\012 end\015\012\015\012 def run\015\012 self.data = @icon + "subtle"\015\012 end\015\012end
\015\012\015\012A nice collection of this pixmap can be found Subtle" class="external">here</a> will add a padding of 3px left and right of the pixmap, so keep that in mind when using the click callback._\015\012\015\012h2. Callbacks\015\012\015\012There are two methods that will be called from subtle on certain conditions:\015\012\015\012* click(): Whenever a mouse button is pressed on a sublets. Can have three arguments: button, x, y\015\012* run(): Whenever either the interval time is expired or the watched file is modified\015\012\015\012h2. Examples\015\012\015\012Below is the code of a shipped sublets that displays the time. It should be really straight forward:\015\012\015\012class Clock < Subtle::Sublet\015\012 def initialize\015\012 self.interval = 60 #< Set interval time\015\012 end\015\012\015\012 def run\015\012 self.data = Time.now.strftime("%d%m%y%H%M") #< Set data\015\012 end\015\012end\015\012
\015\012\015\012Another example for the inotify sublet which is also included within subtle:\015\012\015\012class Notify < Subtle::Sublet\015\012 attr_accessor :file\015\012\015\012 def initialize\015\012 @file = "/tmp/watch"\015\012\015\012 watch @file\015\012 end\015\012\015\012 def run\015\012 begin\015\012 self.data = IO.readlines(@file).first.chop #< Read data and strip\015\012 rescue => err #< Catch error\015\012 puts err\015\012 self.data = "subtle"\015\012 end\015\012 end\015\012end\015\012
\015\012\015\012The watch command also works with Ruby sockets, but be aware of blocking I/O:\015\012\015\012class Socket < Subtle::Sublet\015\012 attr_accessor :socket\015\012\015\012 def initialize\015\012 @socket = TCPSocket("localhost", 6600)\015\012\015\012 watch @socket\015\012 end\015\012\015\012 def run\015\012 begin\015\012 self.data = @socket.readline.chop #< Read data and strip\015\012 rescue => err #< Catch error\015\012 puts err\015\012 self.data = "subtle"\015\012 end\015\012 end\015\012end\015\012
\015\012\015\012_Subtle will automatically set sockets to O_NONBLOCK._\015\012\015\012And finally an example with a click callback:\015\012\015\012class Click < Subtle::Sublet\015\012 def initialize\015\012 self.interval = 999 #< Do nothing\015\012 end\015\012\015\012 def click(button, x, y)\015\012 find_client("xterm").raise\015\012 puts button, x, y\015\012 end\015\012\015\012 def run\015\012 # Do more nothing\015\012 end\015\012end\015\012
\015\012{{tocnavi(Subtler,Quickstart,Grabs)}}¶
class Colorful < Subtle::Sublet\015\012 attr_accessor :red, :green, :blue\015\012\015\012 def initialize\015\012 @red = Subtle::Color.new("#ff0000")\015\012 @green = Subtle::Color.new("#00ff00")\015\012 @blue = Subtle::Color.new("#0000ff")\015\012 end\015\012\015\012 def run\015\012 self.data = @red + "su" + @green + "bt" + @blue + "le"\015\012 end\015\012end
class Iconized < Subtle::Sublet\015\012 attr_accessor :icon\015\012\015\012 def initialize\015\012 @icon = Subtle::Icon.new("/usr/share/icons/subtle.xbm"\015\012 end\015\012\015\012 def run\015\012 self.data = @icon + "subtle"\015\012 end\015\012end
class Clock < Subtle::Sublet\015\012 def initialize\015\012 self.interval = 60 #< Set interval time\015\012 end\015\012\015\012 def run\015\012 self.data = Time.now.strftime("%d%m%y%H%M") #< Set data\015\012 end\015\012end\015\012
class Notify < Subtle::Sublet\015\012 attr_accessor :file\015\012\015\012 def initialize\015\012 @file = "/tmp/watch"\015\012\015\012 watch @file\015\012 end\015\012\015\012 def run\015\012 begin\015\012 self.data = IO.readlines(@file).first.chop #< Read data and strip\015\012 rescue => err #< Catch error\015\012 puts err\015\012 self.data = "subtle"\015\012 end\015\012 end\015\012end\015\012
class Socket < Subtle::Sublet\015\012 attr_accessor :socket\015\012\015\012 def initialize\015\012 @socket = TCPSocket("localhost", 6600)\015\012\015\012 watch @socket\015\012 end\015\012\015\012 def run\015\012 begin\015\012 self.data = @socket.readline.chop #< Read data and strip\015\012 rescue => err #< Catch error\015\012 puts err\015\012 self.data = "subtle"\015\012 end\015\012 end\015\012end\015\012
class Click < Subtle::Sublet\015\012 def initialize\015\012 self.interval = 999 #< Do nothing\015\012 end\015\012\015\012 def click(button, x, y)\015\012 find_client("xterm").raise\015\012 puts button, x, y\015\012 end\015\012\015\012 def run\015\012 # Do more nothing\015\012 end\015\012end\015\012