Skip to content

Commit

Permalink
Merge pull request #42 from Shopify/ansi_fix
Browse files Browse the repository at this point in the history
Fix ansi problem
  • Loading branch information
Lisa Ugray authored Oct 12, 2018
2 parents c0e697f + 1d65e84 commit ba85dfa
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 59 deletions.
6 changes: 0 additions & 6 deletions lib/cli/ui/ansi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,6 @@ def self.previous_line
cursor_up + control('1', 'G')
end

# Move to the end of the line
#
def self.end_of_line
control("\033[", 'C')
end

def self.clear_to_end_of_line
control('', 'K')
end
Expand Down
4 changes: 1 addition & 3 deletions lib/cli/ui/progress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def self.progress(width: Terminal.width)
puts bar.to_s
CLI::UI.raw do
print(ANSI.show_cursor)
puts(ANSI.previous_line + ANSI.end_of_line)
end
end

Expand Down Expand Up @@ -67,8 +66,7 @@ def tick(percent: 0.01, set_percent: nil)
@percent_done = [@percent_done, 1.0].min # Make sure we can't go above 1.0

print to_s
print CLI::UI::ANSI.previous_line
print CLI::UI::ANSI.end_of_line + "\n"
print CLI::UI::ANSI.previous_line + "\n"
end

# Format the progress bar to be printed to terminal
Expand Down
10 changes: 5 additions & 5 deletions lib/cli/ui/prompt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ def ask_interactive(question, options = nil, multiple: false)
puts_question("#{question} {{yellow:(#{instructions})}}")
resp = interactive_prompt(options, multiple: multiple)

# Clear the line, and reset the question to include the answer
print(ANSI.previous_line + ANSI.end_of_line + ' ')
print(ANSI.cursor_save)
print(' ' * CLI::UI::Terminal.width)
print(ANSI.cursor_restore)
# Clear the line
print ANSI.previous_line + ANSI.clear_to_end_of_line
# Force StdoutRouter to prefix
print ANSI.previous_line + "\n"

# reset the question to include the answer
resp_text = resp
if multiple
resp_text = case resp.size
Expand Down
2 changes: 0 additions & 2 deletions lib/cli/ui/prompt/interactive_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def call
ensure
CLI::UI.raw do
print(ANSI.show_cursor)
puts(ANSI.previous_line + ANSI.end_of_line)
end
end

