Module: Wmiirc

Extended by:
Wmiirc
Included in:
Wmiirc, Sandbox
Defined in:
lib/wmiirc/handler.rb,
lib/wmiirc.rb,
lib/wmiirc/menu.rb,
lib/wmiirc/config.rb,
lib/wmiirc/system.rb,
lib/wmiirc/import.rb,
lib/wmiirc/loader.rb

Overview

DSL for wmiirc configuration.

Defined Under Namespace

Modules: Import, Loader Classes: Config, Handler, Sandbox

Constant Summary

DIR =

path to user's wmii configuration directory

File.dirname(File.dirname(__FILE__))
LOG =

keep a log file to aid the user in debugging

Logger.new(File.join(DIR, 'wmiirc.log'))
SANDBOX =
Sandbox.new
HISTORY_DIR =
File.join(DIR, 'history')
EVENTS =
Handler.new
ACTIONS =
Handler.new
KEYS =
Handler.new

Instance Method Summary (collapse)

Instance Method Details

- (Object) action(*a, &b)

If a block is given, registers a handler for the given action and returns the handler. Otherwise, executes all handlers for the given action.



65
66
67
# File 'lib/wmiirc/handler.rb', line 65

def action(*a, &b)
  ACTIONS.handle(*a, &b)
end

- (Boolean) action?(name)

Checks if a handler for the given action name has been registered.

Returns:

  • (Boolean)


72
73
74
# File 'lib/wmiirc/handler.rb', line 72

def action? name
  ACTIONS.key? name
end

- (Object) actions

Returns a list of registered action names.



79
80
81
# File 'lib/wmiirc/handler.rb', line 79

def actions
  ACTIONS.keys
end

- (Object) click_menu(choices, initial = nil)

Shows a menu (where the user must click a menu item using their mouse to make a choice) with the given items and returns the chosen item.

Parameters:

  • initial (defaults to: nil)

    The choice that should be initially selected.

    If this choice is not included in the list of choices, then this item will be made into a makeshift title-bar for the menu.

  • choices (Array)

    List of choices to display in the menu.

Returns:

  • nil if nothing was chosen.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/wmiirc/menu.rb', line 68

def click_menu choices, initial=nil
  command = ['wmii9menu']

  if initial
    command << '-i'

    unless choices.include? initial
      initial = "<<#{initial}>>:"
      command << initial
    end

    command << initial
  end

  command.concat choices

  choice = `#{command.shelljoin}`.chomp
  choice unless choice.empty?
end

- (Object) client_menu(*key_menu_args)

Shows a #key_menu containing all currently available clients and returns the chosen client.



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/wmiirc/menu.rb', line 93

def client_menu *key_menu_args
  clients = Rumai.clients

  choices = clients.map do |c|
    "#{c[:label].read.downcase} @#{c[:tags].read}"
  end

  if index = index_menu(choices, *key_menu_args)
    clients[index]
  end
end

- (Object) dialog(message, *arguments)

Shows a dialog box containing the given message.

This is a “fire and forget” operation. The result of the launched dialog box is NOT returned by this method!

Parameters:

  • message

    The message to be displayed.

  • arguments

    Additional command-line arguments for `xmessage`.



118
119
120
# File 'lib/wmiirc/system.rb', line 118

def dialog message, *arguments
  launch! 'xmessage', '-nearmouse', *arguments, message, type: 'DIALOG'
end

- (Object) event(*a, &b)

If a block is given, registers a handler for the given event and returns the handler. Otherwise, executes all handlers for the given event.



42
43
44
# File 'lib/wmiirc/handler.rb', line 42

def event(*a, &b)
  EVENTS.handle(*a, &b)
end

- (Boolean) event?(name)

Checks if a handler for the given event name has been registered.

Returns:

  • (Boolean)


49
50
51
# File 'lib/wmiirc/handler.rb', line 49

def event? name
  EVENTS.key? name
end

- (Object) events

Returns a list of registered event names.



56
57
58
# File 'lib/wmiirc/handler.rb', line 56

def events
  EVENTS.keys
end

- (Object) index_menu(choices, *key_menu_args)

Shows a #key_menu containing the given choices and returns the index of the chosen item.



110
111
112
113
114
115
# File 'lib/wmiirc/menu.rb', line 110

def index_menu choices, *key_menu_args
  choices = choices.each_with_index.map {|c,i| "#{c}\t#{i}" }
  if target = key_menu(choices, *key_menu_args)
    target[/\d+\z/].to_i
  end
end

- (Object) key(*a, &b)

