2

Dear all JLine users,

I am recently developing a console application where I use JLine, to provide command and file name completion.

It works pretty well with FileNameCompleter but however I cannot get the full file name right.

My code is like below:

        List<Completer> loadCompleter =
                Arrays.asList(
                    new StringsCompleter(commands),
                    new FileNameCompleter(),
                    new NullCompleter()
                );
        console.addCompleter(new ArgumentCompleter(loadCompleter));

        while ((line = console.readLine()) != null) {
            line = line.trim();
            // here I print out the line in char.
            char[] result = line.toCharArray();
            for (int i = 0; i < result.length; i ++) {
                System.out.println(result[i] + " : " + (int)result[i]);
            }
         }

In the last part of my code I am printing out the line I got from the console, if for example, I have received

myCommand test\new\test.txt

the output is myCommand testnewtest.txt

The backward slash is gone for some reason and I never got the right file path. This is not an issue when I am testing in Unix like system since forward slash seems ok.

Can anyone help me on the right way of getting the full filename? Many thanks.

Si.

zyzyis
  • 213
  • 3
  • 12
  • See https://www.student.cs.uwaterloo.ca/~cs132/Weekly/W03/FilePaths.html which puts an important note `A backslash — like is used in a file path — can't be used directly in a Java String. Java uses the backslash to mean “the next character means something special”. Doubling the backslash in Java indicates that “the next character (the second backslash) should be inserted into the String”.` – ecle Apr 25 '13 at 05:00
  • It is nothing wrong in your code...Only that the user needs to enter the filename with its path as "test\\new\\test.txt" in Java String instead for Java to interpret it correctly. It is clumbersome to the user, I guess. If so, you can replace any occurrence of "\" into "\\" from the user string input before printing it out. – ecle Apr 25 '13 at 05:08
  • You cannot get \ directly from Java input, because as mentioned in your previous comment, the \ is interpreted in something else, e.g. \n, \t. – zyzyis Apr 26 '13 at 08:01
  • Because the user gets the file name from FileCompleter, so I don't think they like to move back the cursor and add extra \, maybe I need to patch something in Jline, to adds extra \ in the file completer. – zyzyis Apr 26 '13 at 08:02
  • @zyzyis Did you solve the problem? – JohnWinter Jun 08 '15 at 21:06

2 Answers2

-1

JLine eats backslashes because they are used to escape special characters such as !. You can disable special characters (and the loss of backslashes) by adding the following into your ConsoleReader initialization:

console.setExpandEvents(false);

Alternatively, if you do want to retain special characters, you need to double up your backslashes (so instead of foo\bar, input foo\\bar).

-1

It works for me now, I am jline-2.11.jar.

See https://github.com/Qatar-Computing-Research-Institute/NADEEF/blob/master/console/src/qa/qcri/nadeef/console/Console.java

zyzyis
  • 213
  • 3
  • 12