require 'yaml' require 'tty-prompt' require_relative 'terminal_utils' module Hitcher class Config extend Hitcher::TerminalUtils attr_accessor :terminal, :spec_filename def initialize @spec_filename = "cspec" end def save(path) File.open(path,"w") do |f| f.write YAML.dump(self) end end def self.load(path) File.open(path,"r") do |f| @cont = f.read end YAML.load(@cont) end def self.init_config tty = TTY::Prompt.new if not File.exist?(Hitcher::CONF_PATH) FileUtils.mkdir_p(File.dirname(Hitcher::CONF_PATH)) if not File.exist?(File.dirname(Hitcher::CONF_PATH)) c = Config.new found = detect_known_terminal if not found.empty? if found.length > 1 begin sel = tty.select("There are more than 1 terminal program found during config file initialization. Please select 1 for default:") do |m| found.each do |k,v| m.choice "#{k} - #{v}",v end m.choice "Quit",:quit end if sel != :quit c.terminal = sel else STDERR.puts "\nUser quit." exit(-1) end rescue TTY::Reader::InputInterrupt STDERR.puts "\n\nUser aborted" exit(-1) end else c.terminal = found.first.value end else tty.error "Hitcher requires configuration file with terminal program set. However there is none found from the system." tty.error "System shall proceed to create config file with default value at '#{Hitcher::CONF_PATH}'" e.terminal = "" end c.save(CONF_PATH) tty.ok "Config file successfully created!" c else Config.load(CONF_PATH) end end # init_config end end