Ctrl-D on partial input not throwing EndOfFileException

16 views
Skip to first unread message

Derek Plautz

unread,
Aug 15, 2024, 3:38:13 PM8/15/24
to jline-users
Hello!

First time using JLine and it's awesome. However, running into a small issue (or expected behavior?) for a possible corner case. In a call to LineReader#readLine, if the user has anything typed and then inputs Ctrl-D, the expected EndOfFileException is seemingly not thrown and nothing happens. It seems that Ctrl-D is only handled when the input is empty.

I'm writing my application in Scala, but here's a minimal example of Java code that reproduces the issue:

import org.jline.reader.EndOfFileException;
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
import org.jline.reader.UserInterruptException;

import java.io.IOException;

public class Main {
  public static void main(String[] args) throws IOException {
    boolean running = true;
    LineReader reader = LineReaderBuilder.builder().build();

    while (running) {
      try {
        String line = reader.readLine(">> ");
        System.out.println("ECHO: " + line);
      } catch (UserInterruptException e) {
        System.out.println("^C, new prompt");
      } catch (EndOfFileException e) {
        System.out.println("^D, exiting");
        running = false;
      }
    }

    reader.getTerminal().close();
  }
}


If I run this and have typed anything after the prompt, in order to exit the application I need to clear my input and input Ctrl-D, or input Ctrl-C to get to a new prompt followed by a Ctrl-D on the new empty prompt.

For what it's worth, Ctrl-C does not behave the seem. Regardless of what has been typed after a prompt, Ctrl-C always raises a UserInterruptException. In my opinion, this seems like the correct behavior and Ctrl-D should behave similarly.

This isn't a major issue, but was hoping for some clarification/direction. Using JLine version 3.26.3.

Thanks!

-Derek

Archie Cobbs

unread,
Aug 15, 2024, 3:47:17 PM8/15/24
to jline...@googlegroups.com
This is not a jline specific problem, it's the way the shell behaves.

This bash script behaves the same way:

#!/bin/bash

while read -p '>> ' LINE; do
    echo "ECHO: ${LINE}"
done
echo "^D, exiting"

-Archie

--
You received this message because you are subscribed to the Google Groups "jline-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jline-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jline-users/96d28e92-3956-4d7e-9fc8-e7ffd1ca5dd1n%40googlegroups.com.


--
Archie L. Cobbs

Guillaume Nodet

unread,
Aug 15, 2024, 5:03:58 PM8/15/24
to jline...@googlegroups.com
By default, JLine uses zsh key bindings but It can be configured though. You need to change the keymap on the LineReader and bind Ctrl+D to a different action.

--

Derek Plautz

unread,
Aug 15, 2024, 5:59:12 PM8/15/24
to jline-users
Learn something everyday, thanks for this clarification!
Reply all
Reply to author
Forward
0 new messages