make_menu 1.1.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: 220d243f1715ae665a12f8ed497f109209ba10fc5282298b7d9fec956ff99856
4
- data.tar.gz: dd1adb0c2bed5934598a72e2b3d5ffc4c48477b438b2a1c6206351d742868e9e
3
+ metadata.gz: 8b6cd27b7d216d9aafab18ee1ada5756d4623a293bc659a6b16a01d71c95d180
4
+ data.tar.gz: 48f36e38f319a452675b3c45e8189e873d4679a71a8e8867684c8494cd850fff
5
5
  SHA512:
6
- metadata.gz: e0f933ee0f3af0635c97dfc166e1c416784e0a5b24fac03cadab830179ed36fdce07711fc32e03ba353615de72128534b52d2cbf7b2e6009765043cb8bfb5d7e
7
- data.tar.gz: 87c98103a07b39f4d54e4278042b5f110754ccad21b299b073928945b142c507636e1685ef811f2f93f9f52309b710876d052f0c23c1e7b6ceddbd918f1b9df5
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 (1.1.0)
4
+ make_menu (2.1.0)
5
5
  tty-screen (~> 0.8.2)
6
6
 
7
7
  GEM
@@ -1,15 +1,29 @@
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 = ''
10
24
  @badges.each do |badge|
11
25
  label = badge[:label]
12
- value = badge[:handler].call ? badge[:on] : badge[:off]
26
+ value = badge[:handler].call
13
27
  if row.decolor.size + label.decolor.size + value.decolor.size >= (0.7 * ::TTY::Screen.cols)
14
28
  rows << row
15
29
  row = ''
@@ -21,14 +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 = '', on: ' ON '.green_bg.bold, off: ' OFF '.red_bg.dark, &block)
26
- @badges << {
27
- label: label,
28
- on: on,
29
- off: off,
30
- handler: block
31
- }
32
- end
33
38
  end
34
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|
@@ -12,20 +48,23 @@ module MakeMenu
12
48
  .align(
13
49
  :right,
14
50
  width: @max_widths[:label]
15
- ).color(field[:label_color])
51
+ )
16
52
 
17
53
  value_cel = field[:value]
18
54
  .align(
19
55
  :left,
20
56
  width: @max_widths[:value],
21
57
  pad_right: true
22
- ).color(field[:value_color])
58
+ )
23
59
 
24
60
  puts "#{label_cell}#{value_cel}".align(:center)
25
61
  end
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,14 +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 = nil, label_color: :normal, value_color: :normal, &block)
44
- @fields << {
45
- label: label || '',
46
- label_color: label_color,
47
- value_color: value_color,
48
- handler: block
49
- }
50
- end
51
81
  end
52
82
  end
@@ -20,7 +20,8 @@ module MakeMenu
20
20
  @options = {
21
21
  group_title_color: :underline,
22
22
  clear_screen: true,
23
- pause_on_success: false
23
+ pause_on_success: false,
24
+ badges_first: true
24
25
  }
25
26
  @highlights = {}
26
27
 
@@ -40,8 +41,14 @@ module MakeMenu
40
41
  system 'clear' if clear_screen?
41
42
 
42
43
  display_header
43
- display_badges
44
- display_fields
44
+
45
+ if badges_first?
46
+ display_badges
47
+ display_fields
48
+ else
49
+ display_fields
50
+ display_badges
51
+ end
45
52
 
46
53
  puts colorize(MakeMenu::Text::Table.new(groups).to_s)
47
54
  puts
@@ -93,8 +100,8 @@ module MakeMenu
93
100
  end
94
101
  end
95
102
 
96
- def add_field(field, &block)
97
- fields.add field, &block
103
+ def add_field(label = '', value_from_file: nil, color: :normal, none: '[none]'.dark, &block)
104
+ fields.add label, value_from_file: value_from_file, color: color, none: none, &block
98
105
  end
99
106
 
100
107
  def fields
@@ -105,8 +112,8 @@ module MakeMenu
105
112
  @field_set.display if @field_set
106
113
  end
107
114
 
108
- def add_badge(label = '', on: ' ON '.green_bg.bold, off: ' OFF '.red_bg.dark, &block)
109
- badges.add label, on: on, off: off, &block
115
+ def add_badge(label = '', &block)
116
+ badges.add label, &block
110
117
  end
111
118
 
112
119
  def badges
@@ -149,6 +156,10 @@ module MakeMenu
149
156
  options[:pause_on_success]
150
157
  end
151
158
 
159
+ def badges_first?
160
+ options[:badges_first]
161
+ end
162
+
152
163
  def colorize(text)
153
164
  highlights.each do |word, color|
154
165
  case color
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MakeMenu
4
- VERSION = '1.1.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: 1.1.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-09 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: {}