Skip to content

Commit

Permalink
Fix StandardRB issues
Browse files Browse the repository at this point in the history
  • Loading branch information
fractaledmind committed Aug 20, 2024
1 parent 4afcd8a commit 899f734
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 109 deletions.
4 changes: 2 additions & 2 deletions lib/prompts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
require_relative "prompts/form"

module Prompts
EMPTY = "".freeze
SPACE = " ".freeze
EMPTY = ""
SPACE = " "
MAX_WIDTH = 80
OUTPUT = $stdout

Expand Down
44 changes: 22 additions & 22 deletions lib/prompts/box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ module Prompts
class Box
include TextUtils

SOLID_BORDER = { top_left: "┌", top_right: "┐", bottom_left: "└", bottom_right: "┘", horizontal: "─", vertical: "│" }.freeze
DOUBLE_BORDER = { top_left: "╔", top_right: "╗", bottom_left: "╚", bottom_right: "╝", horizontal: "═", vertical: "║" }.freeze
HEAVY_BORDER = { top_left: "┏", top_right: "┓", bottom_left: "┗", bottom_right: "┛", horizontal: "━", vertical: "┃" }.freeze
ROUNDED_BORDER = { top_left: "╭", top_right: "╮", bottom_left: "╰", bottom_right: "╯", horizontal: "─", vertical: "│" }.freeze
SOLID_BORDER = {top_left: "┌", top_right: "┐", bottom_left: "└", bottom_right: "┘", horizontal: "─", vertical: "│"}.freeze
DOUBLE_BORDER = {top_left: "╔", top_right: "╗", bottom_left: "╚", bottom_right: "╝", horizontal: "═", vertical: "║"}.freeze
HEAVY_BORDER = {top_left: "┏", top_right: "┓", bottom_left: "┗", bottom_right: "┛", horizontal: "━", vertical: "┃"}.freeze
ROUNDED_BORDER = {top_left: "╭", top_right: "╮", bottom_left: "╰", bottom_right: "╯", horizontal: "─", vertical: "│"}.freeze

def initialize(width: MAX_WIDTH, padded: false, border_color: nil, border_style: :rounded)
@width = width
@padded = padded
@border_color = border_color
@line_padding = SPACE * 1
@border_parts = case border_style
when :solid then SOLID_BORDER
when :double then DOUBLE_BORDER
when :heavy then HEAVY_BORDER
else ROUNDED_BORDER
end
when :solid then SOLID_BORDER
when :double then DOUBLE_BORDER
when :heavy then HEAVY_BORDER
else ROUNDED_BORDER
end
@content = []
end

Expand Down Expand Up @@ -53,19 +53,19 @@ def lines

private

def top_border
border = @border_parts[:top_left] + @border_parts[:horizontal] * (@width - 2) + @border_parts[:top_right]
Fmt("#{@line_padding}%{border}#{@border_color}", border: border)
end
def top_border
border = @border_parts[:top_left] + @border_parts[:horizontal] * (@width - 2) + @border_parts[:top_right]
Fmt("#{@line_padding}%{border}#{@border_color}", border: border)
end

def bottom_border
border = @border_parts[:bottom_left] + @border_parts[:horizontal] * (@width - 2) + @border_parts[:bottom_right]
Fmt("#{@line_padding}%{border}#{@border_color}", border: border)
end
def bottom_border
border = @border_parts[:bottom_left] + @border_parts[:horizontal] * (@width - 2) + @border_parts[:bottom_right]
Fmt("#{@line_padding}%{border}#{@border_color}", border: border)
end

