Class: Sxmrb
- Inherits:
-
Object
- Object
- Sxmrb
- 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
-
.renew_instance ⇒ Object
This evil hack experiment is intended to allow swapping out the Shell instance when testing.
Instance Method Summary collapse
-
#initialize(shell: ::Shell.new) ⇒ Sxmrb
constructor
Normally when using Singleton there is no way to pass arguments when instantiating.
-
#input(prompt:) ⇒ Object
Use a blank #menu to prompt for a value.
-
#menu(prompt: nil, options: []) ⇒ Object
Display a menu using dmenu [X11] or vis-menu [ssh].
- #method_missing(cmd, *args, &_block) ⇒ Object
- #respond_to_missing?(cmd, include_private = false) ⇒ Boolean
-
#sh ⇒ Object
Run a system shell command.
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_instance ⇒ Object
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.
63 64 65 |
# File 'lib/sxmrb.rb', line 63 def input(prompt:) (echo | (prompt: prompt)).to_s.chomp end |
#menu(prompt: nil, options: []) ⇒ Object
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
51 52 53 54 55 56 |
# File 'lib/sxmrb.rb', line 51 def (prompt: nil, options: []) sh *(['sxmo_dmenu_with_kb.sh'].tap { |cmd| cmd.push * cmd.push '-p', prompt if prompt }) end |
#respond_to_missing?(cmd, include_private = false) ⇒ 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 |
#sh ⇒ Object
Run a system shell command.
38 39 40 41 42 |
# File 'lib/sxmrb.rb', line 38 def sh(...) @_sh.system(...) rescue ::StandardError => e raise Error, e end |