make_menu 2.0.0 → 2.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/Gemfile.lock +1 -1
- data/lib/make_menu/badge_set.rb +14 -7
- data/lib/make_menu/builder.rb +1 -0
- data/lib/make_menu/console/prompter.rb +35 -7
- data/lib/make_menu/field_set.rb +39 -17
- data/lib/make_menu/text/column.rb +7 -5
- data/lib/make_menu/text/table.rb +1 -1
- data/lib/make_menu/version.rb +1 -1
- data/lib/make_menu.rb +36 -0
- data/make_menu.gemspec +1 -1
- 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: d77eaca0e51f7d0a83a9dfee76c802001897c54ea3de35bd01dbee07e36e4f04
|
4
|
+
data.tar.gz: 4b508767ebcdd7c6de85fc90e7d596a5b0f11293c1a108c83abd819f01cda9fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e0182748c51ea9417c65d14223ab0b558204877f2dd25cb78b9787310e8794ce972a6c04f1a4fb1aad4099fab6ca7a8dec0e4dbf8658c6113ceca972d122f9b
|
7
|
+
data.tar.gz: 8f0157fa975c994df129d9d55446c78331527d9aca1bfa3f543e99990c92186b909651f95334270ce2328585e2700d28da1988f8d41446073ce03599db17ab7b
|
data/Gemfile.lock
CHANGED
data/lib/make_menu/badge_set.rb
CHANGED
@@ -1,9 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module MakeMenu
|
4
|
+
# A set of BADGES to display above the menu
|
2
5
|
class BadgeSet
|
3
6
|
def initialize
|
4
7
|
@badges = []
|
5
8
|
end
|
6
9
|
|
10
|
+
# Add a new badge to the set
|
11
|
+
# @param [String] label Optional label to print to the left of the badge value
|
12
|
+
# @param [Proc] block Block to run each time the menu is re-drawn
|
13
|
+
def add(label = '', &block)
|
14
|
+
@badges << {
|
15
|
+
label: label,
|
16
|
+
handler: block
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
# Print badges in a wrapping horizontal row
|
7
21
|
def display
|
8
22
|
rows = []
|
9
23
|
row = ''
|
@@ -21,12 +35,5 @@ module MakeMenu
|
|
21
35
|
puts rows.join("\n\n").align_block(:center)
|
22
36
|
puts
|
23
37
|
end
|
24
|
-
|
25
|
-
def add(label = '', &block)
|
26
|
-
@badges << {
|
27
|
-
label: label,
|
28
|
-
handler: block
|
29
|
-
}
|
30
|
-
end
|
31
38
|
end
|
32
39
|
end
|
data/lib/make_menu/builder.rb
CHANGED
@@ -7,10 +7,29 @@ module MakeMenu
|
|
7
7
|
module Prompter
|
8
8
|
PressedEscape = Class.new(StandardError)
|
9
9
|
|
10
|
-
def self.
|
11
|
-
|
10
|
+
def self.prompt_and_save(text, file:, obscure: false)
|
11
|
+
if file.is_a? Symbol
|
12
|
+
file = ".#{file}"
|
13
|
+
end
|
14
|
+
|
15
|
+
current = File.exists?(file) ? File.read(file).strip : ''
|
16
|
+
|
17
|
+
response = prompt(text, input: current, obscure: obscure)
|
18
|
+
|
19
|
+
if response.empty?
|
20
|
+
File.delete(file) if File.exists?(file)
|
21
|
+
else
|
22
|
+
File.write(file, response)
|
23
|
+
end
|
24
|
+
|
25
|
+
return response
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.prompt(text = '', input: '', obscure: false, value_color: :light_yellow)
|
29
|
+
text = text.bold
|
30
|
+
|
31
|
+
print "\r#{text}#{input.color(value_color)}"
|
12
32
|
|
13
|
-
input = ''
|
14
33
|
char = ''
|
15
34
|
|
16
35
|
until !char.empty? && char.ord == 13
|
@@ -21,11 +40,17 @@ module MakeMenu
|
|
21
40
|
# BACKSPACE
|
22
41
|
input = input[0..-2]
|
23
42
|
print "\r#{text}#{' ' * input.size} "
|
24
|
-
print "\r#{text}#{obscure ? '*' * input.size : input}"
|
43
|
+
print "\r#{text}#{obscure ? '*'.color(value_color) * input.size : input.color(value_color)}"
|
25
44
|
|
26
45
|
when 27
|
27
46
|
# ESC
|
28
|
-
raise PressedEscape
|
47
|
+
raise PressedEscape if input.empty?
|
48
|
+
|
49
|
+
print "\r#{text}#{' ' * input.size} "
|
50
|
+
print "\r#{text}"
|
51
|
+
|
52
|
+
input = ''
|
53
|
+
char = ''
|
29
54
|
|
30
55
|
when 13
|
31
56
|
# ENTER
|
@@ -33,13 +58,16 @@ module MakeMenu
|
|
33
58
|
else
|
34
59
|
input += char
|
35
60
|
if obscure
|
36
|
-
print '*'
|
61
|
+
print '*'.color(value_color)
|
37
62
|
else
|
38
|
-
print char
|
63
|
+
print char.color(value_color)
|
39
64
|
end
|
40
65
|
end
|
41
66
|
end
|
42
67
|
|
68
|
+
print "\r#{text}#{' ' * input.size} "
|
69
|
+
print "\r#{text}"
|
70
|
+
|
43
71
|
input
|
44
72
|
end
|
45
73
|
end
|
data/lib/make_menu/field_set.rb
CHANGED
@@ -1,10 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module MakeMenu
|
4
|
+
# A set of FIELDS to display above the menu
|
2
5
|
class FieldSet
|
3
6
|
def initialize
|
4
7
|
@fields = []
|
5
8
|
@max_widths = {}
|
6
9
|
end
|
7
10
|
|
11
|
+
# Add a new field to the set
|
12
|
+
# @param [String] label Optional label to print to the left of the field value
|
13
|
+
# @param [String,Symbol] value_from_file If set, read the field value from a file
|
14
|
+
# in the current directory. A String is used literally, a Symbol is assumed
|
15
|
+
# to be a hidden file (i.e. starts with `.`)
|
16
|
+
# @param [String,Symbol,Integer] color Formatting to apply to the value
|
17
|
+
# @param [String] none Text to display if the file does not exist, or is empty
|
18
|
+
# @param [Proc] block Block to return the value to display
|
19
|
+
def add(label = '', value_from_file: nil, color: :normal, none: '[none]'.dark, &block)
|
20
|
+
if value_from_file
|
21
|
+
if value_from_file.is_a? Symbol
|
22
|
+
value_from_file = ".#{value_from_file}"
|
23
|
+
end
|
24
|
+
block = lambda do
|
25
|
+
if File.exists? value_from_file
|
26
|
+
value = File.read(value_from_file).strip
|
27
|
+
|
28
|
+
return none if value.empty?
|
29
|
+
|
30
|
+
return value.color(color)
|
31
|
+
else
|
32
|
+
return none
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
@fields << {
|
38
|
+
label: label,
|
39
|
+
handler: block
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
# Print fields in an aligned vertical stack
|
8
44
|
def display
|
9
45
|
build
|
10
46
|
@fields.each do |field|
|
@@ -26,6 +62,9 @@ module MakeMenu
|
|
26
62
|
puts
|
27
63
|
end
|
28
64
|
|
65
|
+
private
|
66
|
+
|
67
|
+
# Calculate maximum sizes of labels and rendered values
|
29
68
|
def build
|
30
69
|
@max_widths[:label] = 0
|
31
70
|
@max_widths[:value] = 0
|
@@ -39,22 +78,5 @@ module MakeMenu
|
|
39
78
|
@max_widths[:value] = value_width if value_width > @max_widths[:value]
|
40
79
|
end
|
41
80
|
end
|
42
|
-
|
43
|
-
def add(label = '', value_from_file: nil, color: :normal, none: '[none]'.dark, &block)
|
44
|
-
if value_from_file
|
45
|
-
block = lambda do
|
46
|
-
if File.exists? value_from_file
|
47
|
-
return File.read(value_from_file).strip.color(color)
|
48
|
-
else
|
49
|
-
return none
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
@fields << {
|
55
|
-
label: label,
|
56
|
-
handler: block
|
57
|
-
}
|
58
|
-
end
|
59
81
|
end
|
60
82
|
end
|
@@ -2,21 +2,23 @@
|
|
2
2
|
|
3
3
|
module MakeMenu
|
4
4
|
module Text
|
5
|
+
SPACING = 2
|
6
|
+
|
5
7
|
# A column of text with a fixed with
|
6
8
|
class Column
|
7
|
-
def initialize
|
8
|
-
@width =
|
9
|
+
def initialize
|
10
|
+
@width = nil
|
9
11
|
@rows = []
|
10
12
|
@row_index = 0
|
11
13
|
end
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
-
attr_accessor :rows, :row_index
|
15
|
+
attr_accessor :rows, :row_index, :width
|
16
16
|
|
17
17
|
# Add a block of text to the column. Each row will be padded to the column width
|
18
18
|
def add(text)
|
19
19
|
self.rows += text.split("\n").map do |row|
|
20
|
+
self.width = row.decolor.size + SPACING unless width
|
21
|
+
self.width = [width, row.decolor.size + SPACING].max
|
20
22
|
row.gsub("\r", '')
|
21
23
|
end
|
22
24
|
self.row_index += text.lines.size
|
data/lib/make_menu/text/table.rb
CHANGED
data/lib/make_menu/version.rb
CHANGED
data/lib/make_menu.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'make_menu/console/prompter'
|
3
4
|
require_relative 'make_menu/console/color_string'
|
4
5
|
require_relative 'make_menu/menu'
|
5
6
|
|
@@ -13,4 +14,39 @@ module MakeMenu
|
|
13
14
|
def self.run(makefile = './Makefile', &block)
|
14
15
|
MakeMenu::Menu.new(makefile).run(&block)
|
15
16
|
end
|
17
|
+
|
18
|
+
def self.prompt(text = nil, obscure: false, value_from_file: nil)
|
19
|
+
if (preamble = ARGV[0])
|
20
|
+
if text.nil?
|
21
|
+
if preamble.include?("\n")
|
22
|
+
parts = preamble.split("\n")
|
23
|
+
preamble = parts[0..-2].join("\n")
|
24
|
+
text = parts[-1]
|
25
|
+
puts preamble
|
26
|
+
else
|
27
|
+
text = preamble
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
if value_from_file
|
33
|
+
begin
|
34
|
+
input = Console::Prompter.prompt_and_save text, file: value_from_file, obscure: obscure
|
35
|
+
puts (obscure ? ('*' * input.decolor.size) : input).green.underline
|
36
|
+
|
37
|
+
rescue Console::Prompter::PressedEscape
|
38
|
+
puts '(not updated)'.red
|
39
|
+
end
|
40
|
+
else
|
41
|
+
begin
|
42
|
+
input = Console::Prompter.prompt text, obscure: obscure
|
43
|
+
puts (obscure ? ('*' * input.decolor.size) : input).green.underline
|
44
|
+
$stderr.puts input
|
45
|
+
rescue Console::Prompter::PressedEscape
|
46
|
+
puts '(not updated)'.red
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
input
|
51
|
+
end
|
16
52
|
end
|
data/make_menu.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: make_menu
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Barri Mason
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-02-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: tty-screen
|
@@ -45,7 +45,7 @@ files:
|
|
45
45
|
- lib/make_menu/text/table.rb
|
46
46
|
- lib/make_menu/version.rb
|
47
47
|
- make_menu.gemspec
|
48
|
-
homepage: https://github.com/
|
48
|
+
homepage: https://github.com/MixterGrimalkin/make_menu
|
49
49
|
licenses:
|
50
50
|
- MIT
|
51
51
|
metadata: {}
|