Class: Uh::WM::ActionsHandler

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
EnvLogging
Defined in:
lib/uh/wm/actions_handler.rb

Overview

Provides a context with helper methods for key bindings (RunControl#key), client rules (RunControl#rule) and the Launcher (RunControl#launch).

Instance Method Summary collapse

Constructor Details

#initialize(env, events) ⇒ ActionsHandler

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 ActionsHandler.

Parameters:

  • env (Env)

    An environment

  • events (Dispatcher)

    A dispatcher



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

def initialize env, events
  @env    = env
  @events = events
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

Forwards unhandled messages prefixed with ‘layout_` to the layout, replacing the prefix with `handle_`

Examples:

layout_foo # delegates to `layout.handle_foo'


74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/uh/wm/actions_handler.rb', line 74

def method_missing m, *args, &block
  if respond_to? m
    meth = layout_method m
    log "#{layout.class.name}##{meth} #{args.inspect}"
    if layout.respond_to? meth
      layout.send meth, *args
    else
      log_error "Layout does not implement `#{meth}'"
    end
  else
    super
  end
end

Instance Method Details

#evaluate(code = nil, &block) ⇒ 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 action code given as normal argument or block parameter

Parameters:

  • code (Proc) (defaults to: nil)

    Action code

  • block (Proc)

    Action code



28
29
30
31
32
33
34
# File 'lib/uh/wm/actions_handler.rb', line 28

def evaluate code = nil, &block
  if code
    instance_exec &code
  else
    instance_exec &block
  end
end

#execute(command) ⇒ Object

Executes given command. Forks twice, creates a new session and makes the new process the session leader and process group leader of a new process group. The new process has no controlling terminal. Refer to ‘fork(2)` and `setsid(2)` for more detail.

‘command` argument is executed with `Kernel#exec`.

Parameters:

  • command (String, Array)

    Command to execute



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/uh/wm/actions_handler.rb', line 44

def execute command
  log "Execute: #{command}"
  pid = fork do
    fork do
      Process.setsid
      begin
        exec command
      rescue Errno::ENOENT => e
        log_error "ExecuteError: #{e}"
      end
    end
  end
  Process.waitpid pid
end

#kill_currentObject

Kills layout current client (focused) with Client#kill



60
61
62
63
# File 'lib/uh/wm/actions_handler.rb', line 60

def kill_current
  return unless layout.current_client
  layout.current_client.kill
end

#layoutObject

Returns the layout

Returns:

  • (Object)

    The layout

See Also:



14
# File 'lib/uh/wm/actions_handler.rb', line 14

def_delegator :@env, :layout

#log_separatorObject

Logs a separator string, can help during debug



66
67
68
# File 'lib/uh/wm/actions_handler.rb', line 66

def log_separator
  log '- ' * 24
end

#quitObject

Requests the window manager to terminate



89
90
91
92
# File 'lib/uh/wm/actions_handler.rb', line 89

def quit
  log 'Quit requested'
  @events.emit :quit
end

#respond_to_missing?(m, _) ⇒ Boolean

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.

Checks method existence in layout

Returns:

  • (Boolean)


96
97
98
# File 'lib/uh/wm/actions_handler.rb', line 96

def respond_to_missing? m, _
  m.to_s =~ /\Alayout_/ || super
end