terminal-basic-menu 1.0.0
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 +7 -0
- data/lib/terminal-basic-menu.rb +125 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 61f908608d54e5872d523f2d84d7151b4d1e793dc2b62fba259f7ae817211776
|
4
|
+
data.tar.gz: acfabc6a92621d9ec932f5b82eec106ed3d4426565d2ad48028a269537733ae4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 25efde4fca9a3c8a8ece2a36a2298f9637ae1f465c3ee5acbae28f9731b28f96fdf311ddb715f7b1306c524e37aa25de74368f614bc69214b03bda6c08f2f9ec
|
7
|
+
data.tar.gz: 0aed9b87dacf05a989d65cc1a6d1588fc199c10c1a164a29d60182f8182da098e08d0d76d71e500dfaff4284006001e1e3eb80cb8ab4197d3c56faf0970cbc79
|
@@ -0,0 +1,125 @@
|
|
1
|
+
require 'rainbow'
|
2
|
+
|
3
|
+
# Basic menu class used to create an instance of a terminal menu
|
4
|
+
class Menu
|
5
|
+
attr_accessor :width, :header, :body, :footer, :border_color, :word_wrap, :block_align
|
6
|
+
def initialize(width: 50, header: nil, body: nil, footer: nil, border_color: 0)
|
7
|
+
@width = width - 4 # adjust to allow '| ' and ' |' on each side
|
8
|
+
@header = header
|
9
|
+
@body = body
|
10
|
+
@footer = footer
|
11
|
+
@border_color = border_color
|
12
|
+
@word_wrap = true
|
13
|
+
@block_align = true
|
14
|
+
end
|
15
|
+
|
16
|
+
def display_menu
|
17
|
+
# Return if nothing to display
|
18
|
+
return unless @header || @body || @footer
|
19
|
+
# Change left to ljust etc.
|
20
|
+
align_to_method!
|
21
|
+
print_line_break
|
22
|
+
if @header
|
23
|
+
display_header
|
24
|
+
print_line_break
|
25
|
+
end
|
26
|
+
if @body
|
27
|
+
display_body
|
28
|
+
print_line_break
|
29
|
+
end
|
30
|
+
if @footer
|
31
|
+
display_footer
|
32
|
+
print_line_break
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def display_header
|
37
|
+
print_text(@header[:text], @header[:align], @header[:color])
|
38
|
+
end
|
39
|
+
|
40
|
+
def display_body
|
41
|
+
print_text(@body[:text], @body[:align], @body[:color])
|
42
|
+
print_choices(@body[:choices], @body[:choice_align], @body[:color])
|
43
|
+
end
|
44
|
+
|
45
|
+
def display_footer
|
46
|
+
print_text(@footer[:text], @footer[:align], @footer[:color])
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def print_text(text, align, color, choices: false)
|
52
|
+
color = 0 if color.nil?
|
53
|
+
# Default to center align
|
54
|
+
align = 'center' if align.nil?
|
55
|
+
# Split for normal text not for choices
|
56
|
+
lines = choices ? text.dup : text.split("\n")
|
57
|
+
# Wrap words or text that is too long
|
58
|
+
lines.map! { |line| word_wrap(line) } if @word_wrap
|
59
|
+
lines.flatten!
|
60
|
+
# Check for longest line to allow for grouped alignment if enabled
|
61
|
+
max_length = @block_align ? lines.map(&:length).max : 0
|
62
|
+
# Add extra length for choices '1) '
|
63
|
+
max_length += lines.count.to_s.length + 2 if choices
|
64
|
+
lines.each_with_index do |line, i|
|
65
|
+
line.prepend("#{i + 1}) ") if choices
|
66
|
+
output = Rainbow('| ').fg(border_color)
|
67
|
+
output += Rainbow(line.ljust(max_length).send(align, @width)).fg(color)
|
68
|
+
output += Rainbow(' |').fg(border_color)
|
69
|
+
puts output
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def print_choices(choices, align, color)
|
74
|
+
print_text(choices, align, color, choices: true) unless choices.nil?
|
75
|
+
end
|
76
|
+
|
77
|
+
def print_line_break
|
78
|
+
puts Rainbow('+' + '-' * (@width + 2) + '+').color(border_color)
|
79
|
+
end
|
80
|
+
|
81
|
+
def word_wrap(text)
|
82
|
+
lines = text.split("\n")
|
83
|
+
string = ''
|
84
|
+
lines.each do |line|
|
85
|
+
space_left = @width
|
86
|
+
line.split.each do |word|
|
87
|
+
if word.length + 1 > space_left
|
88
|
+
string += "\n"
|
89
|
+
if word.length > @width
|
90
|
+
# Use regex to split word with newline when reaching max width
|
91
|
+
word = word.scan(/.{1,#{@width}}/).join("\n")
|
92
|
+
end
|
93
|
+
string += word
|
94
|
+
space_left = @width - word.length
|
95
|
+
else
|
96
|
+
string += " #{word}"
|
97
|
+
space_left -= word.length + 1
|
98
|
+
end
|
99
|
+
end
|
100
|
+
string += "\n"
|
101
|
+
end
|
102
|
+
string.strip.split("\n")
|
103
|
+
end
|
104
|
+
|
105
|
+
def align_to_method!
|
106
|
+
@header[:align] = method_name(@header[:align]) if @header
|
107
|
+
if @body
|
108
|
+
@body[:align] = method_name(@body[:align])
|
109
|
+
@body[:choice_align] = method_name(@body[:choice_align])
|
110
|
+
end
|
111
|
+
@footer[:align] = method_name(@footer[:align]) if @footer
|
112
|
+
end
|
113
|
+
|
114
|
+
def method_name(input = nil)
|
115
|
+
return if input.nil?
|
116
|
+
case input
|
117
|
+
when 'left', 'ljust'
|
118
|
+
'ljust'
|
119
|
+
when 'right', 'rjust'
|
120
|
+
'rjust'
|
121
|
+
when 'center', 'middle'
|
122
|
+
'center'
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: terminal-basic-menu
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Armour
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-09-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rainbow
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 3.0.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.0.0
|
33
|
+
description: A simple gem used to create and display menus for terminal apps.
|
34
|
+
email: agrobmonkey@gmail.com
|
35
|
+
executables: []
|
36
|
+
extensions: []
|
37
|
+
extra_rdoc_files: []
|
38
|
+
files:
|
39
|
+
- lib/terminal-basic-menu.rb
|
40
|
+
homepage: https://github.com/dav-armour/terminal-basic-menu
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata: {}
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 2.7.7
|
61
|
+
signing_key:
|
62
|
+
specification_version: 4
|
63
|
+
summary: A basic menu for terminal apps
|
64
|
+
test_files: []
|