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.println("  simple - a simple completor that comples "
021                    + "\"foo\", \"bar\", and \"baz\"");
022            System.out
023                    .println("  files - a completor that comples " + "file names");
024            System.out.println("  dictionary - a completor that comples "
025                    + "english dictionary words");
026            System.out.println("  classes - a completor that comples "
027                    + "java class names");
028            System.out
029                    .println("  trigger - a special word which causes it to assume "
030                            + "the next line is a password");
031            System.out.println("  mask - is the character to print in place of "
032                    + "the actual password character");
033            System.out.println("\n  E.g - java Example simple su '*'\n"
034                    + "will use the simple compleator with 'su' triggering\n"
035                    + "the use of '*' as a password mask.");
036        }
037    
038        public static void main(String[] args) throws IOException {
039            Character mask = null;
040            String trigger = null;
041    
042            ConsoleReader reader = new ConsoleReader();
043            reader.setBellEnabled(false);
044            reader.setDebug(new PrintWriter(new FileWriter("writer.debug", true)));
045    
046            if ((args == null) || (args.length == 0)) {
047                usage();
048    
049                return;
050            }
051    
052            List completors = new LinkedList();
053    
054            if (args.length > 0) {
055                if (args[0].equals("none")) {
056                } else if (args[0].equals("files")) {
057                    completors.add(new FileNameCompletor());
058                } else if (args[0].equals("classes")) {
059                    completors.add(new ClassNameCompletor());
060                } else if (args[0].equals("dictionary")) {
061                    completors.add(new SimpleCompletor(new GZIPInputStream(
062                            Example.class.getResourceAsStream("english.gz"))));
063                } else if (args[0].equals("simple")) {
064                    completors.add(new SimpleCompletor(new String[] { "foo", "bar",
065                            "baz" }));
066                } else {
067                    usage();
068    
069                    return;
070                }
071            }
072    
073            if (args.length == 3) {
074                mask = new Character(args[2].charAt(0));
075                trigger = args[1];
076            }
077    
078            reader.addCompletor(new ArgumentCompletor(completors));
079    
080            String line;
081            PrintWriter out = new PrintWriter(System.out);
082    
083            while ((line = reader.readLine("prompt> ")) != null) {
084                out.println("======>\"" + line + "\"");
085                out.flush();
086    
087                // If we input the special word then we will mask
088                // the next line.
089                if ((trigger != null) && (line.compareTo(trigger) == 0)) {
090                    line = reader.readLine("password> ", mask);
091                }
092                if (line.equalsIgnoreCase("quit") || line.equalsIgnoreCase("exit")) {
093                    break;
094                }
095            }
096        }
097    }