sxmrb 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/Gemfile +2 -0
- data/README.md +29 -1
- data/lib/sxmrb.rb +100 -24
- data/lib/sxmrb/version.rb +2 -2
- data/sxmrb.gemspec +1 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 747dff11cd326291eea482413ce698ffb329503bb695bee27b44c3138432b338
|
4
|
+
data.tar.gz: d0b285f5e948e1569892e31752490690354ef0756910d055fd9b33155b298c63
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad9cdb4a7caff2b5488f8a73a2c5e8fca4e30bd8eee44de3be117a52844eafb04a78db194aad5fd93d3060a4afbb45a7d2f62bca27ec5e8900fa82d10cf05813
|
7
|
+
data.tar.gz: fe55d8de0f68cae3eab8dfb8a185091cfe0c955f4591f77f25ccde0e9df9d1fe1511b9ee188d11806fe9f11e58ccfed1b2772c3e10e9f7295edcbedd076fd071
|
data/.rubocop.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -31,6 +31,14 @@ end
|
|
31
31
|
|
32
32
|
## Usage
|
33
33
|
|
34
|
+
### Prompt for a value:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
value = Sxmrb.input(prompt: 'Amount?')
|
38
|
+
```
|
39
|
+
|
40
|
+
### Build a menu:
|
41
|
+
|
34
42
|
```ruby
|
35
43
|
def main_menu
|
36
44
|
Sxmrb.echo(<<~'END') | Sxmrb.menu(prompt: 'Ledger')
|
@@ -42,8 +50,28 @@ def main_menu
|
|
42
50
|
end
|
43
51
|
```
|
44
52
|
|
53
|
+
### Wrap a system command:
|
54
|
+
|
45
55
|
```ruby
|
46
|
-
|
56
|
+
def ledger(file: @file, command: nil, rest: nil)
|
57
|
+
Sxmrb.sh String.new('ledger').tap { |cmd|
|
58
|
+
cmd << " -f #{file.inspect}" if file
|
59
|
+
cmd << " #{command}" if command
|
60
|
+
cmd << " #{rest}" if rest
|
61
|
+
}
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
### Build a menu from a system command:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
def accounts(query: nil)
|
69
|
+
ledger(command: 'accounts', rest: query)
|
70
|
+
end
|
71
|
+
|
72
|
+
def input_account
|
73
|
+
(accounts | Sxmrb.menu(prompt: 'Account?')).to_s.chomp
|
74
|
+
end
|
47
75
|
```
|
48
76
|
|
49
77
|
## Development
|
data/lib/sxmrb.rb
CHANGED
@@ -5,36 +5,112 @@ require_relative 'sxmrb/version'
|
|
5
5
|
require 'shell'
|
6
6
|
require 'shellwords'
|
7
7
|
|
8
|
+
require 'singleton'
|
9
|
+
require 'forwardable'
|
10
|
+
|
8
11
|
# SXMO user scripts in ruby.
|
9
|
-
|
10
|
-
|
11
|
-
class Error < StandardError; end
|
12
|
+
class Sxmrb
|
13
|
+
include Singleton
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
# Normally when using Singleton there is no way to pass arguments
|
16
|
+
# when instantiating. The renew hack below allows the singleton
|
17
|
+
# instance to be re-instantiated and can pass arguments at that time;
|
18
|
+
# this was added as an experiment to assist with testing.
|
19
|
+
def initialize(shell: ::Shell.new) # :nodoc:
|
20
|
+
@_sh = shell
|
21
|
+
end
|
18
22
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
# Run a system shell command.
|
24
|
+
#
|
25
|
+
# @example
|
26
|
+
# sh('ls -tr').to_s.lines.last
|
27
|
+
def sh(...)
|
28
|
+
@_sh.system(...)
|
29
|
+
rescue ::StandardError => e
|
30
|
+
raise Error, e
|
31
|
+
end
|
27
32
|
|
28
|
-
|
29
|
-
|
30
|
-
|
33
|
+
# Display a menu using dmenu [X11] or vis-menu [ssh].
|
34
|
+
# A newline-separated list of items is read from standard input.
|
35
|
+
# The selected item is printed to standard output.
|
36
|
+
#
|
37
|
+
# @example
|
38
|
+
# file = (sh('ls') | menu(prompt: 'File?')).to_s.chomp
|
39
|
+
def menu(prompt: nil)
|
40
|
+
sh String.new.tap { |cmd|
|
41
|
+
if ENV['SSH_CLIENT'] || ENV['SSH_TTY']
|
42
|
+
cmd << 'vis-menu -i -l 18'
|
43
|
+
else
|
44
|
+
cmd << 'dmenu -c -i -l 18'
|
45
|
+
sh 'sxmo_keyboard.sh open'
|
46
|
+
end
|
31
47
|
|
32
|
-
|
33
|
-
|
34
|
-
|
48
|
+
cmd << " -p #{prompt.inspect}" if prompt
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
52
|
+
# Run the awk command.
|
53
|
+
#
|
54
|
+
# @example
|
55
|
+
# print(sh('ls -l') | awk('{print $1}').to_s)
|
56
|
+
def awk(arg = '')
|
57
|
+
sh("awk -e #{Shellwords.escape arg.chomp}")
|
58
|
+
end
|
59
|
+
|
60
|
+
# Run the cat command.
|
61
|
+
#
|
62
|
+
# @example
|
63
|
+
# print cat('/etc/passwd').to_s
|
64
|
+
def cat(arg = '')
|
65
|
+
sh("cat #{arg.chomp}")
|
66
|
+
end
|
35
67
|
|
36
|
-
|
37
|
-
|
68
|
+
# Run the column command.
|
69
|
+
#
|
70
|
+
# @example
|
71
|
+
# print(sh('ls -l') | column('-t').to_s)
|
72
|
+
def column(arg = '')
|
73
|
+
sh("column #{arg.chomp}")
|
74
|
+
end
|
75
|
+
|
76
|
+
# Run the shell echo command.
|
77
|
+
#
|
78
|
+
# @example
|
79
|
+
# print echo('Hello World').to_s
|
80
|
+
def echo(arg = '')
|
81
|
+
sh("echo #{Shellwords.escape arg.chomp}")
|
82
|
+
end
|
83
|
+
|
84
|
+
# Run the tac command.
|
85
|
+
#
|
86
|
+
# @example
|
87
|
+
# print tac('/etc/passwd').to_s
|
88
|
+
def tac(arg = '')
|
89
|
+
sh("tac #{arg.chomp}")
|
90
|
+
end
|
91
|
+
|
92
|
+
# Use a blank #menu to prompt for a value.
|
93
|
+
#
|
94
|
+
# @example
|
95
|
+
# value = input(prompt: 'Amount?').to_i
|
96
|
+
def input(prompt:)
|
97
|
+
(echo | menu(prompt: prompt)).to_s.chomp
|
98
|
+
end
|
99
|
+
|
100
|
+
class <<self
|
101
|
+
# This evil hack experiment is intended to allow swapping out the
|
102
|
+
# Shell instance when testing.
|
103
|
+
def renew_instance(...) # :nodoc:
|
104
|
+
@singleton__mutex__.synchronize {
|
105
|
+
@singleton__instance__ = new(...)
|
106
|
+
}
|
38
107
|
end
|
108
|
+
|
109
|
+
# Forward instance methods to the Singleton instance; false here
|
110
|
+
# excludes ancestor methods.
|
111
|
+
extend Forwardable
|
112
|
+
def_delegators :instance, *Sxmrb.instance_methods(false)
|
39
113
|
end
|
114
|
+
|
115
|
+
class Error < ::StandardError; end
|
40
116
|
end
|
data/lib/sxmrb/version.rb
CHANGED
data/sxmrb.gemspec
CHANGED
@@ -11,6 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
spec.summary = 'Ruby library for SXMO user scripts.'
|
12
12
|
spec.homepage = 'https://gitlab.com/fjc/sxmrb'
|
13
13
|
spec.license = 'MIT'
|
14
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
|
14
15
|
|
15
16
|
spec.metadata['homepage_uri'] = spec.homepage
|
16
17
|
spec.metadata['source_code_uri'] = spec.homepage
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sxmrb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Frank J. Cameron
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-05-
|
11
|
+
date: 2021-05-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: shell
|
@@ -73,7 +73,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
73
73
|
requirements:
|
74
74
|
- - ">="
|
75
75
|
- !ruby/object:Gem::Version
|
76
|
-
version:
|
76
|
+
version: 2.7.0
|
77
77
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
78
|
requirements:
|
79
79
|
- - ">="
|