001: /*
002: * Copyright (c) 2002-2006, Marc Prud'hommeaux. All rights reserved.
003: *
004: * This software is distributable under the BSD license. See the terms of the
005: * BSD license in the documentation provided with this software.
006: */
007: package jline.example;
008:
009: import jline.*;
010:
011: import java.io.*;
012: import java.util.*;
013: import java.util.zip.*;
014:
015: public class Example {
016: public static void usage() {
017: System.out.println("Usage: java " + Example.class.getName()
018: + " [none/simple/files/dictionary [trigger mask]]");
019: System.out.println(" none - no completors");
020: System.out
021: .println(" simple - a simple completor that comples "
022: + "\"foo\", \"bar\", and \"baz\"");
023: System.out.println(" files - a completor that comples "
024: + "file names");
025: System.out.println(" dictionary - a completor that comples "
026: + "english dictionary words");
027: System.out.println(" classes - a completor that comples "
028: + "java class names");
029: System.out
030: .println(" trigger - a special word which causes it to assume "
031: + "the next line is a password");
032: System.out
033: .println(" mask - is the character to print in place of "
034: + "the actual password character");
035: System.out
036: .println("\n E.g - java Example simple su '*'\n"
037: + "will use the simple compleator with 'su' triggering\n"
038: + "the use of '*' as a password mask.");
039: }
040:
041: public static void main(String[] args) throws IOException {
042: Character mask = null;
043: String trigger = null;
044:
045: ConsoleReader reader = new ConsoleReader();
046: reader.setBellEnabled(false);
047: reader.setDebug(new PrintWriter(new FileWriter("writer.debug",
048: true)));
049:
050: if ((args == null) || (args.length == 0)) {
051: usage();
052:
053: return;
054: }
055:
056: List completors = new LinkedList();
057:
058: if (args.length > 0) {
059: if (args[0].equals("none")) {
060: } else if (args[0].equals("files")) {
061: completors.add(new FileNameCompletor());
062: } else if (args[0].equals("classes")) {
063: completors.add(new ClassNameCompletor());
064: } else if (args[0].equals("dictionary")) {
065: completors.add(new SimpleCompletor(
066: new GZIPInputStream(Example.class
067: .getResourceAsStream("english.gz"))));
068: } else if (args[0].equals("simple")) {
069: completors.add(new SimpleCompletor(new String[] {
070: "foo", "bar", "baz" }));
071: } else {
072: usage();
073:
074: return;
075: }
076: }
077:
078: if (args.length == 3) {
079: mask = new Character(args[2].charAt(0));
080: trigger = args[1];
081: }
082:
083: reader.addCompletor(new ArgumentCompletor(completors));
084:
085: String line;
086: PrintWriter out = new PrintWriter(System.out);
087:
088: while ((line = reader.readLine("prompt> ")) != null) {
089: out.println("======>\"" + line + "\"");
090: out.flush();
091:
092: // If we input the special word then we will mask
093: // the next line.
094: if ((trigger != null) && (line.compareTo(trigger) == 0)) {
095: line = reader.readLine("password> ", mask);
096: }
097: if (line.equalsIgnoreCase("quit")
098: || line.equalsIgnoreCase("exit")) {
099: break;
100: }
101: }
102: }
103: }
|