Preface

Goal: Separate Main Flow, Code, and Data.

So anyone can focus to alter special customization in Main Script, without changing the whole stuff.

Reading

Before you jump off to scripting, you might desire to read this overview.

All The Source Code:

Impatient coder like me, like to open many tab on browser.


Modularized HerbstluftWM in Many Languages

This article is one part of a collection. All integrated, on related to another. So we can compare each other quickly.

Tutorial/ Guidance/ Article: [ Modularized Overview ] [ BASH ] [ Perl ] [ Python ] [ Ruby ] [ PHP ] [ Lua ] [ Haskell ]

Source Code Directory: [ BASH ] [ Perl ] [ Python ] [ Ruby ] [ PHP ] [ Lua ] [ Haskell ]


Directory Structure

Directory Structure has been explained in preface. This figure will explain how it looks in Ruby script directory.

HerbstluftWM: Directory Structure


Modularizing in Ruby

Nothing to say here. Ruby is simple

Declare a module

No need to explicitly define what to export.

Here we export hc function variable from helper script.

Nothing to write in helper Script

You can wrap the script in module.

module Config
    Tag_names = Array (1..9)
    Tag_keys  = (Array (1..9)) << 0
end

Call a module

require_relative 'helper'
require_relative 'config'

System Calls

Here we wrap herbstclient system call in a function named hc.

helper.rb

def hc(arguments)
    system("herbstclient #{arguments}");
end

autostart.rb

# Read the manual in $ man herbstluftwm
hc('emit_hook reload')

# gap counter
system("echo 35 > /tmp/herbstluftwm-gap");

Array: Tag Names and Keys

config.rb

    Tag_names = Array (1..9)
    Tag_keys  = (Array (1..9)) << 0

HerbstluftWM: Tag Status


Hash: Color Schemes

Using key-value pairs, a simple data structure.

assets/gmc.rb

module GMC
    Color = {
        'white' => '#ffffff',
        'black' => '#000000',

        'grey50'  => '#fafafa',
        'grey100' => '#f5f5f5',
    }
end

autostart.rb

# background before wallpaper
system("xsetroot -solid '#{GMC::Color['blue500']}'")

View Source File:

Similar Code: [ BASH Color ] [ Perl Color ] [ Python Color ] [ Ruby Color ] [ PHP Color ] [ Lua Color ] [ Haskell Color ]


Hash: Config

The Hash in Config is very similar with the colors above. Except that it has string interpolation all over the place.

config.rb

module Config
    # Modifier variables
    s = 'Shift'
    c = 'Control'
    m = 'Mod4'
    a = 'Mod1'

    Keybinds = {
      # session
        "#{m}-#{s}-q" => 'quit',
        "#{m}-#{s}-r" => 'reload',
        "#{m}-#{s}-c" => 'close'
    }
end

This config will be utilized in main script as shown in the following code.

autostart.rb

helper::do_config("keybind",   %config::keybinds);
do_config("keybind",   Config::Keybinds)
do_config("keybind",   Config::Tagskeybinds)
do_config("mousebind", Config::Mousebinds)
do_config("attr",      Config::Attributes)
do_config("set",       Config::Sets)
do_config("rule",      Config::Rules)

View Source File:

Similar Code: [ BASH Config ] [ Perl Config ] [ Python Config ] [ Ruby Config ] [ PHP Config ] [ Lua Config ] [ Haskell Config ]


Processing The Hash Config

This is the heart of this script.

This do-config function has two arguments, the herbstclient command i.e “keybind”, and hash from config. I can see how simple and clean, Ruby is.

helper.rb

def do_config(command, hash)
    # loop over hash   
    hash.each do |key, value|
        hc(command+' '+key+' '+value)

        # uncomment to debug in terminal
        # puts(command+' '+key+' '+value)
    end
end

Debug Herbstclient Command

I do not remove line where I do debug when I made this script, so anyone can use it later, avoid examining blindly. Sometimes strange things happen. Just uncomment this line to see what happened.

        puts(command+' '+key+' '+value)

You can see the debugging result in figure below.

HerbstluftWM: Debug Command

View Source File:

Similar Code: [ BASH Helper ] [ Perl Helper ] [ Python Helper ] [ Ruby Helper ] [ PHP Helper ] [ Lua Helper ] [ Haskell Helper ]


Setting the Tags

Nothing special here, Ruby read all exported variable from modules. And I define local tag_names to avoid long namespace.

helper.rb

def set_tags_with_name()
    tag_names = Config::Tag_names
    tag_keys = Config::Tag_keys

    hc("rename default '#{tag_names[0]}' 2>/dev/null || true")
    
    tag_names.each_with_index do |tag_name, index|
        hc("add '#{tag_names[index]}'")
        
        key = tag_keys[index]                
        unless key.to_s.empty?
            hc("keybind Mod4-#{key} use_index '#{index}'");
            hc("keybind Mod4-Shift-#{key} move_index '#{index}'");
        end
    end
end

Launch the Panel

Two more functions left, it is do_panel and startup_run.

This two also be easy to do in Ruby. helper.rb

def do_panel()
    panel = __dir__ + "/../bash/dzen2/panel.sh"

    if not File.exist?(panel) and File.executable?(panel)
        panel = "/etc/xdg/herbstluftwm/panel.sh"
    end
   
    raw = IO.popen('herbstclient list_monitors | cut -d: -f1').read()
    monitors = raw.split("\n")

    for monitor in (monitors)
        system("#{panel} #{monitor} &");
    end
end

Run Baby Run

This is the last part. It is intended to be modified. Everyone has their own personal preferences.

startup.rb

def startup_run()
    command = 'silent new_attr bool my_not_first_autostart'
    system("herbstclient #{command}")
    exitcode = $?.exitstatus

    if (exitcode == 0)
      # non windowed app
        system("compton &")
        system("dunst &")
        system("parcellite &")
        system("nitrogen --restore &")
        system("mpd &")

      # windowed app
        system("xfce4-terminal &")
        system("sleep 1 && firefox &")
        system("sleep 2 && geany &")
        system("sleep 2 && thunar &")
    end
end

View Source File:

Similar Code: [ BASH Startup ] [ Perl Startup ] [ Python Startup ] [ Ruby Startup ] [ PHP Startup ] [ Lua Startup ] [ Haskell Startup ]


Putting It All Together.

The last part is going to main script and putting it all back together.

Now the flow is clear

Header Part: autostart.rb

require_relative 'assets/gmc'
require_relative 'helper'
require_relative 'config'
require_relative 'startup'

Procedural Part: autostart.rb

# background before wallpaper
system("xsetroot -solid '#{GMC::Color['blue500']}'")

# Read the manual in $ man herbstluftwm
hc('emit_hook reload')

# gap counter
system("echo 35 > /tmp/herbstluftwm-gap");

# do not repaint until unlock
hc("lock");

# standard
hc('keyunbind --all')
hc("mouseunbind --all")
hc("unrule -F")

set_tags_with_name()

# do hash config
do_config("keybind",   Config::Keybinds)
do_config("keybind",   Config::Tagskeybinds)
do_config("mousebind", Config::Mousebinds)
do_config("attr",      Config::Attributes)
do_config("set",       Config::Sets)
do_config("rule",      Config::Rules)

# unlock, just to be sure
hc("unlock")

# launch statusbar panel (e.g. dzen2 or lemonbar)
do_panel()

# load on startup
startup_run

View Source File:

Similar Code: [ BASH autostart ] [ Perl autostart ] [ Python autostart ] [ Ruby autostart ] [ PHP autostart ] [ Lua autostart ] [ Haskell autostart ]


Happy Configuring.