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 |
# 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 end |
Instance Method Details
#run ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/mozconfig.rb', line 39 def run selection = begin TTY::Prompt.new.select("Pick a configuration", @configs, default: @active) 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#{.map(&action).join}" end.join("\n\n") File.write("mozconfig", output) end |