If a block is given, registers a handler for the given keypress name and returns the handler. Otherwise, executes all handlers for the given keypress name.



89
90
91
# File 'lib/wmiirc/handler.rb', line 89

def key(*a, &b)
  KEYS.handle(*a, &b)
end

- (Boolean) key?(name)

Checks if a handler for the given keypress name has been registered.

Returns:

  • (Boolean)


96
97
98
# File 'lib/wmiirc/handler.rb', line 96

def key? name
  KEYS.key? name
end

- (Object) key_menu(choices, prompt, history_name = nil)

Shows a menu (where the user must press keys on their keyboard to make a choice) with the given items and returns the chosen item.

Parameters:

  • choices (Array)

    List of choices to display in the menu.

  • prompt (String)

    Instruction on what the user should enter or choose.

  • history_name (String) (defaults to: nil)

    Basename of the file in which the user's choices will be stored: the history file.

Returns:

  • nil if nothing was chosen.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/wmiirc/menu.rb', line 24

def key_menu choices, prompt, history_name=nil
  command = ['wimenu']
  command.push '-p', prompt.to_s if prompt

  if history_name
    history_file = File.join(HISTORY_DIR, history_name.to_s)
    if File.exist? history_file
      # show history *before* actual choices in menu
      history = File.readlines(history_file).map(&:chomp)
      choices = (history & choices).reverse.concat(choices).uniq
    end
  end

  IO.popen(command, 'r+') do |menu|
    menu.puts choices
    menu.close_write

    choice = menu.read
    unless choice.empty?
      if history_name
        # record choice in history for use next time
        File.open(history_file, 'a') {|f| f.puts choice }
      end
      choice
    end
  end
end

- (Object) keys

Returns a list of registered keypress names.



103
104
105
# File 'lib/wmiirc/handler.rb', line 103

def keys
  KEYS.keys
end

- (Object) launch(*args)

Runs #launch! inside the present working directory of the currently focused client or, if undeterminable, that of wmii.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wmiirc/system.rb', line 18

def launch *args
  if label = curr_client.label.read rescue nil
    label.encode(Encoding::UTF_8, undef: :replace, replace: '').
    split(/[\s\[\]\{\}\(\)<>"':]+/).reverse_each do |word|
      if File.exist? path = File.expand_path(word)
        path = File.dirname(path) unless File.directory? path
        Dir.chdir(path) { launch! *args }
        return
      end
    end
  end

  launch! *args
end

- (Object) launch!(command, *arguments_then_wihack_options)

Launches the given command in the background.

Examples

Launch a self-contained shell command (while making sure that the arguments within the shell command are properly quoted):

launch! "xmessage 'hello world' '#{Time.now}'"

Launch a command with explicit arguments (while not having to worry about shell-quoting those arguments):

launch! 'xmessage', 'hello world', Time.now.to_s

Launch a command on the floating layer (treating it as a dialog box) using the `wihack` program:

launch! 'xmessage', 'hello world', Time.now.to_s, type: 'DIALOG'

Parameters:

  • command (String)

    The name or path to the program you want to launch. This can be a self-contained shell command if no arguments are given.

  • arguments_then_wihack_options

    Command-line arguments for the command being launched, optionally followed by a Hash containing command-line option names and values for the `wihack` program.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/wmiirc/system.rb', line 63

def launch! command, *arguments_then_wihack_options
  *arguments, wihack_options = arguments_then_wihack_options

  unless wihack_options.nil? or wihack_options.kind_of? Hash
    arguments.push wihack_options
    wihack_options = nil
  end

  unless arguments.empty?
    command = [command, *arguments].shelljoin
  end

  if wihack_options
    wihack_argv = wihack_options.map {|k,v| ["-#{k}", v] }.flatten
    command = "wihack #{wihack_argv.shelljoin} #{command}"
  end

  spawn command
end

- (Object) notify(title, message, icon = 'dialog-information', *arguments)

Shows a notification with the given title and message.

This is a “fire and forget” operation. The result of the notification command is NOT returned by this method!

Parameters:

  • title

    The title to be displayed.

  • message

    The message to be displayed.

  • icon (defaults to: 'dialog-information')

    The icon to be displayed.

  • arguments

    Additional command-line arguments for `notify-send`.



101
102
103
104
# File 'lib/wmiirc/system.rb', line 101

def notify title, message, icon='dialog-information', *arguments
  Rumai.fs.event.write "Notice #{title}: #{message}\n"
  launch! 'notify-send', '-i', icon, title, message, *arguments
end