( Title: Color terminal support and conditional display for testers File: tester-display.fs Authors: Krishna Myneni, David N. Williams License: Public Domain Version: 1.4.2 Revised: January 17, 2021 The date above may reflect cosmetic changes not logged here. Version 1.4.2 26Sep20 * Added: VFX-HOST * Replaced REDEFINED-WARNING by REDEFINED-WARNING-OFF and REDEFINED-WARNING-ON. 27Sep20 * Added VFX-HOST to ?.FIRST-CR. 29Sep20 * Added conditionally defined VERBOSE. Clarified comment about defining it yet again. 17Jan21 * Revised introductory comments. Version 1.4.0 31May17 * Bumped version from 1.1.2. * Added: UNDEFINED-HOST HOST-S ?.FIRST-CR * Revised host flag logic. 4Jun17 * Added: REDEFINED-WARNING 8Feb18 * Clarified comment about defining VERBOSE. Version 1.1.2 19Feb12 * Replaced iForth color display for Windows hosts by code from Marcel Hendrix that works with black or white background. * Replaced ANSI-TERMINAL-COLORS by USE-TERMINAL-COLORS. Version 1.1.1 30Nov10 * Removed conditional CR initializer. 19Feb11 * Removed PARSE-AREA@. 28Feb11 * Made host constant definition conditional. Version 1.1.0 13Nov10 * Extracted from former ttester-display.fs 1.0.6. * Added host detection. * Added conditional CR initializer. 24Nov10 * Replaced #27 by 27 in ESC definition. This code detects the Forth host environment and supports color terminal display and conditional commenting for ttester, or testers based on ttester. It conditionally defines VERBOSE. That means it should not be loaded before ttester, which defines it unconditionally. Enhanced testers like ttester-xf also define VERBOSE conditionally, and work fine when loaded later. This code is not as strict about minimizing the use of non-CORE words as are ttester and ttester-xf themselves. In particular, it uses the TOOLS EXT words [UNDEFINED], [IF], [ELSE], [THEN], and the interpreted, FILE version of S". This file should be loaded before the first information display, so that ?.FIRST-CR defined below is available. ) \ *** HOST SYSTEMS s" FORTH-NAME" ENVIRONMENT? dup [IF] drop s" pfe" compare 0= [THEN] CONSTANT PFE-HOST s" gforth" ENVIRONMENT? dup [IF] ( s flag) nip nip [THEN] CONSTANT GFORTH-HOST s" IFORTH" ENVIRONMENT? dup [IF] ( flag) drop [THEN] CONSTANT IFORTH-HOST [DEFINED] VFXFORTH CONSTANT VFX-HOST PFE-HOST GFORTH-HOST or IFORTH-HOST or VFX-HOST or 0= CONSTANT UNKNOWN-HOST : HOST-S ( -- c-addr len ) [ PFE-HOST ] [IF] s" PFE" [THEN] [ GFORTH-HOST ] [IF] s" GFORTH" [THEN] [ IFORTH-HOST ] [IF] s" IFORTH" [THEN] [ VFX-HOST ] [IF] s" VFX" [THEN] [ UNKNOWN-HOST ] [IF] s" UNKOWNN" [THEN] ; : REDEFINED-WARNING-OFF ( -- ) [ PFE-HOST ] [IF] REDEFINED-MSG off [THEN] [ GFORTH-HOST ] [IF] WARNINGS off [THEN] [ IFORTH-HOST ] [IF] WARNING off [THEN] [ VFX-HOST ] [IF] postpone -WARNINGS [THEN] [ UNKNOWN-HOST ] [IF] ." ***UNKNOWN-HOST, can't turn redefined warning off." cr [THEN] ; : REDEFINED-WARNING-ON ( -- ) [ PFE-HOST ] [IF] REDEFINED-MSG on [THEN] [ GFORTH-HOST ] [IF] WARNINGS on [THEN] [ IFORTH-HOST ] [IF] WARNING on [THEN] [ VFX-HOST ] [IF] postpone +WARNINGS [THEN] [ UNKNOWN-HOST ] [IF] ." ***UNKNOWN-HOST, can't turn redefined warning on." cr [THEN] ; \ *** COLOR TERMINAL SUPPORT ( The only Forth system at present for which we can handle Windows color-terminal settings is iForth, thanks to code supplied by Marcel Hendrix. Settings for Windows with other Forth systems than can be recognized by ENVIRONMENT? would be welcomed. The flag constant USE-TERMINAL-COLORS should be predefined as false before loading this file in the following cases: 1. You don't want colors. 2. Your system is not iForth and is hosted by a system that does not support ANSI colors, such as Windows. 3. Your system is iForth and is hosted by a non-Windows system that does not support ANSI colors. ) [UNDEFINED] USE-TERMINAL-COLORS [IF] true CONSTANT USE-TERMINAL-COLORS \ use ANSI when not iForth [THEN] BASE @ decimal [UNDEFINED] ESC [IF] 27 CONSTANT esc [THEN] USE-TERMINAL-COLORS 0= [IF] : normal-text ( -- ) ; immediate : red-text ( -- ) ; immediate : green-text ( -- ) ; immediate : blue-text ( -- ) ; immediate [ELSE] IFORTH-HOST 0= [IF] \ ANSI (Krishna Myneni) : normal-text ( -- ) esc emit ." [0m" ; : red-text ( -- ) esc emit ." [31m" ; : green-text ( -- ) esc emit ." [32m" ; : blue-text ( -- ) esc emit ." [34m" ; [ELSE] WINDOWS? [IF] \ Marcel Hendrix : normal-text ( -- ) 7 TO TextFGColor SetTerm ; : red-text ( -- ) 1 TO TextFGColor SetTerm ; : green-text ( -- ) 2 TO TextFGColor SetTerm ; \ blue would be too dark : blue-text ( -- ) 6 TO TextFGColor SetTerm ; [ELSE] : bs-emits ( u -- ) 0 ?DO bs emit LOOP ; : normal-text ( -- ) esc emit ." [0m" 3 bs-emits ; : red-text ( -- ) esc emit ." [31m" 4 bs-emits ; : green-text ( -- ) esc emit ." [32m" 4 bs-emits ; \ works both for b and w background : blue-text ( -- ) esc emit ." [36m" 4 bs-emits ; [THEN] [THEN] [THEN] BASE ! \ *** CONDITIONAL DISPLAY \ In case this file is loaded before, or without ttester-xf.fs or \ xftester.fs: [UNDEFINED] VERBOSE [IF] VARIABLE VERBOSE false VERBOSE ! [THEN] : ?. ( x -- ) VERBOSE @ IF . ELSE drop THEN ; : ?.CR ( -- ) VERBOSE @ IF cr THEN ; : ?.( ( -- ) VERBOSE @ IF postpone .( ELSE postpone ( THEN ; : ?TYPE ( s -- ) VERBOSE @ IF type ELSE 2drop THEN ; : ?." ( ccc<"> -- ) \ a version of Krishna Myneni's COMMENT ( Parse the next string in the parse area with double quote as trailing delimiter. If VERBOSE is true, print the resulting string in blue, and if no quote is present, emit a trailing CR. Otherwise, discard the parsed string. A version of Krishna Myneni's COMMENT with optional CR. ) source nip >in @ - ( unparsed.len) [char] " parse VERBOSE @ IF dup >r blue-text type normal-text r> = IF cr THEN ELSE 2drop drop THEN ; : ?.FIRST-CR ( -- ) ( Some hosts need a CR in order to display the first line of testing output on its own line, instead of at the end of a system message line. ) PFE-HOST IFORTH-HOST or VFX-HOST or IF ?.CR THEN ;