Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getCursorPos() to std/terminal #22749

Merged
merged 5 commits into from
Oct 8, 2023
Merged

Conversation

MatrixManAtYrService
Copy link
Contributor

@MatrixManAtYrService MatrixManAtYrService commented Sep 23, 2023

This would be handy for making terminal apps which display content below the prompt (e.g. fzf does this).

Need to test it on windows before I remove "draft" status.

@MatrixManAtYrService MatrixManAtYrService changed the title Add getCursorPos() to std/terminal (non-windows) Add getCursorPos() to std/terminal Sep 23, 2023
lib/pure/terminal.nim Outdated Show resolved Hide resolved
@Araq Araq marked this pull request as ready for review September 26, 2023 14:44
var oldMode: Termios
discard fd.tcGetAttr(addr oldMode)
fd.setRaw()
stdout.write(getPos)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation comment should mention that internally it writes to stdout IMHO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oof, I just realized that it doesn't behave very well if there's not a terminal in the loop (writes the request to stdout and blocks while waiting for something from stdin). It might do to warn users about that. Added a comment.

lib/pure/terminal.nim Outdated Show resolved Hide resolved
@Araq Araq added the merge_when_passes_CI mergeable once green label Sep 30, 2023
@MatrixManAtYrService
Copy link
Contributor Author

MatrixManAtYrService commented Oct 7, 2023

From the failing CI:

D:\a\Nim\Nim\lib\pure\terminal.nim(226, 24) Error: getCursorPos(getCurrentProcess()) can raise an unlisted exception: ref OSError
FAILURE
Error: Process completed with exit code 1.

Super sophisticated test program:

# test.nim
import terminal
echo getCursorPos()

Placing it in the same dir as terminal.nim and running nim c test.nim replicates the error, and it goes away after adding OSError to the list. The test program fails, however:

PS C:\Users\matt\src\Nim\lib\pure> .\test.exe
C:\Users\matt\src\Nim\lib\pure\test.nim(2) test
C:\Users\matt\src\Nim\lib\pure\terminal.nim(225) getCursorPos
C:\Users\matt\src\Nim\lib\pure\terminal.nim(221) getCursorPos
C:\Users\matt\.choosenim\toolchains\nim-2.0.0\lib\std\oserrors.nim(92) raiseOSError
Error: unhandled exception: The handle is invalid.
 [OSError]

Fixed it like so:

  proc getCursorPos*(): tuple [x, y: int] {.raises: [ValueError, IOError, OSError].} =
    return getCursorPos(getStdHandle(STD_OUTPUT_HANDLE))

Tested on windows, works ok.

@Araq Araq merged commit 5bcea05 into nim-lang:devel Oct 8, 2023
19 checks passed
@github-actions
Copy link
Contributor

github-actions bot commented Oct 8, 2023

Thanks for your hard work on this PR!
The lines below are statistics of the Nim compiler built from 5bcea05

Hint: mm: orc; opt: speed; options: -d:release
170201 lines; 10.351s; 620.078MiB peakmem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge_when_passes_CI mergeable once green
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants