1 /*
2  * Copyright (c) 2002-2006, Marc Prud'hommeaux. All rights reserved.
3  *
4  * This software is distributable under the BSD license. See the terms of the
5  * BSD license in the documentation provided with this software.
6  */
7 package jline.example;
8 
9 import jline.*;
10 
11 import java.io.*;
12 import java.util.*;
13 import java.util.zip.*;
14 
15 public class Example {
usage()16     public static void usage() {
17         System.out.println("Usage: java " + Example.class.getName()
18                 + " [none/simple/files/dictionary [trigger mask]]");
19         System.out.println("  none - no completors");
20         System.out.println("  simple - a simple completor that comples "
21                 + "\"foo\", \"bar\", and \"baz\"");
22         System.out
23                 .println("  files - a completor that comples " + "file names");
24         System.out.println("  dictionary - a completor that comples "
25                 + "english dictionary words");
26         System.out.println("  classes - a completor that comples "
27                 + "java class names");
28         System.out
29                 .println("  trigger - a special word which causes it to assume "
30                         + "the next line is a password");
31         System.out.println("  mask - is the character to print in place of "
32                 + "the actual password character");
33         System.out.println("\n  E.g - java Example simple su '*'\n"
34                 + "will use the simple compleator with 'su' triggering\n"
35                 + "the use of '*' as a password mask.");
36     }
37 
main(String[] args)38     public static void main(String[] args) throws IOException {
39         Character mask = null;
40         String trigger = null;
41 
42         ConsoleReader reader = new ConsoleReader();
43         reader.setBellEnabled(false);
44         reader.setDebug(new PrintWriter(new FileWriter("writer.debug", true)));
45 
46         if ((args == null) || (args.length == 0)) {
47             usage();
48 
49             return;
50         }
51 
52         List completors = new LinkedList();
53 
54         if (args.length > 0) {
55             if (args[0].equals("none")) {
56             } else if (args[0].equals("files")) {
57                 completors.add(new FileNameCompletor());
58             } else if (args[0].equals("classes")) {
59                 completors.add(new ClassNameCompletor());
60             } else if (args[0].equals("dictionary")) {
61                 completors.add(new SimpleCompletor(new GZIPInputStream(
62                         Example.class.getResourceAsStream("english.gz"))));
63             } else if (args[0].equals("simple")) {
64                 completors.add(new SimpleCompletor(new String[] { "foo", "bar",
65                         "baz" }));
66             } else {
67                 usage();
68 
69                 return;
70             }
71         }
72 
73         if (args.length == 3) {
74             mask = new Character(args[2].charAt(0));
75             trigger = args[1];
76         }
77 
78         reader.addCompletor(new ArgumentCompletor(completors));
79 
80         String line;
81         PrintWriter out = new PrintWriter(System.out);
82 
83         while ((line = reader.readLine("prompt> ")) != null) {
84             out.println("======>\"" + line + "\"");
85             out.flush();
86 
87             // If we input the special word then we will mask
88             // the next line.
89             if ((trigger != null) && (line.compareTo(trigger) == 0)) {
90                 line = reader.readLine("password> ", mask);
91             }
92             if (line.equalsIgnoreCase("quit") || line.equalsIgnoreCase("exit")) {
93                 break;
94             }
95         }
96     }
97 }
98