summaryrefslogtreecommitdiff
Commit message (Collapse)AuthorAgeFilesLines
* meson.build: prepare for gentoo-functions-1.1HEADgentoo-functions-1.1masterSam James4 hours1-1/+1
| | | | Signed-off-by: Sam James <sam@gentoo.org>
* meson.build: fix testsSam James4 hours1-1/+1
| | | | | Fixes: ea4fb2a1e34900635d0db7bd1d8e5e1bea06ced0 Signed-off-by: Sam James <sam@gentoo.org>
* Drop ecma48-cprSam James4 hours2-246/+0
| | | | | | | Obsolete as of bfd5710de9b14712c4a621259b951bd10489d0c0. A neat tool, but it ended up not being a good fit for the reasons described in that commit. Signed-off-by: Sam James <sam@gentoo.org>
* Cleanup functions.sh.in templateSam James4 hours2-12/+2
| | | | | | No longer needed since bfd5710de9b14712c4a621259b951bd10489d0c0. Signed-off-by: Sam James <sam@gentoo.org>
* functions.sh.in: fix typoSam James4 hours1-1/+1
| | | | | | | Kerin fixed this in a later push but I'd already pulled it in by that point. Signed-off-by: Sam James <sam@gentoo.org>
* meson.build: add TODO wrt cleaning up functions.sh templateSam James4 hours1-0/+1
| | | | Signed-off-by: Sam James <sam@gentoo.org>
* Simplify _eprint() and _eend() behaviour markedlyKerin Millar5 hours2-206/+22
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Following almost a year of usage and testing, I now consider my experiments in smart terminal handling to be both a success and a failure by equal measure. I deem them to be a successful in so far as the code functions as intended, and that there have been several positive reports from those intrepid enough to unmask gentoo-functions-1.0. However, I also deem them to be a failure in so far as there is a noticeable impact upon performance. In particular, repeated calls to ebegin() and eend() are handled rather slowly. As such, this commit greatly simplifies the code whilst retaining the most compelling traits of the work that has been performed. The exact changes are described herewith. The _eprint() function shall now only check whether STDOUT refers to a tty. In the event that it does, the message may be printed in a colourised fashion. Otherwise, it shall be printed plainly. No longer will the terminal be asked to remember the position of the cursor (using DECSC), nor shall any variables be used for the purpose of keeping state. The _eend() function shall no longer attempt to restore the prior position of the cursor (using DECRC), nor attempt to determine the present position of the cursor (using ecma48-cpr). Instead, the behaviour is reduced to that of checking whether STDOUT refers to a smart terminal. In the event that it does, the success/failure identifier shall always be printed on the preceding line, right-aligned. Otherwise, it shall be printed plainly on the present line. It remains the case that the terminal will be asked to report its dimensions, so that _eend() can adapt to any adjustments made to the width of the terminal emulator. As a consequence of these changes, the _update_cursor_coords() and _ecma48_cpr() functions are rendered obsolete and have been removed. Additionally, the "genfun_cols", "genfun_y" and "genfun_is_pending_lf" variables are rendered obsolete and have been removed. Finally, the _update_winsize() function has been renamed to _update_columns(). Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Don't check for Emacs every time _eend() is calledKerin Millar6 hours1-11/+10
| | | | | | | | For it is an edge case and it is not clear that it is worth it. Instead, check at the time of sourcing. The underlying bug really ought to be addressed upstream. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Render _update_winsize() immune to the prevailing value of IFSKerin Millar6 hours1-0/+5
| | | | Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Unify the brace styleKerin Millar6 hours1-11/+22
| | | | | | | | I tend to define functions in accordance with the K&R brace style. In deference to the traditions of the project, employ the Allman style instead. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* ecma48-cpr: Use designated initialisers for legibilityKerin Millar7 hours1-12/+15
| | | | Signed-off-by: Kerin Millar <kfm@plushkava.net>
* meson.build: crank version to 1.0gentoo-functions-1.0Sam James2024-02-161-1/+1
| | | | Signed-off-by: Sam James <sam@gentoo.org>
* Account for the buggy CHA implementation in EmacsKerin Millar2023-06-111-2/+11
| | | | | | | | | In Emacs, M-x term opens an "eterm-color" terminal, whose implementation of the CHA (ECMA-48 CSI) sequence suffers from an off-by-one error. Work around it. Note that Eterm does not have this same bug. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Detect PTYs that aren't sufficiently functional to be considered as smartKerin Millar2023-06-111-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In this case, the offender is the PTY that is set up by the _create_pty_or_pipe() utility function in portage, which uses python's pty module. I transpires that its behaviour is not exactly equivalent to whichever controlling terminal is in effect at the time that emerge(1) is executed, if any. As a consequence, the ecma48-cpr utility times out while waiting for a response to the CPR (ECMA-48 CSI) sequence. I can discern no option other than to refrain from considering the terminal as being "smart" in this case. This is accomplished by recognising that stty(1) reports "0 0" when asked to check its size. These are nonsensical readings that would never be produced for a properly functioning, non-dumb terminal. In fact, this check used to be in place but was removed, simply because I forgot how important it is. Processes running under portage that call into gentoo-functions will still be able to produce coloured messages, but all the advantages of the smart terminal handling path will be obviated. This is unfortunate, but I have no intention of returning to the original, broken behaviour of gentoo-functions, which was to assume that the characteristics of the terminal found at the time of sourcing hold forever, and that STDOUT and STDERR will never refer to anything other than that same terminal. I would add that script(1) does not exhibit the same issue. $ tty /dev/pts/0 $ script -c 'tty; stty size <&1; /lib/gentoo/ecma48-cpr <&1' /dev/null Script started, output log file is '/dev/null'. /dev/pts/11 34 144 34 1 Script done. In other words, it is portage that is broken and which should be fixed. Signed-off-by: Kerin Millar <kfm@plushkava.net> Reported-by: Sam James <sam@gentoo.org>
* ecma48-cpr: Ignore SIGINT, SIGTERM and SIGTSTPKerin Millar2023-06-111-0/+7
| | | | | | | | There seems little sense in honouring these signals at the point that the terminal settings are about to be altered and the CPR sequence written. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* ecma48-cpr: Disable flow control in the terminalKerin Millar2023-06-111-2/+1
| | | | | | | While at it, don't bother disabling ECHOE, ECHOK and ECHONL, for they do nothing in the case that ICANON has been disabled. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* ecma48-cpr: Set up the signal handler before writing to the terminalKerin Millar2023-06-111-10/+11
| | | | | | | The idea being to narrow the window of time during which the terminal is in its raw mode as much as is possible. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* ecma48-cpr: Don't assign to new_tty twiceKerin Millar2023-06-111-1/+0
| | | | Signed-off-by: Kerin Millar <kfm@plushkava.net>
* ecma48-cpr: Flush the input queue with tcsetattr(3) insteadKerin Millar2023-06-111-4/+2
| | | | | | | Doing so saves an additional syscall and waits for the output queue to be drained, which is no bad thing. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* test-functions: Add a test for the ebegin() functionKerin Millar2023-06-101-0/+20
| | | | | | | The test determines whether ebegin() appends at least one <newline> character to the message (following the ASCII ellipsis). Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Fix the order of the arguments used to compose the CUP sequenceKerin Millar2023-06-101-6/+6
| | | | | | | | | | | The CUP (ECMA-48 CSI) sequence expects for the row to come first, and the column second. I could have just swapped the arguments but, instead, I have adjusted the surrounding code so as to strictly adhere to this convention (one that is also observed by stty size). This should eliminate the possibility of any such mistake being made in the future. Signed-off-by: Kerin Millar <kfm@plushkava.net> Fixes: 20bc15b5b1009149c4a3d531911d3c219dc55f3a
* Ensure that ebegin() appends a newline to the final messageKerin Millar2023-06-101-1/+1
| | | | | | | | | | | | | The ebegin() function was recently modified to strip the trailing <newline>, if any, from the given message. Later, it was modified to strip all trailing <newline> characters. The purpose of doing so is to ensure that there is no accidental line breaking betwixt the message and the ASCII ellipsis that is appended. However, the function should then have been appending a single <newline> to the composed message before handing it off to _eprint(). Make it so. Signed-off-by: Kerin Millar <kfm@plushkava.net> Fixes: 1947f0ed81f3b95a7e10a8a5a707776948f8a487
* test-functions: Really fix non-conforming TAP outputKerin Millar2023-06-102-11/+13
| | | | | | | | | | | | | | | | | | | | | | The previous "fix" was an experimental patch that was not yet intended for inclusion. This supplemental commit does as little as is necessary to render the test suite properly functional under meson, given that meson.build has been configured to expect the use of the TAP protocol. It also configures meson to be verbose while executing the test suite. As far as I can gather, the TAP support in meson is weak. Subtests do not appear to be supported. For that reason, I have dropped the TAP version from 14 to 13. While meson recognises "# SKIP" as a directive, it does not report the reason that may follow the text. Another issue is that no controlling terminal is present under meson. Therefore, test_update_cursor_coords() is rendered useless, for it will always be skipped. I have left the test in for the time being, in the hope that there may yet be a solution. In any case, this should be enough to render the -9999 ebuild both testable and usable. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* meson.build: Use the TAP protocol for running the testsKerin Millar2023-06-101-0/+1
| | | | Signed-off-by: Kerin Millar <kfm@plushkava.net>
* test-functions: Fix non-conforming TAP 14 outputKerin Millar2023-06-101-11/+18
| | | | | | | | Given that there are no subtests, it isn't permissible to duplicate any of the test numbers. Also, the test_update_cursor_coords() function was not correctly conveying the case where the test is skipped. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Address a slew of shellcheck warningsKerin Millar2023-06-092-9/+13
| | | | | | | | | False-positives galore. It's getting out of hand for test-functions, so I disabled several more tests in its global scope. It was correct to point out that "${PWD}" should be quoted, however. Bash doesn't mind but other sh implementations might. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* test-functions: Add a test for the _update_cursor_coords() functionKerin Millar2023-06-091-0/+22
| | | | Signed-off-by: Kerin Millar <kfm@plushkava.net>
* test-functions: Do not silence ewarn() while testing is_older_than()Kerin Millar2023-06-091-1/+1
| | | | | | | | I changed my mind about this. It's probable that more diagnostic messages will be added in the future and it may then be useful to observe them in the case that the test suite fails. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Restore genfun_y at the same time as genfun_x in _eend()Kerin Millar2023-06-091-0/+1
| | | | | | | That this was not happening did not yet amount to a bug but let's ensure that it cannot be one in the future. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Remove the vim modelineKerin Millar2023-06-091-2/+0
| | | | | | | | I am fed up with the indentation behaviour being wrecked by the imablance between ts and sw and I aleady assume that a tabstop is equivalent to 8 spaces. Enough. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Right-trim messages given to ebegin() and _eprint() where appropriateKerin Millar2023-06-091-4/+12
| | | | | | | Have ebegin() strip all trailing newlines rather than just one at most. Likewise for _eprint() in the case that a smart terminal is found. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* Have ecma48-cpr flush the terminal's input queueKerin Millar2023-06-091-0/+2
| | | | | | | | | | | There is no guarantee that the input queue is empty at the time that ecma48-cpr is executed. If the terminal has input waiting, not only will ecma48-cpr potentially fail but it will pollute the input queue. Work around this by flushing the input queue entirely. I'm not especially pleased by this approach but I can see no other option, short of giving up on the idea of using the CPR sequence at all. Signed-off-by: Kerin Millar <kfm@plushkava.net>
* test-functions: Ensure that ecma48-cpr can be resolved during src_test()Kerin Millar2023-06-092-1/+13
| | | | | | | | The test phase is run prior to the install phase. Compensate for this by wrapping ecma48-cpr with a shim function and redeclaring said function where the value of EBUILD_PHASE is equal to "test". Signed-off-by: Kerin Millar <kfm@plushkava.net>
* meson.build: drop libexecdir for nowSam James2023-06-091-2/+4
| | | | | | | | | | Right now, I don't think there's much benefit for putting the ecma tool in libexecdir given it's in /usr and everything assumes functions.sh is at /lib/gentoo/functions.sh - i.e. it unnecessarily breaks split-usr. Maybe we can just add some symlinks later though. Signed-off-by: Sam James <sam@gentoo.org>
* meson.build: fix running tests out of sourceSam James2023-06-091-1/+1
| | | | Signed-off-by: Sam James <sam@gentoo.org>
* Port to MesonSam James2023-06-094-45/+52
| | | | | | | | | | | | | | | | | | | | | | | | ... yielding ``` /tmp/destdir └── usr └── local ├── bin │   └── consoletype ├── lib │   └── gentoo │   └── functions.sh ├── libexec │   └── gentoo │   └── ecma48-cpr └── share └── man └── man1 └── consoletype.1 11 directories, 4 files ``` Signed-off-by: Sam James <sam@gentoo.org>
* Add a chdir() function to act as a safer alternative to the cd builtinKerin Millar2023-06-092-3/+78
| | | | | | | | | | | | | | | To run cd "$dir" is problematic because: 1) it may consider its operand as an option 2) it will search CDPATH for an operand not beginning with ./, ../ or / 3) it will switch to OLDPWD if the operand is - 4) cdable_vars causes bash to treat the operand as a potential varname This commit introduces a chdir() function that addresses all of these pitfalls. Signed-off-by: Kerin Millar <kfm@plushkava.net> Signed-off-by: Sam James <sam@gentoo.org>
* Overhaul _eprint() and _eend() so as to better leverage smart terminalsKerin Millar2023-06-091-44/+165
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This commit introduces a function named _update_tty_level(), whose reponsibility is to grade the capability of the terminal, if any, on a scale of 0 to 2. A value of 0 indicates that no terminal has been detected, 1 that a dumb terminal has been detected, while 2 indicates that a smart terminal has been detected. For the terminal to be considered as smart, all of the following conditions must hold true. - the TERM variable must not contain "dumb" as a substring - the stty(1) utility must be able to report the terminal's dimensions - the cursor's position must be reported for the ECMA-48 CPR sequence Both the _eprint() and _eend() functions have been adjusted, so as to evaluate the terminal's capabilites on each occasion that they are called, with the overall intention of better exploiting the capabilities of modern terminals, thus providing a better experience for a majority of Gentoo users. These adjustments are described herewith. Common to both functions is that the mechanism of tracking whether a <newline> character is pending for the last printed message is no longer employed for smart terminals. As concerns _eprint(), it now begins by not only determining whether a terminal is attached to STDOUT, but whether it is a smart terminal. In the case that it is smart, it shall be determined whether the cursor is situated at the first column. Should it not be, a <newline> character shall be emitted, on the basis that the last call may have been to a function that printed a message without a trailing <newline>. Next, the message shall be checked, so as to determine whether it contains a trailing <newline> character. Should it not, the message shall be printed, along with a trailing DECSC sequence. The effect of this sequence is to instruct the terminal emulator to save the current position of the cursor. If, on the other hand, the message is found to contain a trailing <newline> character, it shall be stripped prior to printing the message. The terminal shall then be asked to report the new position of the cursor. Should the cursor turn out to be situated at the very last row of the terminal, it shall be assumed that vertical scrolling is about to occur and the cursor shall be moved up by one row, prior to its position being saved by way of the DECSC sequence. Thereafter, two <newline> characters shall be printed, so as to advance to the next line (relative to the message). Otherwise, should the cursor be found to be situated at a row that precedes the last row of the terminal, the DECSC sequence shall be emitted, followed by a single <newline> character. Note that the overall intention is to stash the exact position of the cursor at the point that it is situated at the end of the printed message, but just before a <newline> is printed (if any). As concerns _eend(), and as with _eprint(), it now begins by not only determining whether a terminal is attached to STDOUT, but whether it is a smart terminal, though not before saving the last-known dimensions of the terminal. Next, in the case of a dumb terminal - or no terminal at all - the message shall be printed with a single preceding space. That is, no attempt shall be made to right-align the success/failure indicator. If, on the other hand, the terminal is found to be smart, the behaviour shall be as described by the next paragraph. The current dimensions of the terminal shall be compared against the last-known dimensions of the terminal, as they were prior to the terminal grading procedure. The purpose of doing so is to determine whether the terminal has been recently resized. For the terminal not to have been resized implies that it may yet be possible to write the success/failure indicator on the very same row as the last printed message, even if it were <newline>-terminated. With that in mind, the current position of the cursor shall be saved. Next, the DECRC sequence shall be emitted to the terminal. The effect of this sequence is to instruct the terminal emulator to restore the position of the cursor, as was saved by the DECSC sequence. In other words, the cursor position shall be moved to wherever it was just after the last message was printed, albeit before the trailing <newline> character (if any). Next, the terminal shall be asked to report the new position of the cursor. In the event that the cursor is found to have moved to any other row than the immediately preceding one, it shall be assumed that scrolling has occurred since printing the last message, in which case the cursor shall be moved back to its prior position. Otherwise, the cursor shall be left at its present position. With that out of the way, the degree of indentation required to right-align the indicator shall be trivially calculated by deducting the present column number from the total number of columns spanned by the terminal, with a further 6 deducted to account for the width of the indicator. Should the resulting value be greater than 0, the cursor shall be shifted that many cells to the right. Should the resulting value be negative, it shall be recognised that there is insufficient room to print the indicator on the same line and, instead, the indicator shall be printed on the next line. Should the resulting value be 0, it shall be recognised that the cursor is already situated at the correct column. Finally, the indicator shall be printed, along with a trailing <newline> character. As a consequence of these changes, the genfun_endcol variable has been eliminated, as it is no longer required. It should be noted that both DECSC and DECRC are private mode sequences, and are, thus, not defined by ECMA-48. However, they were introduced by DEC for the VT100 terminal, which was released in 1978. As such, they can be considered as a de-facto standard. Signed-off-by: Kerin Millar <kfm@plushkava.net> Signed-off-by: Sam James <sam@gentoo.org>
* Introduce a utility to obtain the cursor coordinates via ECMA-48 CPRKerin Millar2023-06-071-0/+230
| | | | | | | | | | | | | | | | | | | | | | | The ECMA-48 specification defines the CPR (Cursor Position Report) sequence which, as its name implies, instructs the terminal to divulge the present coordinates of the cursor. Though it is theoretically possible to make use of this sequence by using the shell and standard utilities, it would be tremendously costly and somewhat unreliable to do so. This commit introduces a utility - written in C - that does the job. Its behaviour is quite simple. If STDIN is found to be a terminal, an attempt will be made to initiate - and read - a Cursor Position Report. Upon success, the present row and column of the cursor shall be printed as two space-separated decimal integers. Otherwise, a diagnostic message shall be printed to STDERR, and the utility shall exit with a non-zero status. This utility will be used by the impending overhaul of the _eprint() and _eend() functions. Signed-off-by: Kerin Millar <kfm@plushkava.net> Signed-off-by: Sam James <sam@gentoo.org>
* Don't assume a monochrome terminal in the case that tput colors failsKerin Millar2023-06-071-0/+2
| | | | | Signed-off-by: Kerin Millar <kfm@plushkava.net> Signed-off-by: Sam James <sam@gentoo.org>
* test-functions: Add a test for the _is_visible() functionKerin Millar2023-06-071-0/+21
| | | | | Signed-off-by: Kerin Millar <kfm@plushkava.net> Signed-off-by: Sam James <sam@gentoo.org>
* test-functions: Silence ewarn() while testing is_older_than()Kerin Millar2023-06-071-1/+1
| | | | | | | | | The is_older_than() function now calls ewarn() if given invalid arguments. For the purposes of the test suite, the resulting diagnostic messages aren't particularly interesting. Signed-off-by: Kerin Millar <kfm@plushkava.net> Signed-off-by: Sam James <sam@gentoo.org>
* test-functions: Add a test for the is_identifier() functionKerin Millar2023-06-071-0/+56
| | | | | Signed-off-by: Kerin Millar <kfm@plushkava.net> Signed-off-by: Sam James <sam@gentoo.org>
* Add and integrate the _update_winsize() functionKerin Millar2023-06-071-24/+18
| | | | | | | | | | | | | | | | | | | | | | This new function assumes responsibility for updating the genfun_cols variable. Additionally, it assumes responsibility for updating the newly introduced genfun_rows variable. As before, stty(1) is used to determine the dimensions of the terminal. The approach has been slightly altered so as to incur just one subshell in the course of doing so. The reason for this is that changes will soon be made to the _eprint() and _eend() functions that require for the function to be called repeatedly, not merely at the time of sourcing functions.sh. Should stty(1) fail - or produce nonsensical output - it will no longer be assumed that there are 80 available columns. The requirement for the "[ ok ]" and "[ !! ]" indicators to always be indented was recently eliminated, so there is no longer any need to falsify a value for genfun_cols. Signed-off-by: Kerin Millar <kfm@plushkava.net> Signed-off-by: Sam James <sam@gentoo.org>
* Drop support for the checkwinsize feature of bashKerin Millar2023-06-071-12/+2
| | | | | | | | | | | | | While there is nothing wrong with the way in which it is presently being used, the feature is sufficiently bug-ridden that it will not work correctly with the impending overhaul of the _eprint() and _eend() functions. As such, drop support for it in advance. Chet has pledged to treat my bug report as a feature request, so it may yet be possible to re-introduce support for it in the future. Signed-off-by: Kerin Millar <kfm@plushkava.net> Bug: https://lists.gnu.org/archive/html/bug-bash/2023-02/msg00142.html Signed-off-by: Sam James <sam@gentoo.org>
* Have ebegin() strip the trailing LF of the message it is given, if anyKerin Millar2023-06-071-2/+3
| | | | | | | | This is a precautionary measure, given that ebegin() appends " ..." to the message. Signed-off-by: Kerin Millar <kfm@plushkava.net> Signed-off-by: Sam James <sam@gentoo.org>
* Jettison the genfun_lastbegun_strlen and genfun_lastcall variablesKerin Millar2023-06-071-50/+44
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The genfun_lastbegun_strlen variable was previously used by _eend() to indent the "[ ok ]" and "[ !! ]" indicators in the case that STDOUT is found not to be a tty. Dispense with this variable. Instead, refrain from indenting the indicator at all. After all, the width of the controlling terminal is immaterial unless one is actually intending to print to it. $ { ebegin "Testing"; eend 0; } | cat * Testing ... [ ok ] Apart from simplifying the code, this change brings the overall behaviour of the printing functions closer to that of their counterparts in OpenRC. The genfun_lastcall variable was previously used by the _eprint() function for the sole purpose of printing a LF (newline) character in the case that the last message was printed as a consequence of calling the ebegin() function. It would do so in anticipation of the _eend() function later emitting the CUU (ECMA-48 CSI) sequence to move the cursor up by one line, just prior to printing the "[ ok ]" and "[ !! ]" indicators. Additionally, it was used by the ebegin() function to determine whether a terminating LF character should follow the printed message. Specifically, it would elect to print a LF character in the case that ECMA-48 CSI sequences had been disabled at the time of functions.sh being sourced. Finally, the value of genfun_lastcall would influence the (now defunct) method by which the _eend() function would calculate the degree of indentation required for the indicator in the case of STDOUT not being a tty. This variable has been dispensed with and replaced by a variable named genfun_is_pending_lf. As its name suggests, its purpose is to track whether the last printed message happened to contain a terminating LF character. Now, whenever the _eprint() function is called, it consults the variable so as to determine whether a LF character should be printed for the purpose of terminating the last message, just before proceeding to print the next one. Once the next one has been printed, the value of the variable is updated accordingly. Similarly, the _eend() function consults the variable so as to determine whether a LF character should be printed to a terminal, just prior to the CUU (ECMA-48 CSI) sequence. Ultimately, ebegin() will no longer be sloppily treated as a special case. Rather, any printing function that inhibits the addition of a terminating LF character (ebegin, einfon, ewarn, errorn etc) will have its message be terminated upon calling any printing function thereafter. In addition to cleaning up the code a little, these changes should render the impending overhaul of the _eprint() and _eend() functions easier to digest, once it lands. Signed-off-by: Kerin Millar <kfm@plushkava.net> Signed-off-by: Sam James <sam@gentoo.org>
* Use ewarn() to print the recently added diagnostic messagesKerin Millar2023-06-071-5/+5
| | | | | | | | | | | | | | | Both the _eprint() and _eend() functions will be overhauled in the near future, at which point conveying diagnostic messages (indirectly) to _eprint() will become beneficial. This commit affects the following functions. - esyslog - is_older_than - veend - vewend Signed-off-by: Kerin Millar <kfm@plushkava.net> Signed-off-by: Sam James <sam@gentoo.org>
* Simplify ECMA-48 sequence definition and dial back on the use of tput(1)Kerin Millar2023-02-191-18/+28
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Up until very recently, functions.sh would not attempt to determine whether a given message was being printed to a terminal. Over the years, this has caused some deleterious effects, such as printing ECMA-48 CSI and SGR sequences to file descriptors that are not necessarily attached to a terminal. Another issue used to be that these sequences would be emitted to terminals identifying themselves as "dumb". Rather than tackle the underlying problems, the historical response has tended towards further entrenching the use of the ncurses implementation of tput(1). While it has its uses, the way in which it is employed is in no way portable. POSIX only guarantees the availability of the clear, init and reset operands. Presently, the colors operand is used as a heuristic to determine whether the ncurses implementation is available before proceeding to run a series of commands that will usually output ECMA-48 SGR sequences that are standard for all but ancient and/or obscure video terminals. Another present use of tput(1) is in generating the CUU1 and CUF sequences. In the overwhelming majority of cases, it will generate these as standard ECMA-48 CSI sequences. To put this in perspective, the DEC VT100, a video terminal released almost 45 years ago, supports these sequences. The ECMA-48 specification, itself, is almost as old. Unfortunately, reasoning with such matters tends towards a dichotomy. On the one hand, there is a camp that considers it a cardinal sin to attempt to emit an ANSI escape sequence without first having consulted a specific implementation of tput(1). On the other, there is the camp that points out that ECMA-48 sequences are highly portable and that video terminals from the 1970s and 1980s are largely obsolete. Here are two articles that present opposing points of view. https://mywiki.wooledge.org/BashFAQ/037 https://xn--rpa.cc/irl/term.html Of late, I find the arguments presented by the latter camp to be increasingly convincing. As such, the goal of this commit is twofold. Firstly, to implement an independent method for detecting a dumb terminal. To that end, a _has_dumb_terminal() function has been added, which simply checks the value of TERM. This, alone, determines whether the CUU1 and CUF sequences should be assigned, with tput(1) no longer being used to generate them. Secondly, to further dial back on the use of tput(1) by not using it to generate the SGR (colour) sequences. While I believe that nobody would notice if we were to do away with the use of tput(1) altogether, I have taken a nuanced approach by continuing to use it for detecting the number of available colours. To that end, a _has_monochrome_terminal() function has been implemented. This, alone, determines whether the SGR sequences should be assigned. The function works by first checking whether the terminal is dumb. If not, it tries to run "tput colors", before checking whether -1 colours are reported, for that is how the ncurses implementation reports the absence of colour support. The resulting code is simpler and more pleasing to read, with tput being run once, at most. Also, because there is no longer a requirement to handle the CSI sequences in a raw form, they are now declared as strings containing backslash-escape sequences, with printf %b being used to decode them. This has the advantage of not emitting the raw codes to STDERR while using xtrace for debugging. In the future, I think that it may well be possible to drop the function that detects a monochrome terminal and rely on dumb terminal detection instead but let's see how it goes. Signed-off-by: Kerin Millar <kfm@plushkava.net> Signed-off-by: Sam James <sam@gentoo.org>
* Precede end columns with a single space, not two, in _eend()Kerin Millar2023-02-191-4/+4
| | | | | | | | | A beneficial effect of doing so, apart from being able to show one more character, is that the the associated arithmetic expansions uniformly work with the genuine length of the end column, which is 7. Signed-off-by: Kerin Millar <kfm@plushkava.net> Signed-off-by: Sam James <sam@gentoo.org>