From 86ac89fb616cea93c460f78f6997f6a5ff040e9c Mon Sep 17 00:00:00 2001
From: Thijs Schreijer Terminal
+
-
- CODEPAGE_UTF8
- UTF8 codepage.
-
-
- _readkey ()
- Reads a key from the console non-blocking.
-
-
- autotermrestore ()
- Backs up terminal settings and restores them on application exit.
-
-
detachfds ()
- Creates new file descriptions for
+ stdout and stderr.isatty (file)
+ Checks if a file-handle is a TTY.
-
+getconsolecp ()
- Gets the current console code page (Windows).
+ termsize ()
+ Get the size of the terminal in rows and columns.
Terminal_Windows
+
+
getconsoleflags (file)
Gets console flags (Windows).
-
getconsoleoutputcp ()
- Gets the current console output code page (Windows).
+ listconsoleflags (fh)
+ Debug function for console flags (Windows).
-
+getnonblock (fd)
- Gets non-blocking mode status for a file (Posix).
+ setconsoleflags (file, bitflags)
+ Sets the console flags (Windows).
Terminal_Posix
+
+
-
isatty (file)
- Checks if a file-handle is a TTY.
+ detachfds ()
+ Creates new file descriptions for
stdout and stderr.
-
listconsoleflags (fh)
- Debug function for console flags (Windows).
+ getnonblock (fd)
+ Gets non-blocking mode status for a file (Posix).
listtermflags (fh)
Debug function for terminal flags (Posix).
-
- readansi (timeout[, fsleep=system.sleep])
- Reads a single key, if it is the start of ansi escape sequence then it reads
- the full sequence.
-
-
readkey (timeout[, fsleep=system.sleep])
- Reads a single byte from the console, with a timeout.
+ setnonblock (fd, make_non_block)
+ Enables or disables non-blocking mode for a file (Posix).
-
setconsolecp (cp)
- Sets the current console code page (Windows).
+ tcgetattr (fd)
+ Get termios state (Posix).
-
+setconsoleflags (file, bitflags)
- Sets the console flags (Windows).
+ tcsetattr (fd, actions, termios)
+ Set termios state (Posix).
Terminal_Input
+
+
-
setconsoleoutputcp (cp)
- Sets the current console output code page (Windows).
+ _readkey ()
+ Reads a key from the console non-blocking.
-
setnonblock (fd, make_non_block)
- Enables or disables non-blocking mode for a file (Posix).
+ readansi (timeout[, fsleep=system.sleep])
+ Reads a single key, if it is the start of ansi escape sequence then it reads
+ the full sequence.
-
+tcgetattr (fd)
- Get termios state (Posix).
+ readkey (timeout[, fsleep=system.sleep])
+ Reads a single byte from the console, with a timeout.
Terminal_UTF-8
+
+
-
tcsetattr (fd, actions, termios)
- Set termios state (Posix).
+ CODEPAGE_UTF8
+ UTF8 codepage.
-
termbackup ()
- Returns a backup of terminal settings for stdin/out/err.
+ getconsolecp ()
+ Gets the current console code page (Windows).
-
termrestore (backup)
- Restores terminal settings from a backup
+ getconsoleoutputcp ()
+ Gets the current console output code page (Windows).
-
termsize ()
- Get the size of the terminal in rows and columns.
+ setconsolecp (cp)
+ Sets the current console code page (Windows).
-
termwrap (f)
- Wraps a function to automatically restore terminal settings upon returning.
+ setconsoleoutputcp (cp)
+ Sets the current console output code page (Windows).
utf8cwidth (utf8_char)
@@ -228,6 +229,25 @@
Sleep without a busy loop.
Terminal_Backup
+
+
+
+ autotermrestore ()
+ Backs up terminal settings and restores them on application exit.
+
+
+ termbackup ()
+ Returns a backup of terminal settings for stdin/out/err.
+
+
+ termrestore (backup)
+ Restores terminal settings from a backup
+
+
+termwrap (f)
+ Wraps a function to automatically restore terminal settings upon returning.
+
@@ -403,95 +423,66 @@ requested number of bytes, or an error, never a partial result.
Parameters:
-
-
-
-
-nil if no key was pressed.
On Posix, io.stdin must be set to non-blocking mode using setnonblock -and canonical mode must be turned off using tcsetattr, -before calling this function. Otherwise it will block. No conversions are -done on Posix, so the byte read is returned as-is.
- -On Windows this reads a wide character and converts it to UTF-8. Multi-byte -sequences will be buffered internally and returned one byte at a time. - - -
local system = require('system') +if system.isatty(io.stdin) then + -- enable ANSI coloring etc on Windows, does nothing in Posix. + local flags = system.getconsoleflags(io.stdout) + system.setconsoleflags(io.stdout, flags + sys.COF_VIRTUAL_TERMINAL_PROCESSING) +end+
stdout and stderr.
-Even if the file descriptors are unique, they still might point to the same
-file description, and hence share settings like O_NONBLOCK. This means that
-if one of them is set to non-blocking, the other will be as well. This can
-lead to unexpected behavior.
-
-This function is used to detach stdout and stderr from the original
-file descriptions, and create new file descriptions for them. This allows
-independent control of flags (e.g., O_NONBLOCK) on stdout and stderr,
-avoiding shared side effects.
Does not modify stdin (fd 0), and does nothing on Windows.
-
-
-
-
true on success, or throws an error on failure.
- true if set to non-blocking, false if not. Always returns false on Windows.
- local system = require('system') -if system.isatty(io.stdin) then - -- enable ANSI coloring etc on Windows, does nothing in Posix. - local flags = system.getconsoleflags(io.stdout) - system.setconsoleflags(io.stdout, flags + sys.COF_VIRTUAL_TERMINAL_PROCESSING) -end- -
CIF_ and COF_ constants are available on the module table. Where CIF are the
+input flags (for use with io.stdin) and COF are the output flags (for use with
+io.stdout/io.stderr).
- -- Print the flags for stdin/out/err -system.listconsoleflags(io.stdin) -system.listconsoleflags(io.stdout) -system.listconsoleflags(io.stderr)- +
To see flag status and constant names check listconsoleflags.
-fsleep to wait until either a key is
- available or the timeout is reached.
- It returns immediately if a key is available or if timeout is less than or equal to 0.
- In case of an ANSI sequence, it will return the full sequence as a string.
+Note: not all combinations of flags are allowed, as some are mutually exclusive or mutually required. +See setconsolemode documentation
"char" for a single key, "ansi" for an ANSI sequencetrue on success
"timeout" if the timeout was reached.local system = require('system') +system.listconsoleflags(io.stdout) -- List all the available flags and their current status + +local flags = system.getconsoleflags(io.stdout) +assert(system.setconsoleflags(io.stdout, + flags + system.COF_VIRTUAL_TERMINAL_PROCESSING) + +system.listconsoleflags(io.stdout) -- List again to check the differences+
fsleep to wait until either a byte is available or the timeout is reached.
- The sleep period is exponentially backing off, starting at 0.0125 seconds, with a maximum of 0.2 seconds.
- It returns immediately if a byte is available or if timeout is less than or equal to 0.
+ Creates new file descriptions for stdout and stderr.
+Even if the file descriptors are unique, they still might point to the same
+file description, and hence share settings like O_NONBLOCK. This means that
+if one of them is set to non-blocking, the other will be as well. This can
+lead to unexpected behavior.
-Using system.readansi is preferred over this function. Since this function can leave stray/invalid - byte-sequences in the input buffer, while system.readansi reads full ANSI and UTF8 sequences. +
This function is used to detach stdout and stderr from the original
+file descriptions, and create new file descriptions for them. This allows
+independent control of flags (e.g., O_NONBLOCK) on stdout and stderr,
+avoiding shared side effects.
Does not modify stdin (fd 0), and does nothing on Windows.
-
ok, err = fsleep(secs)
- (default system.sleep)
- true on success (always true on Posix systems)
- CIF_ and COF_ constants are available on the module table. Where CIF are the
-input flags (for use with io.stdin) and COF are the output flags (for use with
-io.stdout/io.stderr).
-
-To see flag status and constant names check listconsoleflags.
- -Note: not all combinations of flags are allowed, as some are mutually exclusive or mutually required. -See setconsolemode documentation + Gets non-blocking mode status for a file (Posix).
true on success
+ bool
+ true if set to non-blocking, false if not. Always returns false on Windows.
local system = require('system') -system.listconsoleflags(io.stdout) -- List all the available flags and their current status - -local flags = system.getconsoleflags(io.stdout) -assert(system.setconsoleflags(io.stdout, - flags + system.COF_VIRTUAL_TERMINAL_PROCESSING) - -system.listconsoleflags(io.stdout) -- List again to check the differences+ setnonblock +
true on success (always true on Posix systems)
- -- Print the flags for stdin/out/err +system.listconsoleflags(io.stdin) +system.listconsoleflags(io.stdout) +system.listconsoleflags(io.stderr)+
iflags, oflags, and lflags
if given will set the output flags
(optional)
- true, if successful. Always returns true on Windows.
+ local system = require('system') + +local status = assert(tcgetattr(io.stdin)) +if not status.lflag:has_all_of(system.L_ECHO) then + -- if echo is off, turn echoing newlines on + tcsetattr(io.stdin, system.TCSANOW, { lflag = status.lflag + system.L_ECHONL })) +end+ + + +
nil if no key was pressed.
+
+On Posix, io.stdin must be set to non-blocking mode using setnonblock +and canonical mode must be turned off using tcsetattr, +before calling this function. Otherwise it will block. No conversions are +done on Posix, so the byte read is returned as-is.
+ +On Windows this reads a wide character and converts it to UTF-8. Multi-byte +sequences will be buffered internally and returned one byte at a time. + + + +
fsleep to wait until either a key is
+ available or the timeout is reached.
+ It returns immediately if a key is available or if timeout is less than or equal to 0.
+ In case of an ANSI sequence, it will return the full sequence as a string.
+
+
+ "char" for a single key, "ansi" for an ANSI sequence"timeout" if the timeout was reached.fsleep to wait until either a byte is available or the timeout is reached.
+ The sleep period is exponentially backing off, starting at 0.0125 seconds, with a maximum of 0.2 seconds.
+ It returns immediately if a byte is available or if timeout is less than or equal to 0.
+
+Using system.readansi is preferred over this function. Since this function can leave stray/invalid + byte-sequences in the input buffer, while system.readansi reads full ANSI and UTF8 sequences. + + +
ok, err = fsleep(secs)
+ (default system.sleep)
true, if successful. Always returns true on Windows.
+ byte
+ the byte value that was read.
"timeout"), or if sleep failed.
local system = require('system') -local status = assert(tcgetattr(io.stdin)) -if not status.lflag:has_all_of(system.L_ECHO) then - -- if echo is off, turn echoing newlines on - tcsetattr(io.stdin, system.TCSANOW, { lflag = status.lflag + system.L_ECHONL })) -end+
iflags, oflags, and lflags
iflags, oflags, and lflags
true on success (always true on Posix systems)
iflags, oflags, and lflags
true on success (always true on Posix systems)