Files
chromebrew/lib/selector.rb
Satadru Pramanik, DO, MPH, MEng 43c334de5f Autosource ~/.bashrc (#9649)
* Autosource ~/.bashrc

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* Add exitmessage...

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* Use env variable to gate USR1

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* add logging

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* adjust logging, add more documentation

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* move env sourced variable in const.rb

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* Update flag variable to be CREW_SOURCE_BASHRC_ACTIVATED

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* Remove need for CREW_SOURCE_BASHRC_ACTIVATED env variable

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* fix logic for trap detection

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* revert to using env variable as flag for bash trap enablement

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* add reversion to env based crew_profile_base trap detection

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* move SOURCE_BASHRC code block

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* use fd instead of env variable, check for interactive terminal in selector

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* fix fd check

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* add exit to shell for installer so that bashrc does not have to be sourced manually after install is run.

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* do not use @pkg unless defined

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* Add some debug code to progress_bar

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* Add some debug code to progress_bar

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* Do not tread on #9654

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* Add some debug code to progress_bar

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* Add some debug code to progress_bar

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* Add some debug code to progress_bar

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* Add some debug code to progress_bar

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* Add some debug code to progress_bar

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* Add some debug code to progress_bar

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* Add some debug code to progress_bar

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* fixup selector and progress_bar

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* remove debug code from progress_bar

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* refactor to use changed bash process name

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* Adjust comment

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* disable more debug code

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* fix comparison

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* fix unit test

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* add some debugging to selector

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* move crewlog to separate file

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* use global verbose

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* adjust timeout to 1 for selector

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* "workaround" using a global variable

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* fix CREW_VERBOSE

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* Use CREW_VERBOSE

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* switch all @opt_verbose to CREW_VERBOSE

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* try to work around missing constant on update

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* add print_source_bashrc to ccache

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* uncommented crewlog lines in crew

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

* remove debug crewlog lines

Signed-off-by: Satadru Pramanik <satadru@gmail.com>

---------

Signed-off-by: Satadru Pramanik <satadru@gmail.com>
2024-04-15 21:28:42 -05:00

102 lines
2.8 KiB
Ruby

# selector.rb: Prompt user to choose an option in a list of options
# See packages/hunspell.rb for example usage
require_relative 'color'
require_relative 'crewlog'
require 'io/console'
class Selector
@@default_prompt = {
heading: 'There are %{total_opts} provider(s) for this package: ',
countdown: 'Default selected in %%2.2i second(s). Enter your choice [1 = %{default}]: '
}
def initialize(options, prompt = @@default_prompt, timeout = 10)
@options = options
# Set timeout to zero if a non-interactive console.
# Check noninteractive usage with `setsid command`.
@timeout = if !IO.console&.console_mode || IO.console&.winsize == [0, 0]
1
else
timeout
end
# substitute expressions in the message ("%{variable}")
@prompt = prompt.transform_values {|p| format(p, { total_opts: @options.size, default: @options[0][:value] }) }
end
def show_prompt
# show_prompt(): Show prompt based on given options
warn "#{@prompt[:heading]}\n\n"
@options.each_with_index do |opt, i|
$stderr.print " #{i + 1}: #{opt[:value]}"
$stderr.print " (#{opt[:description]})" if opt[:description]
$stderr.print "\n"
end
warn "\n"
# only show prompt when crew is running in an interactive terminal
if $stdin.isatty
begin
fire_timer { Thread.kill(@io_read) }
start_reading
@io_read.join
ensure
Thread.kill(@countdown)
end
end
if @io_read.nil? || @io_read[:input].to_s.chomp.empty?
# empty input or timeout
warn "Selected \"#{@options[0][:value]}\" by default.".yellow
choice = 1
elsif Integer(@io_read[:input], exception: false)&.between?(1, @options.size)
# when input is valid (is an integer and in range)
choice = @io_read[:input].to_i
else
# invalid input
warn <<~EOT.yellow
I don't understand "#{@io_read[:input]}". :(
Selected "#{@options[0][:value]}" by default.
EOT
choice = 1
end
# return result
return @options[choice - 1][:value]
end
private
def fire_timer(&when_timeout)
# fire_timer(): start a timer in separate thread
@countdown = Thread.new do
@timeout.downto(0).each do |remaining_time|
# print current countdown
$stderr.print "\r#{format(@prompt[:countdown], remaining_time)}"
if remaining_time.zero?
warn "\nTime expired.\n".yellow
when_timeout.call
else
sleep 1
end
end
end
end
def start_reading
# start_reading(): read from terminal in separate thread
@io_read = Thread.new do
# discard any input in the input buffer
$stdin.read_nonblock(1024)
rescue IO::WaitReadable
ensure
Thread.current[:input] = $stdin.getc
end
end
end