Project

General

Profile

unexist.dev

subtle

Assorted tidbits and projects

Sublets » History » Version 21

Anonymous, 09/02/2009 10:10 PM

1 21
h1. Sublets\015\012\015\012[[Sublets]] are small "Ruby":http://www.ruby-lang.org scripts that provide the data for the statusbar. They are run in an embedded "Ruby":http://www.ruby-lang.org interpreter and are well included in the main loop of [[subtle]].\015\012\015\012Unit tests for [[Subtlets|sublets]] are also included. [[Development|Read more about unit tests]]\015\012\015\012h2. Types\015\012\015\012Currently there are two types of [[Sublets|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":http://en.wikipedia.org/wiki/Inotify) or via socket\015\012\015\012The [[sublet]] data must be of type "String":http://www.ruby-doc.org/core/classes/String.html, everything else will be ignored.\015\012\015\012h2. Customization\015\012\015\012The color of the ouput of a [[sublet|Sublets]] can be changed in this way:\015\012\015\012<pre><code class="ruby">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</code></pre>\015\012\015\012There is also a way to add a "X bitmap":http://en.wikipedia.org/wiki/XBM to a [[Sublets|sublet]]:\015\012\015\012<pre><code class="ruby">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</code></pre>\015\012\015\012A nice collection of this pixmap can be found "here":http://dzen.geekmode.org/dwiki/doku.php?id=dzen:icon-packs.\015\012\015\012_[[Subtle]] 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 [[sublet|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 [[sublet|sublets]] that displays the time. It should be really straight forward:\015\012\015\012<pre><code class="ruby">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</code></pre>\015\012\015\012Another example for the "inotify":http://en.wikipedia.org/wiki/Inotify [[sublets|sublet]] which is also included within [[subtle]]:\015\012\015\012<pre><code class="ruby">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</code></pre>\015\012\015\012The *watch* command also works with "Ruby":http://www.ruby-lang.org sockets, but be aware of blocking I/O:\015\012\015\012<pre><code class="ruby">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</code></pre>\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\012<pre><code class="ruby">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</code></pre>\015\012{{tocnavi(Subtler,Quickstart,Grabs)}}