Terminal.getline

Gets a line, including user editing. Convenience method around the LineGetter class and RealTimeConsoleInput facilities - use them if you need more control.

More...
  1. string getline(string prompt, dchar echoChar, string prefilledData)
    struct Terminal
    string
    getline
    (
    string prompt = null
    ,
    dchar echoChar = dchar.init
    ,
    string prefilledData = null
    )
  2. string getline(string prompt, string prefilledData, dchar echoChar)

Parameters

prompt string

the prompt to give the user. For example, "Your name: ".

echoChar dchar

the character to show back to the user as they type. The default value of dchar.init shows the user their own input back normally. Passing 0 here will disable echo entirely, like a Unix password prompt. Or you might also try '*' to do a password prompt that shows the number of characters input to the user.

prefilledData string

the initial data to populate the edit buffer

Detailed Description

You can set the lineGetter member directly if you want things like stored history.
Terminal terminal = Terminal(ConsoleOutputType.linear);
terminal.lineGetter = new LineGetter(&terminal, "my_history");

auto line = terminal.getline("$ ");
terminal.writeln(line);

You really shouldn't call this if stdin isn't actually a user-interactive terminal! However, if it isn't, it will simply read one line from the pipe without writing the prompt. See stdinIsTerminal.

Meta

History

The echoChar parameter was added on October 11, 2021 (dub v10.4).

The prompt would not take effect if it was null prior to November 12, 2021. Before then, a null prompt would just leave the previous prompt string in place on the object. After that, the prompt is always set to the argument, including turning it off if you pass null (which is the default).

Always pass a string if you want it to display a string.

The prefilledData (and overload with it as second param) was added on January 1, 2023 (dub v10.10 / v11.0).

On November 7, 2023 (dub v11.3), this function started returning stdin.readln in the event that the instance is not connected to a terminal.