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 |
# 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) end |
Instance Method Details
#run ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/mozconfig.rb', line 36 def run selection = TTY::Prompt.new.select("Pick a configuration", @configs) 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 |