Class: Sxmrb

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/sxmrb.rb,
lib/sxmrb/version.rb

Overview

SXMO user scripts in ruby.

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
'0.2.1'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shell: ::Shell.new) ⇒ Sxmrb

Normally when using Singleton there is no way to pass arguments when instantiating. The renew hack below allows the singleton instance to be re-instantiated and can pass arguments at that time; this was added as an experiment to assist with testing.



30
31
32
# File 'lib/sxmrb.rb', line 30

def initialize(shell: ::Shell.new) # :nodoc:
  @_sh = shell
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(cmd, *args, &_block) ⇒ Object



67
68
69
# File 'lib/sxmrb.rb', line 67

def method_missing(cmd, *args, &_block)
  sh(*([cmd.name] + args))
end

Class Method Details

.renew_instanceObject

This evil hack experiment is intended to allow swapping out the Shell instance when testing.



78
79
80
81
82
# File 'lib/sxmrb.rb', line 78

def renew_instance(...) # :nodoc:
  @singleton__mutex__.synchronize {
    @singleton__instance__ = new(...)
  }
end

Instance Method Details

#input(prompt:) ⇒ Object

Use a blank #menu to prompt for a value.

Examples:

value = input(prompt: 'Amount?').to_i


63
64
65
# File 'lib/sxmrb.rb', line 63

def input(prompt:)
  (echo | menu(prompt: prompt)).to_s.chomp
end

Display a menu using dmenu [X11] or vis-menu [ssh]. A newline-separated list of items is read from standard input. The selected item is printed to standard output.

rubocop:disable Lint/AmbiguousOperator

Examples:

file = (sh('ls') | menu(prompt: 'File?')).to_s.chomp


51
52
53
54
55
56
# File 'lib/sxmrb.rb', line 51

def menu(prompt: nil, options: [])
  sh *(['sxmo_dmenu_with_kb.sh'].tap { |cmd|
    cmd.push *options
    cmd.push '-p', prompt if prompt
  })
end

#respond_to_missing?(cmd, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/sxmrb.rb', line 71

def respond_to_missing?(cmd, include_private = false)
  sh('which', cmd.name).empty? ? super : true
end

#shObject

Run a system shell command.

Examples:

sh('ls -tr').to_s.lines.last


38
39
40
41
42
# File 'lib/sxmrb.rb', line 38

def sh(...)
  @_sh.system(...)
rescue ::StandardError => e
  raise Error, e
end