JEG2 / highline

A higher level command-line oriented interface.
Other
1.29k stars 137 forks source link

Can't get terminal_size #239

Closed asfaltboy closed 1 year ago

asfaltboy commented 5 years ago

I tried bumping highline version from 1.6.2 to 2.0.1 in hubsmoke/bro/#75 but this caused me to spend an obscene amount of time trying to replace HighLine::SystemExtensions.terminal_size with HighLine.new.output_cols... It seems HighLine.new.terminal in latest version is mutilated on my environment (MacOS Mojave 10.14.3) and terminal is always Nil.

The interesting thing is that on 1.6.2 that previous call to terminal_size worked just fine...

abinoam commented 5 years ago

Thanks for reporting it @asfaltboy !

Could you please clone the repo and post here the results of bundle exec rake acceptance

abinoam commented 5 years ago

What is the output of

cli = HighLine.new
size = cli.terminal.terminal_size
asfaltboy commented 5 years ago
Output of `bundle exec rake acceptance` ``` ===== at_readline.rb ===== This step checks if the readline autocomplete feature is working. The test has 5 options you can choose from: save, sample, exec, exit and load. If you type the first character of one of them and then press the key you should see the options available for autocomplete. For example, if I type 's' and then I press I should see a list with 'save' and 'sample' as possible options for autocomplete. Although, if I type 'l' and then press the key it should be readly autcompleted as 'load', because 'load' is the only option that begins with the 'l' letter in this particular case. If I don't type any character but press two times, I should be able to see ALL available options. Please, play with Readline autocomplete for a while, pressing to see that it really gets the selected answer. When ready, just type 'exit' and the loop will finish. Don't forget to answer 'y' (yes) or 'n' (no) to the question at the end. --- Enter command: rake aborted! LoadError: dlopen(/Users/pavel.savchenko/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/x86_64-darwin17/readline.bundle, 9): Library not loaded: /usr/local/opt/readline/lib/libreadline.7.dylib Referenced from: /Users/pavel.savchenko/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/x86_64-darwin17/readline.bundle Reason: image not found - /Users/pavel.savchenko/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/x86_64-darwin17/readline.bundle /Users/pavel.savchenko/github/highline/lib/highline/terminal.rb:99:in `require' /Users/pavel.savchenko/github/highline/lib/highline/terminal.rb:99:in `get_line_with_readline' /Users/pavel.savchenko/github/highline/lib/highline/terminal.rb:88:in `get_line' /Users/pavel.savchenko/github/highline/lib/highline.rb:532:in `get_line' /Users/pavel.savchenko/github/highline/lib/highline.rb:516:in `get_response_line_mode' /Users/pavel.savchenko/github/highline/lib/highline/question.rb:524:in `get_response' /Users/pavel.savchenko/github/highline/lib/highline/question.rb:536:in `get_response_or_default' /Users/pavel.savchenko/github/highline/lib/highline/question_asker.rb:30:in `ask_once' /Users/pavel.savchenko/github/highline/lib/highline.rb:223:in `ask' /Users/pavel.savchenko/github/highline/test/acceptance/at_readline.rb:28:in `block (3 levels) in ' /Users/pavel.savchenko/github/highline/test/acceptance/at_readline.rb:26:in `loop' /Users/pavel.savchenko/github/highline/test/acceptance/at_readline.rb:26:in `block (2 levels) in ' /Users/pavel.savchenko/github/highline/test/acceptance/acceptance_test.rb:57:in `check' /Users/pavel.savchenko/github/highline/test/acceptance/acceptance_test.rb:19:in `check' /Users/pavel.savchenko/github/highline/test/acceptance/at_readline.rb:5:in `' test/acceptance/acceptance.rb:11:in `load' test/acceptance/acceptance.rb:11:in `block in ' test/acceptance/acceptance.rb:11:in `each' test/acceptance/acceptance.rb:11:in `' /Users/pavel.savchenko/github/highline/Rakefile:17:in `load' /Users/pavel.savchenko/github/highline/Rakefile:17:in `block in ' /Users/pavel.savchenko/.rvm/gems/ruby-2.4.1/gems/rake-12.3.2/exe/rake:27:in `' /Users/pavel.savchenko/.rvm/gems/ruby-2.4.1/bin/ruby_executable_hooks:15:in `eval' /Users/pavel.savchenko/.rvm/gems/ruby-2.4.1/bin/ruby_executable_hooks:15:in `
' Tasks: TOP => acceptance (See full trace by running task with --trace) ```

Looks like I had an issue with readline, if this is anything like Python, I don't trust MacOS upgrade to not break dependencies, so I'll go ahead and re-install ruby 2.4.1, and report back once done.

asfaltboy commented 5 years ago