def align(text, alignment, between: @border_parts[:vertical])
formatted_boundary = Fmt("%{boundary}#{@border_color}", boundary: between)
wrap_text(text, width: @width, line_prefix: formatted_boundary + SPACE, line_suffix: SPACE + formatted_boundary, alignment: alignment)
end
def align(text, alignment, between: @border_parts[:vertical])
formatted_boundary = Fmt("%{boundary}#{@border_color}", boundary: between)
wrap_text(text, width: @width, line_prefix: formatted_boundary + SPACE, line_suffix: SPACE + formatted_boundary, alignment: alignment)
end
end
end
end
14 changes: 7 additions & 7 deletions lib/prompts/confirm_prompt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Prompts
class ConfirmPrompt < Prompt
def initialize(...)
super(...)
super

@prompt = if @default == false
"Choose [y/N]:"
Expand All @@ -20,12 +20,12 @@ def initialize(...)

private

def resolve_choice_from(response)
case response
when "y", "Y" then true
when "n", "N" then false
when "" then @default_boolean
end
def resolve_choice_from(response)
case response
when "y", "Y" then true
when "n", "N" then false
when "" then @default_boolean
end
end
end
end
38 changes: 19 additions & 19 deletions lib/prompts/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,28 @@ def prepend(*lines)

private

def clear_screen
jump_cursor_to_top
erase_down
end
def clear_screen
jump_cursor_to_top
erase_down
end

def render_frame
@frame_stack << @slots.dup
OUTPUT.puts SPACE
def render_frame
@frame_stack << @slots.dup
OUTPUT.puts SPACE

return if @slots.empty?
return if @slots.empty?

OUTPUT.puts @slots.join("\n")
OUTPUT.puts SPACE
@slots.clear
end
OUTPUT.puts @slots.join("\n")
OUTPUT.puts SPACE
@slots.clear
end

def jump_cursor_to_top
OUTPUT.print "\033[H"
end
def jump_cursor_to_top
OUTPUT.print "\033[H"
end

def erase_down
OUTPUT.print "\e[J"
end
def erase_down
OUTPUT.print "\e[J"
end
end
end
end
14 changes: 7 additions & 7 deletions lib/prompts/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Prompts
class Form
def initialize()
def initialize
@content = nil
@prompts = []
@results = []
Expand Down Expand Up @@ -51,10 +51,10 @@ def start

private

def prepend_form_content_to_prompt(prompt)
prompt.prepare_content
@content.gap
prompt.prepend_content(*@content.slots)
end
def prepend_form_content_to_prompt(prompt)
prompt.prepare_content
@content.gap
prompt.prepend_content(*@content.slots)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/prompts/paragraph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ def lines
wrap_text(@text, width: @width, line_prefix: @line_padding, alignment: :none)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/prompts/pause_prompt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Prompts
class PausePrompt < Prompt
def initialize(...)
super(...)
super

@prompt = "Press Enter ⏎ to continue..."
end
Expand Down
78 changes: 40 additions & 38 deletions lib/prompts/prompt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def content(&block)
@content
end

# standard:disable Style/TrivialAccessors
def label(label)
@label = label
end
Expand All @@ -44,6 +45,7 @@ def hint(hint)
def default(default)
@default = default
end
# standard:enable Style/TrivialAccessors

def ask
prepare_content if !@content_prepared
Expand All @@ -54,7 +56,7 @@ def ask
@content.render
*initial_prompt_lines, last_prompt_line = formatted_prompt
puts initial_prompt_lines.join("\n") if initial_prompt_lines.any?
response = Reline.readline(last_prompt_line, history = false).chomp
response = Reline.readline(last_prompt_line, _history = false).chomp
@choice = resolve_choice_from(response)

if (@error = ensure_validity(response))
Expand Down Expand Up @@ -87,53 +89,53 @@ def prepare_content

private

def prepare_default
Reline.pre_input_hook = -> do
Reline.insert_text @default.to_s
# Remove the hook right away.
Reline.pre_input_hook = nil
end
def prepare_default
Reline.pre_input_hook = -> do
Reline.insert_text @default.to_s
# Remove the hook right away.
Reline.pre_input_hook = nil
end
end

def prepare_validations
if @required
error_message = @required.is_a?(String) ? @required : "Value cannot be empty."
@validations << ->(input) { error_message if input.empty? }
end