Expand All @@ -74,7 +73,6 @@ def reset_position
# When we redraw the options, they will be overwritten
CLI::UI.raw do
num_lines.times { print(ANSI.previous_line) }
print(ANSI.previous_line + ANSI.end_of_line + "\n")
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/cli/ui/progress_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def assert_bar(percent: nil, set_percent: nil, expected_filled: 0, expected_unfi
assert_equal expected_bar, bar.to_s
end

assert_equal expected_bar + "\e[1A\e[1G\e[\e[C\n", out
assert_equal expected_bar + "\e[1A\e[1G\n", out
end
end
end
Expand Down
63 changes: 21 additions & 42 deletions test/cli/ui/prompt_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_confirm_sigint
? q (Choose with ↑ ↓ ⏎)
\e[?25l> 1. yes\e[K
2. no\e[K
\e[?25h\e[\e[C
\e[?25h
EOF
assert_result(expected_out, "", :SIGINT)
end
Expand Down Expand Up @@ -76,7 +76,7 @@ def test_ask_interactive_sigint
? q (Choose with ↑ ↓ ⏎)
\e[?25l> 1. a\e[K
2. b\e[K
\e[?25h\e[\e[C
\e[?25h
EOF
assert_result(expected_out, "", :SIGINT)
end
Expand All @@ -87,12 +87,10 @@ def test_confirm_happy_path
? q (Choose with ↑ ↓ ⏎)
\e[?25l> 1. yes\e[K
2. no\e[K
\e[\e[C
#{' ' * CLI::UI::Terminal.width}
#{' ' * CLI::UI::Terminal.width}
\e[\e[C
\e[?25h\e[\e[C
\e[\e[C \e[s#{' ' * CLI::UI::Terminal.width}\e[u? q (You chose: yes)
\e[?25h\e[K
? q (You chose: yes)
EOF
assert_result(expected_out, "", true)
end
Expand All @@ -103,12 +101,10 @@ def test_confirm_invalid
? q (Choose with ↑ ↓ ⏎)
\e[?25l> 1. yes\e[K
2. no\e[K
\e[\e[C
#{' ' * CLI::UI::Terminal.width}
#{' ' * CLI::UI::Terminal.width}
\e[\e[C
\e[?25h\e[\e[C
\e[\e[C \e[s#{' ' * CLI::UI::Terminal.width}\e[u? q (You chose: yes)
\e[?25h\e[K
? q (You chose: yes)
EOF
assert_result(expected_out, "", true)
end
Expand All @@ -119,12 +115,10 @@ def test_confirm_no_match_internal
? q (Choose with ↑ ↓ ⏎)
\e[?25l> 1. yes\e[K
2. no\e[K
\e[\e[C
#{' ' * CLI::UI::Terminal.width}
#{' ' * CLI::UI::Terminal.width}
\e[\e[C
\e[?25h\e[\e[C
\e[\e[C \e[s#{' ' * CLI::UI::Terminal.width}\e[u? q (You chose: no)
\e[?25h\e[K
? q (You chose: no)
EOF
assert_result(expected_out, "", false)
end
Expand Down Expand Up @@ -219,12 +213,10 @@ def test_ask_interactive_with_block
? q (Choose with ↑ ↓ ⏎)
\e[?25l> 1. a\e[K
2. b\e[K
\e[\e[C
#{' ' * CLI::UI::Terminal.width}
#{' ' * CLI::UI::Terminal.width}
\e[\e[C
\e[?25h\e[\e[C
\e[\e[C \e[s#{' ' * CLI::UI::Terminal.width}\e[u? q (You chose: b)
\e[?25h\e[K
? q (You chose: b)
EOF
assert_result(expected_out, "", "b was selected")
end
Expand All @@ -237,12 +229,10 @@ def test_ask_interactive_with_number
? q (Choose with ↑ ↓ ⏎)
\e[?25l> 1. a\e[K
2. b\e[K
\e[\e[C
#{' ' * CLI::UI::Terminal.width}
#{' ' * CLI::UI::Terminal.width}
\e[\e[C
\e[?25h\e[\e[C
\e[\e[C \e[s#{' ' * CLI::UI::Terminal.width}\e[u? q (You chose: b)
\e[?25h\e[K
? q (You chose: b)
EOF
assert_result(expected_out, "", "b")
end
Expand All @@ -255,15 +245,12 @@ def test_ask_interactive_with_vim_bound_arrows
? q (Choose with ↑ ↓ ⏎)
\e[?25l> 1. a\e[K
2. b\e[K
\e[\e[C
1. a\e[K
> 2. b\e[K
\e[\e[C
#{' ' * CLI::UI::Terminal.width}
#{' ' * CLI::UI::Terminal.width}
\e[\e[C
\e[?25h\e[\e[C
\e[\e[C \e[s#{' ' * CLI::UI::Terminal.width}\e[u? q (You chose: b)
\e[?25h\e[K
? q (You chose: b)
EOF
assert_result(expected_out, "", "b")
end
Expand All @@ -276,12 +263,10 @@ def test_ask_interactive_select_using_space
? q (Choose with ↑ ↓ ⏎)
\e[?25l> 1. a\e[K
2. b\e[K
\e[\e[C
#{' ' * CLI::UI::Terminal.width}
#{' ' * CLI::UI::Terminal.width}
\e[\e[C
\e[?25h\e[\e[C
\e[\e[C \e[s#{' ' * CLI::UI::Terminal.width}\e[u? q (You chose: a)
\e[?25h\e[K
? q (You chose: a)
EOF
assert_result(expected_out, "", "a")
end
Expand All @@ -299,7 +284,7 @@ def test_ask_interactive_escape
? q (Choose with ↑ ↓ ⏎)
\e[?25l> 1. a\e[K
2. b\e[K
\e[?25h\e[\e[C
\e[?25h
EOF
assert_result(expected_out, nil, :SIGINT)
end
Expand All @@ -312,12 +297,10 @@ def test_ask_interactive_invalid_input
? q (Choose with ↑ ↓ ⏎)
\e[?25l> 1. a\e[K
2. b\e[K
\e[\e[C
#{' ' * CLI::UI::Terminal.width}
#{' ' * CLI::UI::Terminal.width}
\e[\e[C
\e[?25h\e[\e[C
\e[\e[C \e[s#{' ' * CLI::UI::Terminal.width}\e[u? q (You chose: b)
\e[?25h\e[K
? q (You chose: b)
EOF
assert_result(expected_out, "", "b")
end
Expand All @@ -334,18 +317,14 @@ def test_ask_interactive_with_blank_option
? q (Choose with ↑ ↓ ⏎)
\e[?25l> 1. a\e[K
2.#{blank}\e[K
\e[\e[C
1. a\e[K
> 2.#{blank}\e[K
\e[\e[C
> 1. a\e[K
2.#{blank}\e[K
\e[\e[C
#{' ' * CLI::UI::Terminal.width}
#{' ' * CLI::UI::Terminal.width}
\e[\e[C
\e[?25h\e[\e[C
\e[\e[C \e[s#{' ' * CLI::UI::Terminal.width}\e[u? q (You chose: a)
\e[?25h\e[K
? q (You chose: a)
EOF
assert_result(expected_out, "", "a was selected")
end
Expand Down

0 comments on commit ba85dfa

Please sign in to comment.