Sha256: 9dbe392ac52f9d0a07bfe67afa2156758088ac60ff5e044464c75ad4e7189106

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

require 'readline'

module Ryonan
  module Interactive
    class << self
      def print_template_list(filer)
        puts 'Template List'
        filer.templatable_dirs.map.with_index do |dir, index|
          puts "    [#{index}] : '#{dir}'"
        end
        puts ''
      end

      def select_template(filer)
        select = Readline.readline('Select Template[*] -> ')
        if select.match(/\d+/) && !filer.templatable_dirs[select.to_i].nil?
          filer.templatable_dirs[select.to_i]
        else
          puts 'The specified template does not exist'
          exit 1
        end
      end

      def read_template_name
        name = Readline.readline(<<~'EOS'.chomp + ' ')
            Input the destination directory name
            ( Deprecated: If the input is blank, to expand the template directory on one level up )
            ->
        EOS

        if name == Config.template_dir_name
          puts "#{Config.template_dir_name} will overlap with the template directory name"
          exit 1
        else
          name
        end
      end

      def read_configs(template_root)
        puts 'Set the value to the variable in the template'
        variables = File.open(config_file_path(template_root)).readlines.map(&:chomp)
        variables.map { |line| read_config(line) }.to_h
      end

      private

      def config_file_path(template_root)
        "#{template_root}/#{Config.template_dir_name}/#{Config.config_file_name}"
      end

      def read_config(line)
        if line =~ /.+=/
          (variable_name, default) = line.split('=')
          input = Readline.readline("    #{variable_name} (Default: #{default}) -> ")
          variable_value = input.empty? ? default : input
        else
          variable_name = line
          variable_value = Readline.readline("    #{variable_name} -> ")
        end
        [variable_name, variable_value]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ryonan-0.0.3 lib/ryonan/interactive.rb