#-- # rbmh/prompt.rb : rbmhshow 0.4.2 # # Copyright (C) 2004--2005 Merlin Hughes # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # #++ # Prompting require 'rbmh/ansi' module RBMH class Prompter def initialize(context) @context = context @color_prompt = RBMH::ANSI.color(*(@context.profile['rbmh-color-prompt'] or '').split(' ')) @color_default = RBMH::ANSI.color(*(@context.profile['rbmh-color-default'] or '').split(' ')) @uncolor = RBMH::ANSI.uncolor end attr_reader :context def prompt(what, prefix, default, env) default = Prompter.deprefix(default, prefix) if default and default !~ TrailingQueryRE value = default else default = default ? default.chop : env && ENV[env] if default print "#{@color_prompt}#{what}?#{@uncolor} (#{@color_default}#{prefix}#{default}#{@uncolor}) #{prefix}" else print "#{@color_prompt}#{what}?#{@uncolor} #{prefix}" end begin value = STDIN.gets rescue Interrupt puts '^C' value = nil else if value.nil? puts '^D' else value.chomp! # `\n' if value.empty? value = default elsif env # I store in an environment variable, so if the user does # an exec command, the value will be remembered by children. ENV[env] = value = Prompter.deprefix(value, prefix) end end end end value end end def Prompter.deprefix(value, prefix) (value and (not prefix.empty?) and (value.index(prefix) == 0)) ? value[prefix.length..-1] : value end TrailingQueryRE = /\?$/ end # module RBMH