Class: MozConfig::TUI
- Inherits:
-
Object
- Object
- MozConfig::TUI
- Defined in:
- lib/mozconfig.rb
Instance Method Summary collapse
-
#initialize ⇒ TUI
constructor
A new instance of TUI.
- #run ⇒ Object
Constructor Details
#initialize ⇒ TUI
Returns a new instance of TUI.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mozconfig.rb', line 12 def initialize unless File.exist?("mozconfig") $stderr.puts "Error: mozconfig not found." exit 1 end # Fix whitespace-only lines and split by paragraph, then each paragraph # into lines @mozconfig = File.read("mozconfig") .gsub(/^\s+$/, "") .split(/\n{2,}/) .map { |p| p.lines.map(&:strip_beginning) } # Only paragraphs that begin with a comment are recognized as configs; # anything else will remain active globally. configs, @globals = @mozconfig.partition { |p| p.first.commented? } # Form configs into a hash with the names as keys @configs = configs.to_h { |cfg| [cfg.first.uncomment.strip, cfg[1..]] } # Globals won't be manipulated further so we can rejoin them @globals.map!(&:join) # Find the active config @active = @configs.find { |_, | !.first.commented? }&.first @prompt = TTY::Prompt.new @prompt.on(:keypress) do |event| case event.value when "j" @prompt.trigger(:keydown) when "k" @prompt.trigger(:keyup) when "\e" # Escape $stderr.puts "\nExiting. Configuration was not changed." exit 0 end end end |
Instance Method Details
#run ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/mozconfig.rb', line 53 def run selection = begin @prompt.select( "Pick a configuration", @configs, default: @active, cycle: true ) rescue TTY::Reader::InputInterrupt $stderr.puts "\nProcess interrupted. Closing." exit 130 end output = "#{@globals.join("\n\n")}\n\n" output << @configs.map do |name, | action = == selection ? :uncomment : :comment "#{name.comment}\n#{options.map(&action).join}" end.join("\n\n") File.write("mozconfig", output) end |