Hmmm, that didn't work. Interestingly enough when running the command above in irb it works fine:

$ irb -rhighline                                                                                                                                           4346ms  Wed Feb 20 21:19:45 2019
irb(main):001:0> cli = HighLine.new
=> #<HighLine:0x007fa24502b9f0 @input=#<IO:<STDIN>>, @output=#<IO:<STDOUT>>, @multi_indent=true, @indent_size=3, @indent_level=0, @wrap_at=nil, @page_at=nil, @header=nil, @prompt=nil, @key=nil, @use_color=true, @track_eof=true, @terminal=#<HighLine::Terminal::IOConsole:0x007fa24502b680 @input=#<IO:<STDIN>>, @output=#<IO:<STDOUT>>>>
irb(main):002:0> size = cli.terminal.terminal_size

But when running the same from bro.rb I see that terminal_size is nil, here is the relevant code:

sep = ""
cli = HighLine.new
p cli
p cli.terminal
p cli.terminal.terminal_size
size = cli.terminal.terminal_size
p size
(size.first - 5).times { sep += "." }
sep += "\n"

And here is the result:

#<HighLine:0x007fd9bd0ad810 @input=#<IO:<STDIN>>, @output=#<IO:<STDOUT>>, @multi_indent=true, @indent_size=3, @indent_level=0, @wrap_at=nil, @page_at=nil, @header=nil, @prompt=nil, @key=nil, @use_color=true, @track_eof=true, @terminal=#
#<HighLine::Terminal::IOConsole:0x007fd9bd0ad220 @input=#<IO:<STDIN>>, @output=#<IO:<STDOUT>>>
nil
nil
error: undefined method `first' for nil:NilClass. Use --trace to view backtrace

I'm not really savvy in Ruby, so do please let me know what other info I should provide?

abinoam commented 5 years ago

Please, could you issue a puts HighLine::VERSION just to be sure your environment is set right. Perhaps your fireing different highline versions when switching from your script to irb. Just a guess.

abinoam commented 5 years ago

Were already able to run bundle exec rake acceptance.

I need that final report like this:

=== HighLine Acceptance Tests Report
Date: 2019-02-21 11:32:33 UTC
HighLine::VERSION: 2.0.1
Terminal: HighLine::Terminal::IOConsole
RUBY_DESCRIPTION: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]
Readline::VERSION: 7.0
ENV['SHELL']: /bin/bash
ENV['TERM']: xterm-256color
ENV['TERM_PROGRAM']: iTerm.app
asfaltboy commented 5 years ago

Sure

bundle exec bro gzip                                                                                                                                      259ms  Thu Feb 21 15:03:00 2019
        5 entries for gzip -- submit your own example with "bro add gzip"
2.0.1
#<HighLine:0x007fd1f80768c8 @input=#<IO:<STDIN>>, @output=#<IO:<STDOUT>>, @multi_indent=true, @indent_size=3, @indent_level=0, @wrap_at=nil, @page_at=nil, @header=nil, @prompt=nil, @key=nil, @use_color=true, @track_eof=true, @terminal=#
#<HighLine::Terminal::IOConsole:0x007fd1f80765d0 @input=#<IO:<STDIN>>, @output=#<IO:<STDOUT>>>
nil
nil
error: undefined method `first' for nil:NilClass. Use --trace to view backtrace

and

=== HighLine Acceptance Tests Report
Date: 2019-02-21 14:05:10 UTC
HighLine::VERSION: 2.0.1
Terminal: HighLine::Terminal::IOConsole
RUBY_DESCRIPTION: ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin18]
Readline::VERSION: 8.0
ENV['SHELL']: /usr/local/bin/fish
ENV['TERM']: screen-256color
ENV['TERM_PROGRAM']: iTerm.app
abinoam commented 5 years ago

Im so sorry I couldn't progress on this. Passing here just to let you know that I still care for your issue.

asfaltboy commented 5 years ago

No worries, let me know if you need more traces or debug logs, etc.

abinoam commented 5 years ago
  def output_cols
    return 80 unless @output.tty?
    terminal.terminal_size.first
  rescue NoMethodError
    return 80
  end

If terminal @output is not a tty? then terminal.terminal_size should not return nil.

I could just put a default fallback value for that (the same as I did for when it is not a tty?).

Could you please check these lines and copy past the results here?

p STDIN.tty? # should be true
p STDIN.terminal_size # should be an array with 2 values (lines and cols size)
abinoam commented 1 year ago

@asfaltboy I'm reviewing the issues list.

Could you confirm this issue still exists?

I don't have High Sierra so I'm not able to reproduce it.

I'll be closing this, but feel free to comment or reopen it if it's still happening.

Thanks for reporting.