Class: Uh::WM::RunControl

Inherits:
Object
  • Object
show all
Defined in:
lib/uh/wm/run_control.rb

Overview

Provides the context and behavior for run control file evaluation.

Constant Summary collapse

KEYSYM_TRANSLATIONS =

Key sym translations for key bindings.

{
  backspace:  :BackSpace,
  enter:      :Return,
  return:     :Return,
  tab:        :Tab
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ RunControl

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of RunControl.

Parameters:

  • env (Env)

    An environment



27
28
29
# File 'lib/uh/wm/run_control.rb', line 27

def initialize env
  @env = env
end

Class Method Details

.evaluate(env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Builds an instance and evaluates any run control file defined in the given Env instance

Parameters:

  • env (Env)

    An environment



18
19
20
21
22
# File 'lib/uh/wm/run_control.rb', line 18

def evaluate env
  rc_path = File.expand_path(env.rc_path)
  rc = new env
  rc.evaluate File.read(rc_path), rc_path if File.exist?(rc_path)
end

Instance Method Details

#evaluate(code, path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Evaluates run control code

Parameters:

  • code (String)

    The run control file content

  • path (String)

    The run control file path



35
36
37
38
39
# File 'lib/uh/wm/run_control.rb', line 35

def evaluate code, path
  instance_eval code, path
rescue ::StandardError, ::ScriptError => e
  raise RunControlEvaluationError, e.message, e.backtrace
end

#key(*keysyms, &block) ⇒ Object

Registers a key binding

Examples:

Output message on standard output when ‘modkey+f` is pressed

key(:f) { puts 'hello world!' }

Add ‘shift` key sym to the modifier mask

key(:q, :shift) { quit }

Convert capitalized key sym ‘Q` to `shift+q`

key(:Q) { quit }

Convert ‘enter` to `return` X key sym

key(:enter) { execute 'xterm' }

Parameters:

  • keysyms (Symbol)

    X key sym

  • block

    Code to execute when the key binding is triggered, with ‘self` as an ActionsHandler instance



53
54
55
# File 'lib/uh/wm/run_control.rb', line 53

def key *keysyms, &block
  @env.keybinds[translate_keysym *keysyms] = block
end

#launch(&block) ⇒ Object

Declares code to execute on window manager connection

Examples:

launch { execute 'xterm' }

Parameters:

  • block

    Code to execute when the window manager has connected, with ‘self` as a Launcher instance



62
63
64
# File 'lib/uh/wm/run_control.rb', line 62

def launch &block
  @env.launch = block
end

#layout(arg, options = {}) ⇒ Object

Defines the layout with either a layout class or an instance with optional layout options. When given only a hash, configures options for the default layout and ignores the ‘options` parameter.

Examples:

layout MyLayout
layout MyLayout, foo: :bar
layout MyLayout.new
layout MyLayout.new(foo: :bar)
layout foo: :bar

Parameters:

  • arg (Class, Object, Hash)

    A layout class, a layout instance, or options for the default layout

  • options (Hash) (defaults to: {})

    Layout options



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/uh/wm/run_control.rb', line 78

def layout arg, options = {}
  case arg
  when Class
    if options.any?
      @env.layout = arg.new options
    else
      @env.layout_class = arg
    end
  when Hash
    @env.layout_options = arg
  else
    @env.layout = arg
  end
end

#modifier(keysym, ignore: []) ⇒ Object

Defines the modifier masks to use for key bindings

Examples:

modifier :mod1 # Use `mod1' as modifier

Parameters:

  • keysym (Symbol)

    X key sym

Raises:



98
99
100
101
102
103
104
105
106
107
# File 'lib/uh/wm/run_control.rb', line 98

def modifier keysym, ignore: []
  [keysym, *ignore].each do |mod|
    unless KEY_MODIFIERS.keys.include? mod
      fail RunControlArgumentError,
        "invalid modifier keysym `#{mod.inspect}'"
    end
  end
  @env.modifier         = keysym
  @env.modifier_ignore  = [*ignore]
end

#rule(selectors = '', &block) ⇒ Object

Declares a client rule

Examples:

rule %w[firefox chrome] do
  log 'moving client to `www\' view!'
  layout_view_set 'www'
end

Parameters:

  • selectors (String, Array<String>) (defaults to: '')

    Substring matched against the beginning of clients window application name

  • block

    Code to execute when the client rule is matched



118
119
120
# File 'lib/uh/wm/run_control.rb', line 118

def rule selectors = '', &block
  [*selectors].each { |selector| @env.rules[/\A#{selector}/i] = block }
end

#worker(type, **options) ⇒ Object

Configures the worker

Examples:

Use the blocking worker

worker :block

Use the kqueue worker with 1 second timeout

worker :kqueue, timeout: 1

Use the multiplexing (‘select()`) worker with 1 second timeout

worker :mux, timeout: 1

Parameters:

  • type (Symbol)

    Worker type: ‘:block`, `:kqueue` or `:mux`

  • options (Hash)

    Worker options

Raises:



132
133
134
135
136
137
# File 'lib/uh/wm/run_control.rb', line 132

def worker type, **options
  unless Workers.type? type
    fail RunControlArgumentError, "invalid worker type `#{type}'"
  end
  @env.worker = [type, options]
end