001/** 002 * RenameWand 2.2 003 * Copyright 2007 Zach Scrivena 004 * 2007-12-09 005 * zachscrivena@gmail.com 006 * http://renamewand.sourceforge.net/ 007 * 008 * RenameWand is a simple command-line utility for renaming files or 009 * directories using an intuitive but powerful syntax. 010 * 011 * TERMS AND CONDITIONS: 012 * This program is free software: you can redistribute it and/or modify 013 * it under the terms of the GNU General Public License as published by 014 * the Free Software Foundation, either version 3 of the License, or 015 * (at your option) any later version. 016 * 017 * This program is distributed in the hope that it will be useful, 018 * but WITHOUT ANY WARRANTY; without even the implied warranty of 019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 020 * GNU General Public License for more details. 021 * 022 * You should have received a copy of the GNU General Public License 023 * along with this program. If not, see <http://www.gnu.org/licenses/>. 024 */ 025 026package ca.bc.webarts.tools.renamewand; 027 028import java.util.Locale; 029import java.util.Scanner; 030 031/** 032 * Simple class for basic user input/output. 033 */ 034class UserIO 035{ 036 /** 037 * Prompt user for a single-character input. 038 * 039 * @param prompt 040 * Prompt string 041 * @param ops 042 * Options string containing permitted character responses (automatically converted 043 * to upper case) 044 * @return 045 * Character chosen by the user (automatically converted to upper case) 046 */ 047 static char userCharPrompt( 048 final String prompt, 049 final String ops) 050 { 051 /* case-insensitive comparison; convert everything to uppercase */ 052 final String options = ops.toUpperCase(Locale.ENGLISH); 053 054 final Scanner kb = new Scanner(System.in); 055 056 while (true) 057 { 058 RenameWand.stdout.print(prompt); 059 RenameWand.stdout.flush(); 060 061 final String response = kb.nextLine().trim(); 062 063 if (response.length() != 1) 064 continue; 065 066 /* convert to char */ 067 final char c = response.toUpperCase(Locale.ENGLISH).charAt(0); 068 069 if (options.indexOf(c) >= 0) 070 return c; 071 } 072 } 073 074 075 /** 076 * Print immediately to standard output. 077 * 078 * @param o 079 * Object to be printed 080 */ 081 static void debug_p( 082 final Object o) 083 { 084 RenameWand.stdout.print(o + ""); 085 RenameWand.stdout.flush(); 086 } 087}