Sha256: 1ea3bba5108e36bfb4a48ae38753a0d684eab77cb34f9deb7d73af53a0f2eec2

Contents?: true

Size: 1.93 KB

Versions: 4

Compression:

Stored size: 1.93 KB

Contents

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
Hitcher-0.1.3 lib/hitcher/config.rb
Hitcher-0.1.2 lib/hitcher/config.rb
Hitcher-0.1.1 lib/hitcher/config.rb
Hitcher-0.1.0 lib/hitcher/config.rb