From 7ebab0711106a76cace8b81e74f8c540a9a5dc60 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Mon, 24 Apr 2023 15:04:29 -0400 Subject: Use existing save_stty functionality Yes, `save_stty` and `restore_stty` should probably not live in terminal, but they do. No reason to re-implement them. And they handle error cases better. --- lib/highline/terminal/unix_stty.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/highline/terminal/unix_stty.rb b/lib/highline/terminal/unix_stty.rb index ad9c85d..1dbed36 100644 --- a/lib/highline/terminal/unix_stty.rb +++ b/lib/highline/terminal/unix_stty.rb @@ -32,13 +32,13 @@ class HighLine # (see Terminal#raw_no_echo_mode) def raw_no_echo_mode - @state = `stty -g` + save_stty system "stty raw -echo -icanon isig" end # (see Terminal#restore_mode) def restore_mode - system "stty #{@state}" + restore_stty print "\r" end -- cgit v1.2.1 From ec1885893dc2bb863b4858a110ad86abf7ee4612 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Mon, 24 Apr 2023 15:33:54 -0400 Subject: Do not call stty on non-tty When testing on a non-tty (e.g.: github actions), either one of these lines produces an error: ```ruby input.echo = true ask("question", "y") { |q| q.readline = true } ``` The error is produced by ruby internals: ```bash echo | rake | cat ......stty: stdin isn't a terminal...... ``` This change skips the stty calls if the input is not on a tty, so the errors will not be produced. --- lib/highline/terminal.rb | 2 +- lib/highline/terminal/unix_stty.rb | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/highline/terminal.rb b/lib/highline/terminal.rb index 550c0c7..bfa0d08 100644 --- a/lib/highline/terminal.rb +++ b/lib/highline/terminal.rb @@ -176,7 +176,7 @@ class HighLine # Saves terminal state using shell stty command. def save_stty @stty_save = begin - `stty -g`.chomp + `stty -g`.chomp if input.tty? rescue StandardError nil end diff --git a/lib/highline/terminal/unix_stty.rb b/lib/highline/terminal/unix_stty.rb index 1dbed36..df1460b 100644 --- a/lib/highline/terminal/unix_stty.rb +++ b/lib/highline/terminal/unix_stty.rb @@ -20,7 +20,9 @@ class HighLine rescue LoadError end - if /solaris/ =~ RUBY_PLATFORM && + if !@output.tty? + [80, 24] + elsif /solaris/ =~ RUBY_PLATFORM && `stty` =~ /\brows = (\d+).*\bcolumns = (\d+)/ [Regexp.last_match(2), Regexp.last_match(1)].map(&:to_i) elsif `stty size` =~ /^(\d+)\s(\d+)$/ @@ -33,7 +35,7 @@ class HighLine # (see Terminal#raw_no_echo_mode) def raw_no_echo_mode save_stty - system "stty raw -echo -icanon isig" + system "stty raw -echo -icanon isig" if input.tty? end # (see Terminal#restore_mode) -- cgit v1.2.1