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

feat(term): getting/setting console flags (windows) #21

Open
wants to merge 26 commits into
base: master
Choose a base branch
from

Conversation

Tieske
Copy link
Member

@Tieske Tieske commented Mar 23, 2024

adding some terminal functions to be able to better control terminal in- and output.

  • getconsoleflags and setconsoleflags for getting/setting the current console configuration flags on Windows
  • getconsolecp and setconsolecp for getting/setting the console codepage on Windows
  • getconsoleoutputcp and setconsoleoutputcp for getting/setting the console output codepage on Windows
  • tcgetattr and tcsetattr for getting/setting the current console configuration flags on Posix
  • getnonblock and setnonblock for getting/setting the non-blocking flag on Posix
  • bitflags: a support feature for the above flag type controls to facilitate bit manipulation without resorting to binary operations (to also support PuC Lua 5.1)
  • readkey reads a keyboard input from stdin in a non-blocking way (utf8, also on Windows)
  • readansi reads a keyboard input from stdin in a non-blocking way, parses ansi and utf8 sequences
  • termsize gets the current terminal size in rows and columns
  • utf8cwidth and utf8swidth for getting the display width (in columns) of respectively a single utf8 character, or a utf8 string
  • helpers; termbackup, termrestore, autotermrestore, and termwrap for managing the many terminal settings on all platforms.

Terminal behaviour is fundamentally different on Posix and Windows. So in pretty much all cases of using terminal in/output cross platform the user will still have to write platform specific code. But the above primitives at least allow to create CLI applications that behave similar on all platforms.

See doc_topics/03-terminal.md for an overview of functionalities.

This PR includes a number of examples:

  • using ANSI sequences to draw on screen
  • reading non-blocking input
  • displaying a progress type control (spinner)
  • reading a secret on the CLI (without echo it on screen)
  • reading a line of input in a non-blocking way

EDIT: adjusted description to match the latest code

@Tieske Tieske marked this pull request as ready for review April 30, 2024 07:28
@Tieske
Copy link
Member Author

Tieske commented Apr 30, 2024

@o-lim I still need to add more tests. But a first review would be most welcome.

src/bitflags.c Show resolved Hide resolved
src/bitflags.c Outdated Show resolved Hide resolved
src/bitflags.c Outdated Show resolved Hide resolved
spec/05-bitflags_spec.lua Outdated Show resolved Hide resolved
{NULL, NULL}
};

static const struct luaL_Reg lsbf_methods[] = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't all of this be implemented in lua rather than C? Is there something special about bitflags that can only be done in C?

Copy link
Member Author

Choose a reason for hiding this comment

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

Lua 5.1 doesn't have bitwise operators.

spec/04-term_spec.lua Outdated Show resolved Hide resolved
@Tieske Tieske force-pushed the terminal branch 2 times, most recently from 60a49e3 to 0e0e50c Compare May 20, 2024 12:16
@Tieske
Copy link
Member Author

Tieske commented May 23, 2024

@o-lim this has turned out to be a bit of a rabbit hole. But it is now covered I think, although a far larger contribution than anticipated.

Once keyboard input was added, it needed UTF-8 conversion and non-blocking mode, which then needed UTF-8 width again...

The most powerful example I think is the examples/readline.lua, which shows a non-blocking way of reading a line of input from the user, with UTF-8 support, in a platform independent way. Which is what I wanted to achieve, for use with coroutine schedulers running background tasks, while reading from the keyboard.

So apologies for the massive PR, hope you can find some time for a review.

The file doc_topics/03-terminal.md gives a pretty good overview.

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

Successfully merging this pull request may close these issues.

None yet

2 participants