make_menu 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b2d84b31eeff4e7646254d1ad56a798eba7bbdc7022273cac49166c1a9fd25f4
4
- data.tar.gz: c11ee1cc88e3ef8ee9b51fb2d0e1c3b7b2ef6da7f9194e00ee9ec4a3abf5a55c
3
+ metadata.gz: 8b6cd27b7d216d9aafab18ee1ada5756d4623a293bc659a6b16a01d71c95d180
4
+ data.tar.gz: 48f36e38f319a452675b3c45e8189e873d4679a71a8e8867684c8494cd850fff
5
5
  SHA512:
6
- metadata.gz: f0d3393504b31d23d6c788a300d646488e5d99ce8943d0afb69cf57a9a8b9ae594f6e7f199d869cd6d65399ca2d31c6ebffc94e2c568259fd9cc725cbe0f388c
7
- data.tar.gz: 0d3d990803202ba5523bfc311ff6acf000eeb7df582670752e311a70e9b3920e88e28f0d47ed6c50d366494a203eec8f67a502315cecea4f6923acef9b988725
6
+ metadata.gz: 3a3d2c60b794b93e27dfa7de7da5fd1bf75c5b385209b5c2ee571d881824cda532a36b6b2bcdb4dddb70684dcab426ea1a0c544ff724177ff3c0b3eee13bf45d
7
+ data.tar.gz: 8db18bd0530bc1118573efbe5d3df92663a8e52a9053d489118ec2ab3ceadc4f2e68427687809695de3ba0e6d41909c1f04f89feb675ca3acfae3afc96c1ca0e
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- make_menu (2.0.0)
4
+ make_menu (2.1.0)
5
5
  tty-screen (~> 0.8.2)
6
6
 
7
7
  GEM
@@ -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
@@ -2,6 +2,7 @@
2
2
 
3
3
  module MakeMenu
4
4
  module Builder
5
+ # Parse `makefile` and add all annotated targets to `menu`
5
6
  def self.build(makefile, menu)
6
7
  File.open(makefile, 'r') do |file|
7
8
  option_number = 1
@@ -7,10 +7,29 @@ module MakeMenu
7
7
  module Prompter
8
8
  PressedEscape = Class.new(StandardError)
9
9
 
10
- def self.prompt(text = '', obscure: false)
11
- print text
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
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MakeMenu
4
- VERSION = '2.0.0'
4
+ VERSION = '2.1.0'
5
5
  end
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
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
15
15
  'Gemfile.lock'
16
16
  ]
17
17
  s.homepage =
18
- 'https://github.com/MisterGrimalkin/make_menu'
18
+ 'https://github.com/MixterGrimalkin/make_menu'
19
19
  s.license = 'MIT'
20
20
  s.add_dependency 'tty-screen', '~> 0.8.2'
21
21
  s.description = ''
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.0.0
4
+ version: 2.1.0
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-01-10 00:00:00.000000000 Z
11
+ date: 2024-02-07 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/MisterGrimalkin/make_menu
48
+ homepage: https://github.com/MixterGrimalkin/make_menu
49
49
  licenses:
50
50
  - MIT
51
51
  metadata: {}