Jun'uary

Add awesome like layout to WMII

Jan 发表于 2008-08-31 23:42:26

Awesome is another tiling window manager which has many users on this planet (maybe more than wmii users?) It's very similar to xmonad(both use vim-style key-bindings), the main difference between them is awesome's configuration is written in a simple self-defined syntax instead of haskell which used by xmonad.

Simple syntax for configuration means easy to use, but its possibility of customization may be less than wmii and xmonad, that's why I still use wmii now. However, awesome(and xmonad) has an useful concept named 'layout' which wmii lack of. User is always working in a certain layout. Usually layout is composed of a main window who occupies the most area and other windows.



Screenshot is an example of layout: vim is now the main window. Awesome will move the new create window to the right area (the second picture shows the screen after I created a new terminal in wmii with awesome layout support), so you can do some temporary work or tail a log there then come back to main window.



In the contrast, wmii always open new window in the same column, make your main workarea smaller, and you have to manually move it to a new column (the last picture shows the screen after I created a new terminal in normal wmii).



That's why I wrote this piece of code to simulate the behavior of awesome in wmii. I think the code is self-explaination enough. It must be used with ruby-wmii.

First create a new file named jan-plugin.rb (or whatever name you like) in $HOME/.wmii-3.5/plugins dir.

Plugin.define "jan"  do
  author '"Jan X" <jan.h.xie@gmail.com>'

    layouts = {:default => nil,
    # keep left column as main area
    :right_main => lambda do |wmii, id|
      wmii.write '/tag/sel/ctl', 'send sel right'
    end,
    :left_main => lambda do |wmii, id|
      columns = wmii.read('/tag/sel/index').split("\n")
      has_only_one_column = (columns.find_all{|l| l =~ /^#/}.size < 3)
      cur = columns.find{|l| l =~ /#{id}/}

      # the strategy used here:
      # 1. do nothing to screen terminal
      # 2. when there's only one column, create new column at right, move new window to the column
      # 3. when there's multi column, if the new window is created in column 1, move it to right column
      # 4. else, do nothing
      wmii.write '/tag/sel/ctl', 'send sel right' if cur && (cur =~ /urxvt:URxvt/) && !(cur =~ /urxvt:URxvt:screen/) && (has_only_one_column || (cur[0..0] == '1'))
    end
  }

  bar_applet('layout', 999) do |wmii, bar|
    rules = wmii.plugin_config['jan:layout']['rules'] || {}
    tag = wmii.read('/tag/sel/ctl').split("\n")[0]

    # bar action
    switch = lambda do
    end
    wmii.on_key(*([wmii.plugin_config["jan:layout"]["layout_toggle_keys"] || ["MODKEY-/"]]).flatten, &switch)
    bar.on_click(&switch)
    bar.data = rules[tag] || "Default"

    Thread.new do
      open("|wmiir read /event") do |is|
        loop do
          case is.gets
          when /CreateClient (.*)/
            id =
            tag = wmii.read('/tag/sel/ctl').split("\n")[0]
            layouts[rules[tag]].call(wmii, id) if rules[tag] && layouts[rules[tag]]
          when /FocusTag (.*)/
            bar.data = rules[] || "Default"
          end
        end
      end
    end
  end

end

Extend WMII::Configuration in your wmiirc-config.rb:

class WMII::Configuration
  def colrules(*val)
    if val[0].nil?
      @ixp_conn.read("/colrules")
    else
      @ixp_conn.write "/colrules", val[0]
    end
  end
end

Set colrules in wmiirc-config:

WMII:Configuration.define do
...
  colrules <<EOF
/www/ -> 70+30
/.*/ -> 60+40
EOF
...
end
Now the new created column in www tag will has a width equal to 30% of screen width. Finally set applet params and use the new applet:

  plugin_config["jan:layout"]["rules"] = {
    "www" => :left_main
  }

  from "jan" do
    use_bar_applet "acpi", 850
    use_bar_applet "layout", 950 # here we tell ruby-wmii to use the new defined layout applet
    use_binding "open-document" # this's another extension I made, I'll post it later when I have time
  end

Then you'll have a awesome like layout in wmii!

ps. Notice how cool is ruby's kernel#open method? In the script I pass it a string "|wmiir read /event", then it will read "wmiir read /event"'s output from pipe! So smart. If we don't add "|" before the string the call will block, because read /event will never get EOF. kernel#open can do more than this, you can even pass a url to it and it will fetch the resource for you by using open-uri lib.
关键词(Tag): ruby layout awesome wmii



收藏: QQ书签 del.icio.us 订阅: Google 抓虾

最新评论


  • 八爪
    2008-09-20 23:39:15 匿名 116.232.*.*

    越来越有深度了,博写的很专业啊

发表评论

* 昵称

已经注册过? 请登录

新用户请先注册 以便能显示头像及追踪评论回复

Email
网址
* 评论
表情
 
 

分类小组论坛
杂谈, 娱乐、八卦, 文学、艺术, 体育, 旅游、同城, 象牙塔, 情感, 时尚、生活, 星座, 科技

请注意遵守中华人民共和国法律法规, 如威胁到本站生存, 将依法向有关部门报告, 同时本站的相关记录可能成为对您不利的证据.

相关法律法规
全国人大常委会关于维护互联网安全的决定
中华人民共和国计算机信息系统安全保护条例
中华人民共和国计算机信息网络国际联网管理暂行规定
计算机信息网络国际联网安全保护管理办法
计算机信息系统国际联网保密管理规定

Jan'uary

人类一思考,老夫就发笑

搜索

日历