if @validate
@validations << @validate
end
def prepare_validations
if @required
error_message = @required.is_a?(String) ? @required : "Value cannot be empty."
@validations << ->(input) { error_message if input.empty? }
end

def resolve_choice_from(response)
response
if @validate
@validations << @validate
end
end

def formatted_prompt
prompt_with_space = @prompt.end_with?(SPACE) ? @prompt : @prompt + SPACE
ansi_prompt = Fmt("%{prompt}faint|bold", prompt: prompt_with_space)
@formatted_prompt ||= Paragraph.new(ansi_prompt, width: MAX_WIDTH).lines
end
def resolve_choice_from(response)
response
end

def formatted_label
Fmt("%{label}cyan|bold %{instructions}faint|italic", label: @label, instructions: @instructions ? "(#{@instructions})" : "")
end
def formatted_prompt
prompt_with_space = @prompt.end_with?(SPACE) ? @prompt : @prompt + SPACE
ansi_prompt = Fmt("%{prompt}faint|bold", prompt: prompt_with_space)
@formatted_prompt ||= Paragraph.new(ansi_prompt, width: MAX_WIDTH).lines
end

def formatted_hint
Fmt("%{hint}faint|bold", hint: @hint)
end
def formatted_label
Fmt("%{label}cyan|bold %{instructions}faint|italic", label: @label, instructions: @instructions ? "(#{@instructions})" : "")
end

def formatted_error
Fmt("%{error}red|bold", error: @error + " Try again (×#{@attempts})...")
end
def formatted_hint
Fmt("%{hint}faint|bold", hint: @hint)
end

def ensure_validity(response)
@validations.each do |validation|
result = validation.call(response)
return result if result
end
nil
def formatted_error
Fmt("%{error}red|bold", error: @error + " Try again (×#{@attempts})...")
end

def ensure_validity(response)
@validations.each do |validation|
result = validation.call(response)
return result if result
end
nil
end
end
end
18 changes: 9 additions & 9 deletions lib/prompts/select_prompt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ def initialize(options: nil, **kwargs)
super(**kwargs)

@options = options.is_a?(Array) ? options.to_h { |item| [item, item] } : options
if (index = @options.keys.index(@default))
@default = index + 1
else
@default = nil
@default = if (index = @options.keys.index(@default))
index + 1
end
@instructions = "Enter the number of your choice"
@hint ||= "Type your response and press Enter ⏎"
@validations << ->(choice) { "Invalid choice." if !choice.to_i.between?(1, @options.size) }
end

# standard:disable Style/TrivialAccessors
def options(options)
@options = options
end
# standard:enable Style/TrivialAccessors

def prepare_content
super
Expand All @@ -36,10 +36,10 @@ def prepare_content

private

def resolve_choice_from(response)
choice = response.to_i
key, value = @options.to_a[choice - 1]
value
end
def resolve_choice_from(response)
choice = response.to_i
_key, value = @options.to_a[choice - 1]
value
end
end
end
2 changes: 1 addition & 1 deletion lib/prompts/text_prompt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Prompts
class TextPrompt < Prompt
def initialize(...)
super(...)
super

@instructions = "Press Enter to submit"
@hint ||= "Type your response and press Enter ⏎"
Expand Down
4 changes: 2 additions & 2 deletions lib/prompts/text_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

module Prompts
module TextUtils
ANSI_REGEX = /\e\[[0-9;]*[a-zA-Z]/.freeze
ANSI_REGEX = /\e\[[0-9;]*[a-zA-Z]/

def wrap_text(text, width:, line_prefix: EMPTY, line_suffix: EMPTY, alignment: :left)
words = text.scan(Regexp.union(/\S+/, ANSI_REGEX))
Expand Down Expand Up @@ -55,4 +55,4 @@ def strip_ansi(text)
text.gsub(ANSI_REGEX, EMPTY)
end
end
end
end

0 comments on commit 899f734

Please sign in to comment.