Project

General

Profile

unexist.dev

Assorted tidbits and projects

Snippets » History » Version 5

Anonymous, 04/21/2010 09:50 PM

1 5
h1. Recipes\015\012\015\012This page shows small snippets that might be useful:\015\012\015\012{{>toc}}\015\012\015\012h2. Gravity tiling\015\012\015\012<pre><code class="ruby">\015\012def gravity_tile(v = nil)\015\012  v ||= Subtlext::View.current\015\012\015\012  clients = v.clients\015\012  screen  = Subtlext::Screen.current\015\012\015\012  assoc = {}\015\012\015\012  clients.each do |c|\015\012    begin\015\012      assoc[c.gravity.id] << c rescue assoc[c.gravity.id] = [ c ]\015\012    rescue StandardError\015\012      # Startup errors\015\012    end\015\012  end\015\012\015\012  assoc.each do |k, v|\015\012    g = Subtlext::Gravity[k]\015\012\015\012    # Calculate gravity\015\012    w = screen.geometry.width * g.geometry.width / 100\015\012    h = screen.geometry.height * g.geometry.height / 100\015\012    x = screen.geometry.x + (screen.geometry.width - w) * g.geometry.x / 100\015\012    y = screen.geometry.y + (screen.geometry.height - h) * g.geometry.y / 100\015\012\015\012    width = w / v.size\015\012    pos   = x\015\012\015\012    v.each do |c|\015\012      c.resize   = false\015\012      c.geometry = [ pos, y, width - 2 * OPTIONS[:border], h - 2 * OPTIONS[:border] ]\015\012      pos += width\015\012    end\015\012  end\015\012end\015\012</code></pre>\015\012\015\012<pre><code class="ruby">\015\012HOOKS = {\015\012  :HookClientGravity => lambda { |c| gravity_tile },\015\012}\015\012</code></pre>\015\012\015\012h2. Rotate\015\012\015\012This will rotate the windows on the current view clockwise.\015\012\015\012<pre><code class="ruby">\015\012"S-KP_Divide" => lambda {\015\012  current_view.clients.each do |c|\015\012    c.gravity = \015\012      case(c.gravity)\015\012        when 7: 8\015\012        when 8: 6\015\012        when 9: 6\015\012        when 6: 2\015\012        when 3: 2\015\012        when 2: 4\015\012        when 1: 4\015\012        when 4: 8\015\012      end\015\012  end  \015\012}\015\012</code></pre>\015\012\015\012h2. Swap\015\012\015\012This two snippets toggle the gravity mode of two windows\015\012\015\012<pre><code class="ruby">\015\012"S-KP_Add" => lambda {\015\012  current_view.clients.each do |c|\015\012    c.gravity = \015\012      case(c.gravity)\015\012        when 4: 4.to_mode33.to_horz\015\012        when 6: 6.to_mode66.to_horz\015\012        when 8: 8.to_mode33.to_vert\015\012        when 2: 2.to_mode66.to_vert          \015\012      end\015\012  end\015\012}\015\012</code></pre>\015\012\015\012<pre><code class="ruby">\015\012"S-KP_Subtract" => lambda {\015\012  current_view.clients.each do |c|\015\012    c.gravity = \015\012      case(c.gravity)\015\012        when 4: 4.to_mode66.to_horz\015\012        when 6: 6.to_mode33.to_horz\015\012        when 8: 8.to_mode66.to_vert\015\012        when 2: 2.to_mode33.to_vert\015\012      end\015\012    end\015\012}\015\012</code></pre>\015\012\015\012h2. Save/load\015\012\015\012This recipe saves/loads the gravities of the windows of the current view into/from a yaml file.\015\012\015\012<pre><code class="ruby">\015\012"S-KP_Add" => lambda {\015\012  require "yaml"\015\012\015\012  gravities = {}\015\012\015\012  current_view.clients.each do |c|\015\012    grav = c.gravity\015\012\015\012    grav = grav.to_horz   if(c.horz?) \015\012    grav = grav.to_vert   if(c.vert?)\015\012    grav = grav.to_mode33 if(c.mode33?)\015\012    grav = grav.to_mode66 if(c.mode66?)\015\012\015\012    gravities[c.name] = grav\015\012  end\015\012\015\012  begin\015\012    File.open(ENV["HOME"] + "/gravities.yaml", "w") { |file| file.puts(gravities.to_yaml) }\015\012  rescue\015\012    puts "Failed writing file"\015\012  end\015\012}\015\012</code></pre>\015\012\015\012<pre><code class="ruby">\015\012"S-KP_Subtract" => lambda {\015\012  require "yaml"\015\012\015\012  gravities = {}\015\012\015\012  begin\015\012    File.open(ENV["HOME"] + "/gravities.yaml", "r") { |file|\015\012      gravities = YAML.load(file)\015\012    }\015\012  rescue\015\012    puts "Failed reading file"\015\012  end\015\012\015\012  gravities.each { |k, v|\015\012    c = find_client(k)\015\012    c.gravity = v if(!c.nil?)\015\012  }\015\012}\015\012</code></pre>\015\012\015\012h2. Move client\015\012\015\012The following will help to move a client to a specific view.\015\012\015\012<pre><code class="ruby">\015\012def move_view(client, view_id)\015\012  begin\015\012    cur  = current_view\015\012    view = views[view_id - 1]\015\012\015\012    begin\015\012      tag = find_tag(view.name)\015\012    rescue\015\012      tag = add_tag(view.name)\015\012    end\015\012\015\012    # Remove client from current tag\015\012    if(client.has_tag?(cur.name))\015\012      client.untag(cur.name)\015\012    end\015\012\015\012    # Tag new view with itself\015\012    if(!view.has_tag?(tag))\015\012      view.tag(tag)\015\012    end\015\012\015\012    # Finally add tag to client\015\012    client.tag(tag)\015\012  rescue => err\015\012    puts err\015\012  end\015\012end\015\012\015\012GRABS = {\015\012  "A-C-1" => lambda { |c| move_view(c, 1) },\015\012  "A-C-2" => lambda { |c| move_view(c, 2) },\015\012  "A-C-3" => lambda { |c| move_view(c, 3) },\015\012  "A-C-4" => lambda { |c| move_view(c, 4) },\015\012}\015\012</code></pre>\015\012\015\012h2. Copy client\015\012\015\012Similiar to the recipe above this will not move but copy the client to a view.\015\012\015\012<pre><code class="ruby">\015\012def copy_view(client, view_id) # {{{\015\012  begin\015\012    cur  = current_view\015\012    view = views[view_id - 1]\015\012\015\012    begin\015\012      tag = find_tag(view.name)\015\012    rescue\015\012      tag = add_tag(view.name)\015\012    end\015\012\015\012    # Tag new view with itself\015\012    if(!view.has_tag?(tag))\015\012      view.tag(tag)\015\012    end\015\012\015\012    # Finally add tag to client\015\012    client.tag(tag)\015\012  rescue => err\015\012    puts err\015\012  end\015\012end\015\012\015\012GRABS = {\015\012  "A-W-1" => lambda { |c| copy_view(c, 1) },\015\012  "A-W-2" => lambda { |c| copy_view(c, 2) },\015\012  "A-W-3" => lambda { |c| copy_view(c, 3) },\015\012  "A-W-4" => lambda { |c| copy_view(c, 4) },\015\012}\015\012</code></pre>\015\012\015\012