Skip to content

Expose readline.Interface "line", "cursor", "getCursorPos" #30347

Description

@Js-Brecht

Describe the solution you'd like
I think it would be very useful to be able to access readline.Interface's current line and cursor values, as well as access the _getCursorPos() member function.

It's frequently desirable to draw something outside of the current prompt during input. I am endlessly recreating actions that readline does already; specifically, tracking the cursor position (so listening for left, right, home, end, delete, backspace, ctrl+, etc, etc, ad nauseam), as well as listening to stdin for data, so that I can keep track of what the input looks like. This way, if I want to draw something outside of the prompt, I know where to return the cursor to.

I also see that readline uses Interface._getCursorPos() internally; also very useful, and pretty much what I'm recreating with Interface.line & Interface.cursor calculations.

Being able to read Interface.line at any given time can be important, if you need to use that value somewhere else, but you aren't done with the prompt yet (for example, drawing an autocomplete list)

But it seems awfully redundant to have to do all of this, when readline already does it quite well. Honestly, by the time I'm done, I've basically created my own readline interface. Seems like a waste.

I can read Interface.line, Interface.cursor, etc... but they aren't exposed in @types/node, so it makes me feel like they are omitted for a reason.

Describe alternatives you've considered
There's a couple of alternatives. One, I guess, is to just use the Interface members, and hope they don't ever change. The other is essentially creating my own custom readline interface, which really is just reinventing the wheel.


Perhaps somebody could share the philosophy behind the current design? On the other hand, if they can be safely exposed in the types, I'd be happy to make a PR.

Activity

  1. added
    readlineIssues and PRs related to the built-in readline module.
    on Nov 12, 2019
  2. bnoordhuis commented on Nov 12, 2019

    @bnoordhuis
    Member

    I can't speak for other collaborators but I'm okay with promoting _getCursorPos() to public API, i.e., by stripping the underscore. readline's implementation has been stable for a long time now and I don't expect that to change.

    The .line and .cursor properties have simply never been documented, I think.

    I suggest opening a pull request and see how it's received.

  3. cjihrig commented on Nov 12, 2019

    @cjihrig
    Contributor

    I'd be OK with that suggestion as well ^

  4. added a commit that references this issue on Nov 19, 2019
  5. Js-Brecht commented on Nov 20, 2019

    @Js-Brecht
    ContributorAuthor

    Opened a PR for @types/node. If I strip the underscore from _getCursorPos(), would that only make it into future releases of node? Or are patches still being released for v8+?

  6. cjihrig commented on Nov 20, 2019

    @cjihrig
    Contributor

    Opened a PR for @types/node.

    We should document them in this repo as well. Care to open a documentation PR?

    If I strip the underscore from _getCursorPos(), would that only make it into future releases of node? Or are patches still being released for v8+?

    I'd recommend stripping the underscore, but also creating an alias (_getCursorPos = getCursorPos). If you don't create the alias, it will be a breaking change and will have to wait for Node 14. If you add the alias, it can be a semver-minor change, allowing it to land in Node 13 and potentially as far back as Node 10. I think Node 8 is unlikely at this point, given that it is end-of-life next month.

  7. Js-Brecht commented on Nov 20, 2019

    @Js-Brecht
    ContributorAuthor

    If it's all the same to you, I think I'd rather just make an alias the other way around: getCursorPos = _getCursorPos.

    To me that makes more sense (even though they both effectively do the same thing); instead of changing every instance where _getCursorPos is used internally, it would just be exposing an internal member. Minimally invasive 😆.

    I will make both of these PRs when I have a few more minutes to spare.

  8. antsmartian commented on Nov 27, 2019

    @antsmartian
    Contributor

    +1 to move _getCursorPos to public API.

  9. Js-Brecht commented on Nov 27, 2019

    @Js-Brecht
    ContributorAuthor

    Hey guys, sorry it's taken me so long to get these PRs opened. Had a lot going on lately.

    PR #30667 is documentation for the line and cursor properties.
    Just created PR #30687 for _getCursorPos() -> getCursorPos().

  10. antsmartian commented on Jan 7, 2020

    @antsmartian
    Contributor

    Thanks @Js-Brecht , this is taken care in: #30667, #30687

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    readlineIssues and PRs related to the built-in readline module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions