From e0f6947ee0c7ed8cf693e0f3dad83f9d1364e064 Mon Sep 17 00:00:00 2001 From: Fabio Date: Sat, 26 Apr 2025 09:19:45 +0200 Subject: [PATCH] Complete Ansi docs, added examples --- Ansi_readme.md | 121 +++++++++++++++------ Examples/Ansi/basics.hws | 197 +++++++++++++++++++++++++++++++++++ Examples/Ansi/requesters.hws | 111 ++++++++++++++++++++ Examples/Ansi/term_app.hws | 116 +++++++++++++++++++++ 4 files changed, 514 insertions(+), 31 deletions(-) create mode 100644 Examples/Ansi/basics.hws create mode 100644 Examples/Ansi/requesters.hws create mode 100644 Examples/Ansi/term_app.hws diff --git a/Ansi_readme.md b/Ansi_readme.md index f06def8..ddc579e 100644 --- a/Ansi_readme.md +++ b/Ansi_readme.md @@ -34,6 +34,16 @@ I’ve developed this library to have an invaluable help while I’m debugging a Of course you need that your host system console is able to understand ANSI ascape codes, but almost any OS is able to do that… except Windows. For the Windows OS you need to install a thirdy party application to accomplish the task. On my previous development machine, running Windows 10, I’m using **ansicon** program to make use of ANSI codes. +Along with the ANSI support there is also a `term` object that can be used to drive simple terminal applications with an inbuilt menu system, it's very basic, but can help you to quickly build utilities that need to run on the terminal without messing with complex frameworks. + +## EXAMPLES + +You can find the sollowing examples in the [Examples/Ansi](Examples/Ansi) folder: + +- [basic.hws](Examples/Ansi/basic.hws) : This example tests and shows the basic like colors, shapes, styles and so on. +- [requesters.hws](Examples/Ansi/requesters.hws) : This program shows how to use the various requesters you can use in your apps. +- [term_app.hws](Examples/Ansi/term_app.hws) : Simple TermApp application that shows how to use the TermApp object with its own menu system. + ## ANSI Functions All ANSI related functions are mapped into the `Ansi` table. @@ -71,9 +81,9 @@ Here is a list of the included functions mapped in the Ansi table, each function - [Ansi.GetDeleteCharacters()](#ansi_getdeletecharacters) - [Ansi.GetDeleteLines()](#ansi_getdeletelines) - [Ansi.GetInsertBlankLines()](#ansi_getinsertblanklines) -- `Ansi.GetRndBGColor()` -- `Ansi.GetRndFgColor()` -- `Ansi.Set()` +- [Ansi.GetRndBGColor()](#ansi_getrndbgcolor) +- [Ansi.GetRndFgColor()](#ansi_getrndfgcolor) +- [Ansi.Set()](#ansi_set) --- @@ -81,21 +91,29 @@ Here is a list of the included functions mapped in the Ansi table, each function Here is a list of all the `term` object supported methods: -- `Term.App:ClearInfo()` -- `Term.App:ClearStatus()` -- `Term.App:ClearWarning()` -- `Term.App:GridView()` -- `Term.App:MenuAdd()` -- `Term.App:New()` -- `Term.App:Progress()` -- `Term.App:SetInfo()` -- `Term.App:SetStatus()` -- `Term.App:SetWarning()` -- `Term.App:ShowInput()` -- `Term.App:ShowMenu()` -- `Term.App:ShowMessage()` -- `Term.App:Start()` -- `Term.App:alignNumber()` +- [Term.App:ClearInfo()](#term_app_clearinfo) +- [Term.App:ClearStatus()](#term_app_clearstatus) +- [Term.App:ClearWarning()](#term_app_clearwarning) +- [Term.App:GridView()](#term_app_gridview) +- [Term.App:MenuAdd()](#term_app_menuadd) +- [Term.App:New()](#term_app_new) +- [Term.App:Progress()](#term_app_progress) +- [Term.App:SetInfo()](#term_app_setinfo) +- [Term.App:SetStatus()](#term_app_setstatus) +- [Term.App:SetWarning()](#term_app_setwarning) +- [Term.App:ShowInput()](#term_app_showinput) +- [Term.App:ShowMenu()](#term_app_showmenu) +- [Term.App:ShowMessage()](#term_app_showmessage) +- [Term.App:Start()](#term_app_start) +- [Term.App:alignNumber()](#term_app_alignnumber) +- [Term.App:_DrawHeader()](#term_app_drawheader) +- [Term.App:_DrawInfo()](#term_app_drawinfo) +- [Term.App:_DrawMenuItems()](#term_app_drawmenuitems) +- [Term.App:_DrawMenuState()](#term_app_drawmenustate) +- [Term.App:_DrawStatus()](#term_app_drawstatus) +- [Term.App:_DrawWarning()](#term_app_drawwarning) +- [Term.App:_FullWidth()](#term_app_fullwidth) +- [Term.App:_FixedWidth()](#term_app_fixedwidth) --- @@ -103,23 +121,23 @@ Here is a list of all the `term` object supported methods: List of all `term` related functions: -- `Term.CTLine()` -- `Term.Clear()` -- `Term.GetSize()` -- `Term.Input()` -- `Term.Line()` -- `Term.Print()` -- `Term.PrintAt()` -- `Term.SetTermSize()` +- [Term.CTLine()](#term_ctline) +- [Term.Clear()](#term_clear) +- [Term.GetSize()](#term_getsize) +- [Term.Input()](#term_input) +- [Term.Line()](#term_line) +- [Term.Print()](#term_print) +- [Term.PrintAt()](#term_printat) +- [Term.SetTermSize()](#term_settermsize) --- ### TERM DRAWING FUNCTION -- `Term.Draw.Box()` -- `Term.Draw.FBox()` -- `Term.Draw.HLine()` -- `Term.Draw.VLine()` +- [Term.Draw.Box()](#term_draw_box) +- [Term.Draw.FBox()](#term_draw_fbox) +- [Term.Draw.HLine()](#term_draw_hline) +- [Term.Draw.VLine()](#term_draw_vline) --- @@ -243,7 +261,7 @@ Here is a list of the characters used to draw the shapes, they are stored in the .BLO7E = Chr(9607) ▇ BOX DRAWINGS LOWER 7 EIGHTHS .FB = Chr(9608) █ BOX DRAWINGS FULL BLOCK .LSB = Chr(9617) ░ LIGHT SHADE -.MSB = Chr(9618) ▒ MEDIUM SHADE +.MSB = Chr(9618) ▒ MEDIUM SHADE .HSB = Chr(9619) ▓ HEAVY SHADE .L7E = Chr(9609) ▉ BOX DRAWINGS LEFT 7 EIGHTHS .L3Q = Chr(9610) ▊ BOX DRAWINGS LEFT 3 QUANRTERS @@ -261,6 +279,7 @@ Here is a list of the characters used to draw the shapes, they are stored in the # DOCUMENTATION + ## Term.SetTermSize(r, c, color) This function sets the terminal's mode building a string that it's stored in the `Ansi.Code.SetTerm` string, the mode assigned depends on the specified size. @@ -285,6 +304,7 @@ To set the terminal you have to print the string returned by this function. --- + ## Term.GetSize() On a Linux environment this call will report the terminal size, if we are not on Linux the contents of `Term.Size.Rows` and `Term.size.columns` will be returned. @@ -296,6 +316,7 @@ On a Linux environment this call will report the terminal size, if we are not on --- + ## Ansi.GetRndFgColor() Returns an ANSI string you can use to set a random foreground color (0-8). @@ -306,6 +327,7 @@ Returns an ANSI string you can use to set a random foreground color (0-8). --- + ## Ansi.GetRndBgColor() Returns an ANSI string you can use to set a random background color (0-8). @@ -316,6 +338,7 @@ Returns an ANSI string you can use to set a random background color (0-8). --- + ## Ansi.Set(value, termSize) Switch ANSI mode on or off and adjust the terminal size in characters. @@ -378,6 +401,7 @@ Returns a string with the ANSI escape sequence needed to clear `chars` character --- + ## Ansi.GetInsertCharacters(chars) Returns the escape sequence to insert `chars` characters. @@ -407,6 +431,7 @@ Returns the escape sequence to delete `chars` characters. --- + ## Ansi.GetCursorMove(row, column) Returns the escape sequence to move the cursor to (`row`, `column`). @@ -436,6 +461,7 @@ Returns the escape sequence needed to move the cursor by the specified number of --- + ## Term.getInput(t, wordwrap, linefeed, maxLen) Listen to stdin for a keypress @@ -457,6 +483,7 @@ The input is terminated when `ENTER` or `RETURN` key is pressed. --- + ## Term.Input(t, wordwrap, linefeed) Prompt the user with a message using the inbuilt `ConsolePrompt` command. @@ -473,6 +500,7 @@ Prompt the user with a message using the inbuilt `ConsolePrompt` command. --- + ## Term.PrintAt(r, c, txt) Move the cursor at the given position and print the `txt` text. @@ -485,6 +513,7 @@ Move the cursor at the given position and print the `txt` text. --- + ## Term.Print(t, wordwrap, newlineoffset, linefeed, getdata Print a string to the console. @@ -499,6 +528,7 @@ Print a string to the console. --- + ## Term.Draw.FBox(options) Draws a filled box. @@ -514,6 +544,7 @@ Draws a filled box. --- + ## Term.Draw.Box(options) Draws an empty box. @@ -531,6 +562,7 @@ Draws an empty box. --- + ## Term.Draw.HLine(options) Draw an horizontal line. @@ -546,6 +578,7 @@ Draw an horizontal line. --- + ## Term.Draw.VLine(options) Draw an vertical line. @@ -561,6 +594,7 @@ Draw an vertical line. --- + ## Term.Line(character, style) Draw an horizontal line with any character. @@ -572,6 +606,7 @@ Draw an horizontal line with any character. --- + ## Term.CTLine(character, txt, style) Draws a full width line with the given `character` and center on it the given `txt` text with the specified `style`. @@ -584,6 +619,7 @@ Draws a full width line with the given `character` and center on it the given `t --- + ## Term.Clear() Sets the background color to black, clears the screen and moves the cursor to the top-left angle (home). @@ -594,6 +630,7 @@ Sets the background color to black, clears the screen and moves the cursor to th This is an useful object filled with methods to enable the programmer to build menu-driven applications easily. + ## Term.App:New() Creates a new, empty, `TermApp` object. @@ -604,6 +641,7 @@ Creates a new, empty, `TermApp` object. --- + ## Term.App:\_FullWidth(txt, align) **Utility method** that adds the necessary spaces to the given `txt` and returns a string that fills the current terminal's width. @@ -619,12 +657,14 @@ Creates a new, empty, `TermApp` object. --- + ## Term.App:\_DrawHeader() **Utility method** used to draw the application's header. --- + ## Term.App:SetStatus(txt) Sets the status line with the given text. @@ -635,12 +675,14 @@ Sets the status line with the given text. --- + ## Term.App:ClearStatus(txt) Clears the status line. --- + ## Term.App:SetInfo(txt) Sets the info line with the given text. @@ -651,12 +693,14 @@ Sets the info line with the given text. --- + ## Term.App:ClearInfo() Clears the informations line. --- + ## Term.App:SetWarning(txt) Sets the warnings line with the given text. @@ -667,12 +711,14 @@ Sets the warnings line with the given text. --- + ## Term.App:ClearWarning() Clears the warnings line. --- + ## Term.App:\_DrawStatus(txt) **Utility method** that prints the current status message if `txt` is not specified, otherwise replaces the message with `txt` and prints it. @@ -683,6 +729,7 @@ Clears the warnings line. --- + ## Term.App:\_DrawInfo(txt) **Utility method** that prints the current info message if `txt` is not specified, otherwise replaces the message with `txt` and prints it. @@ -693,6 +740,7 @@ Clears the warnings line. --- + ## Term.App:\_DrawWarning(txt) **Utility method** that prints the current warning message if 'txt' is not specified, otherwise replaces the message with 'txt' and prints it. @@ -703,6 +751,7 @@ Clears the warnings line. --- + ## Term.App:\_DrawMenuState(txt) **Utility method** that prints the current menu's state if 'txt' is not specified, otherwise replaces the message with 'txt' and prints it. @@ -713,6 +762,7 @@ Clears the warnings line. --- + ## Term.App:MenuAdd(name, items) This method is used to define an application menu. @@ -731,6 +781,7 @@ If `command` is an empty string the description is interpreted as a menu header. --- + ## Term.App:\_FixedWidth(txt, size, align) **Utility method** to format the given `txt` to a defined `size` and the passed @@ -749,6 +800,7 @@ If the string does not fit into the specified size it will be trunkated but the --- + ## Term.App:alignNumber(value, decimals, size) Returns the number `value` as a string, right aligned, with a length of `size` characters and `decimals` decimal numbers. @@ -765,6 +817,7 @@ Returns the number `value` as a string, right aligned, with a length of `size` c --- + ## Term.App:\_DrawMenuItems(menuEntry) **Utility method** to draw the menu items of the given menu entry. @@ -775,6 +828,7 @@ Returns the number `value` as a string, right aligned, with a length of `size` c --- + ## Term.App:ShowMessage(txt, waitkey, wordwrap) This method is used to show a message to the user. @@ -787,6 +841,7 @@ This method is used to show a message to the user. --- + ## Term.App:GridView(data, columns, headers, positions, callback, info, formats) Display data in a scrollable grid. @@ -821,6 +876,7 @@ Keys used to navigate the grid are hardcoded: --- + ## Term.App:ShowInput(txt, req) Shows an input requester. @@ -836,6 +892,7 @@ Shows an input requester. --- + ## Term.App:ShowMenu(menuEntry) Method used to show a menu and to handle the user's input. @@ -846,6 +903,7 @@ Method used to show a menu and to handle the user's input. --- + ## Term.App:Progress(txt, vcurr, vmax) Method used to show and update a progress bar in the status line. @@ -858,6 +916,7 @@ Method used to show and update a progress bar in the status line. --- + ## Term.App:Start(menuEntry) Starts the application showing the menu entry specified as its first menu. diff --git a/Examples/Ansi/basics.hws b/Examples/Ansi/basics.hws new file mode 100644 index 0000000..3479768 --- /dev/null +++ b/Examples/Ansi/basics.hws @@ -0,0 +1,197 @@ +; ------------------------------------------------------------------- +; ANSI Example, basic usage +; ------------------------------------------------------------------- +; Author : Fabio Falcucci (Allanon) +; License : Freeware +; Version : 1.0 +; Release : 04/11/2024 +; Dependancies : Ansi.hws +; PayPal Support : hijoe@tin.it +; Support me on Patreon! https://www.patreon.com/Allanon71 +; Github repository https://github.com/Allanon71 +; ------------------------------------------------------------------ + +; Let's include the ANSI library +@INCLUDE "../../Ansi.hws" + +; Since we are targeting console applications we don't need an Hollyweood display +@DISPLAY { Hidden = True } + +; Let's ask for the current terminal size: this works well on Linux systems but +; on Windows there are issues. +Term.GetSize() + +; Switch ANSI On and clear the screen +Term.Clear() +Term.AnsiMode = True ; By default colors are switched off you can also use + ; Ansi.Set() function for this. + + +; Prints what we have found using color macros +Term.Print("~{NOTI}~{HOME}ANSI Library Test Program~{NORM}", True, Nil, True) +Term.Print("-------------------------", True, Nil, True) +Term.Print("~{FGRN}Terminal Size : ~{FYEL}" .. Term.Size.Rows .. "~{FGRN}x~{FYEL}" .. Term.Size.Columns, True, Nil, True) +Term.Print("~{NORM}", True, Nil, True) + +; Let's test wordwrapping +t1 = [[This is just a test string for the ANSILib library, the following test will check if the text is printed as expected with Ansi support ON and OFF then the wordwrap will be turned off and the test will be repeated. +This line should be printed alone at the bottom.]] + +; We are using ConsolePrint() that does not have wordwrap support +Term.Print("~{NOTI}Inbuilt ConsolePrint() command~{NORM}", True, Nil, True) +ConsolePrint(t1) +Term.Print("", True, Nil, True) + +; Now let's see wordwrap in action +Term.Print("~{NOTI}But Term.Print() can wordwrap text...~{NORM}", True, Nil, True) +Term.Print(t1, True, Nil, True) +Term.Print("", True, Nil, True) + +Term.Print("~{NORM}", True, Nil, True) +Term.Input("Hit Enter to continue:", True, True) + +; LINES +; This part draws some random horizontal lines, with random type "full" and "light" +; and random colors. +Term.Clear() +Term.Print("~{NOTI}~{HOME}TESTING LINES~{NORM}", True, Nil, True) +Term.Print("~{FGRN}Random Horizontal Lines (with a delay)...~{NORM}", True, Nil, True) +For Local r = 4 To Term.Size.Rows-4 + Local c, l = Rnd(20)+1, Rnd(50)+1 + Term.Draw.HLine({ row = r, + column = c, + length = l, + Color = Ansi.GetRndFgColor(), + Type = IIf(Rnd(2)=1, "full", "light") }) + Wait(50, #MILLISECONDS) +Next +Term.Input(Ansi.GetCursorMove(3, 1) .. "Hit Enter to continue...", True, True) + +; Now let's test vertical lines +Term.Clear() +Term.Print("~{NOTI}~{HOME}TESTING LINES~{NORM}", True, Nil, True) +Term.Print("~{FGRN}Random Vertical Lines (with a delay)...~{NORM}", True, Nil, True) +For Local c = 2 To Term.Size.Columns-4 + Local r, l = Rnd(5)+5, Rnd(15)+1 + Term.Draw.VLine({ row = r, + column = c, + length = l, + Color = Ansi.GetRndFgColor(), + Type = IIf(Rnd(2)=1, "full", "light") }) + Wait(50, #MILLISECONDS) +Next +Term.Input(Ansi.GetCursorMove(3, 1) .. "Hit Enter to continue...", True, True) + +; BOXES +; Testing filled boxes. +Term.Clear() +Term.Print("~{NOTI}~{HOME}TESTING BOXES~{NORM}", True, Nil, True) +Term.Print("~{FGRN}Random Filled Boxes (with a delay)...~{NORM}", True, Nil, True) +For Local i = 1 To 40 + Local r = Rnd(Term.Size.Rows)-3 + Local c = Rnd(Term.Size.Columns)-10 + Local w = Rnd(Term.Size.Columns-c-4)+1 + Local h = Rnd(Term.Size.Rows-r-4)+1 + Term.Draw.FBox({ row = r, + column = c, + width = w, + height = h, + Color = Ansi.GetRndFgColor() }) + Wait(50, #MILLISECONDS) +Next +Term.Input(Ansi.GetCursorMove(3, 1) .. "Hit Enter to continue...", True, True) + +; Testing empty boxes. +Term.Clear() +Term.Print("~{NOTI}~{HOME}TESTING BOXES~{NORM}", True, Nil, True) +Term.Print("~{FGRN}Random Empty Boxes (with a delay)...~{NORM}", True, Nil, True) +For Local i = 1 To 40 + Local r = Rnd(Term.Size.Rows)-3 + Local c = Rnd(Term.Size.Columns)-10 + Local w = Rnd(Term.Size.Columns-c-4)+1 + Local h = Rnd(Term.Size.Rows-r-4)+1 + Term.Draw.Box({ row = r, + column = c, + width = w, + height = h, + Color = Ansi.GetRndFgColor(), + type = "light", + angles = IIf(RndF()>0.5, "squared", "rounded") }) + Wait(50, #MILLISECONDS) +Next +Term.Input(Ansi.GetCursorMove(3, 1) .. "Hit Enter to continue...", True, True) + +; PRINTAT +; Testing PrintAt function +Term.Clear() +Term.Print("~{NOTI}~{HOME}TESTING PRINTAT()~{NORM}", True, Nil, True) +Term.Print("~{FGRN}Printing at random positions (with a delay)...~{NORM}", True, Nil, True) +For Local i = 1 To 40 + Local r, c = Rnd(Term.Size.Rows-5)+4, Rnd(Term.Size.Columns-4)+1 + Term.PrintAt(r, c, Ansi.GetRndFgColor() .. "Print At (" .. r .. "," .. c .. ")") + Wait(50, #MILLISECONDS) +Next +Term.Input(Ansi.GetCursorMove(3, 1) .. "Hit Enter to continue...", True, True) + +; INPUT +; Testing Input routines +Term.Clear() +Term.Print("~{NOTI}~{HOME}TESTING TEXT INPUT~{NORM}", True, Nil, True) +Term.Print("~{FGRN}Please answer the questions...~{NORM}", True, Nil, True) +Local name = Term.Input(Ansi.GetCursorMove(10, 2) .. "~{FCYA}Type your name and hit ENTER:~{ADVI}") +Local sname = Term.Input(Ansi.GetCursorMove(12, 2) .. "~{FCYA}Type your surname and hit ENTER:~{ADVI}") +Term.PrintAt(14, 2, "~{FWHI}name: ~{FGRN}" .. name .. "~{FWHI}, surname: ~{FGRN}" .. sname .. "~{NORM}") +Term.Input(Ansi.GetCursorMove(3, 1) .. "Hit Enter to continue...", True, True) + +; COLORS +; You can pick random colors, both for background & foreground +Term.Clear() +Term.Print("~{NOTI}~{HOME}TESTING COLORS~{NORM}", True, Nil, True) +Term.Print("~{FGRN}Printing some lines with random colors...~{NORM}", True, Nil, True) +Term.Print("", True, Nil, True) +For Local i = 1 To 5 + Term.Print(Ansi.GetRndFgColor() .. Ansi.GetRndBgColor() .. i .. ":Testing Random Colors~{NORM}") +Next +Term.Input(Ansi.GetCursorMove(3, 1) .. "Hit Enter to continue...", True, True) +; You can pick random colors, both for background & foreground +Term.Clear() +Term.Print("~{NOTI}~{HOME}TESTING COLORS~{NORM}", True, Nil, True) +Term.Print("~{FGRN}Using the most common ANSI escape sequences...~{NORM}", True, Nil, True) +Term.Print("", True, Nil, True) +Local toPrint = [[01:RESET command ->~{RSET}<- done. +02:BOLD ON/OFF : ~{BLD+}BOLD style~{BLD-} BOLD OFF +03:DIM ON/OFF : ~{DIM+}DIM style~{RSET} DIM OFF +04:ITALIC ON/OFF : ~{ITA+}ITALIC style~{ITA-} ITALIC OFF +05:UNDERLINE ON/OFF : ~{UND+}UNDERLINE style~{UND-} UNDERLINE OFF +06:BLINK ON/OFF : ~{BLI+}BLINK style~{BLI-} BLINK OFF +07:INVERSE ON/OFF : ~{INV+}INVERSE style~{INV-} INVERSE OFF +08:HIDDEN ON/OFF : ~{HID+}HIDDEN style~{HID-} HIDDEN OFF +09:STRIKETR ON/OFF : ~{STR+}STRIKETR style~{STR-} STRIKETR OFF +10:FG BLACK : ~{FBLK}FG BLACK style~{FDEF} FG BLACK OFF +11:FG RED : ~{FRED}FG RED style~{FDEF} FG RED OFF +12:FG GREEN : ~{FGRN}FG GREEN style~{FDEF} FG GREEN OFF +13:FG BLUE : ~{FBLU}FG BLUE style~{FDEF} FG BLUE OFF +14:FG MAGENTA : ~{FMAG}FG MAGENTA style~{FDEF} FG MAGENTA OFF +15:FG CYA : ~{FCYA}FG CYAN style~{FDEF} FG CYAN OFF +16:FG WHITE : ~{FWHI}FG WHITE style~{FDEF} FG WHITE OFF +17:FG DEFAULT : ~{FDEF}FG DEFAULT style~{FDEF} FG DEFAULT OFF +18:BG BLACK : ~{BBLK}BG BLACK style~{BDEF} BG BLACK OFF +19:BG RED : ~{BRED}BG RED style~{BDEF} BG RED OFF +20:BG GREEN : ~{BGRN}BG GREEN style~{BDEF} BG GREEN OFF +21:BG BLUE : ~{BBLU}BG BLUE style~{BDEF} BG BLUE OFF +22:BG MAGENTA : ~{BMAG}BG MAGENTA style~{BDEF} BG MAGENTA OFF +23:BG CYAN : ~{BCYA}BG CYAN style~{BDEF} BG CYAN OFF +24:BG WHITE : ~{BWHI}BG WHITE style~{BDEF} BG WHITE OFF +25:BG DEFAULT : ~{BDEF}BG DEFAULT style~{BDEF} BG DEFAULT OFF +26:NORMAL SET : ~{NORM}NORMAL style~{NORM} BACK TO NORMAL +26:NOTICE SET : ~{NOTI}NOTICE style~{NORM} BACK TO NORMAL +27:WARN SET : ~{WARN}WARN style~{NORM} BACK TO NORMAL +28:ERROR SET : ~{ERRO}ERROR style~{NORM} BACK TO NORMAL +29:ADVICE SET : ~{ADVI}ADVICE style~{NORM} BACK TO NORMAL +30:QUOTE SET : ~{QUOT}QUOTE style~{NORM} BACK TO NORMAL +]] +Term.Print(toPrint, False) +Term.Print(" ~{FRED}RED~{FGRN}GREEN~{FDEF}", False) +Term.Print("BOLD->~{BLD+}~{FRED}RED~{FGRN}GREEN~{FDEF}~{BLD-}", False) + +Term.Input(Ansi.GetCursorMove(3, 1) .. "--- Hit Enter END ---", True, True) diff --git a/Examples/Ansi/requesters.hws b/Examples/Ansi/requesters.hws new file mode 100644 index 0000000..d8907f8 --- /dev/null +++ b/Examples/Ansi/requesters.hws @@ -0,0 +1,111 @@ +; ------------------------------------------------------------------- +; ANSI Example, requesters usage +; ------------------------------------------------------------------- +; Author : Fabio Falcucci (Allanon) +; License : Freeware +; Version : 1.0 +; Release : 04/11/2024 +; Dependancies : Ansi.hws +; PayPal Support : hijoe@tin.it +; Support me on Patreon! https://www.patreon.com/Allanon71 +; Github repository https://github.com/Allanon71 +; ------------------------------------------------------------------ + +; Let's include the ANSI library +@INCLUDE "../../Ansi.hws" + +; Since we are targeting console applications we don't need an Hollyweood display +@DISPLAY { Hidden = True } + +; Let's ask for the current terminal size: this works well on Linux systems but +; on Windows there are issues. +Term.GetSize() + +; Switch ANSI On and clear the screen +Term.Clear() +Term.AnsiMode = True ; By default colors are switched off you can also use + ; Ansi.Set() function for this. + +; To be able to use requester you have to create at least an app object +Local app =Term.App:New() + +; Now we can SHOW A MESSAGE on the screen +app:ShowMessage("This is some text to print in the requester's body, it will be wordwrapped since I've set the 'wordwrap' argument set to True. This is some text to print in the requester's body, it will be wordwrapped since I've set the 'wordwrap' argument set to True. This is some text to print in the requester's body, it will be wordwrapped since I've set the 'wordwrap' argument set to True.", True, True) + +; Here is an INPUT BOX +result = app:ShowInput("Here is an example of a box waiting for the user to type something.", "Type something and hit ENTER:") +Term.Clear() +Term.Print("~{NOTI}~{HOME}You have typed:~{NORM} " .. result, True, Nil, True) + +; Another type of requester is the GridView, a pretty complex structure that allows you +; to show tables using a grid, attaching an event to the 'select' action and allowing +; you to perform text search within the data set. +; --- +; Let's prepare a test data set + Local data = + { name = + { "Fabio", + "Mario", + "Pino", + "Jhon", + "Luca", + "Mary", + "Annah", + "Marcus", + "Zoe", + "Rick" }, + + surname = + { "Falcucci", + "Rossi", + "Bianchi", + "Ross", + "White", + "Mellow", + "Loomar", + "Trevis", + "Rimm", + "Koller" }, + + age = + { 48, + 41, + 30, + 58, + 18, + 34, + 32, + 55, + 19, + 41 } + } + + Local columns = + { "name", + "surname", + ">age" } ; The > sign means: align to the right + + Local headers = + { "NAME", + "SURNAME", + "AGE" } + + Local positions = + { 1, + 20, + 40 } + + Local numbers = + { [2] = "%.4f" } + + +; Now let's show the data +app:GridView(data, columns, headers, positions, + Function(d, index) + ; This is a callback function activated when the hilighted row is selected + tapp:ShowMessage( + "ITEM SELECTED\n\n" .. + d.Name[index] .. " " .. d.Surname[index] .. ", Age: " .. d.Age[index]) + EndFunction, + "*** BIG PROBLEM: commands must be confirmed with ENTER ***", + numbers) diff --git a/Examples/Ansi/term_app.hws b/Examples/Ansi/term_app.hws new file mode 100644 index 0000000..64e31e9 --- /dev/null +++ b/Examples/Ansi/term_app.hws @@ -0,0 +1,116 @@ +; ------------------------------------------------------------------- +; ANSI Example, building a basic terminal app +; ------------------------------------------------------------------- +; Author : Fabio Falcucci (Allanon) +; License : Freeware +; Version : 1.0 +; Release : 06/11/2024 +; Dependancies : Ansi.hws +; PayPal Support : hijoe@tin.it +; Support me on Patreon! https://www.patreon.com/Allanon71 +; Github repository https://github.com/Allanon71 +; ------------------------------------------------------------------ + +; Let's include the ANSI library +@INCLUDE "../../Ansi.hws" + +; Since we are targeting console applications we don't need an Hollyweood display +@DISPLAY { Hidden = True } + +; Let's ask for the current terminal size: this works well on Linux systems but +; on Windows there are issues. +Term.GetSize() + +; Switch ANSI On and clear the screen +Term.Clear() +Term.AnsiMode = True ; By default colors are switched off you can also use + ; Ansi.Set() function for this. + +; Let's create the app object +Local app =Term.App:New() + +; A terminal app is a menu driven application with some useful features to quickly +; create console applications. +; First we need to setup a few fields of the created app object: +app.name = "TERM App Example" +app.version = "1.0" +app.build = "06.11.2024" +app.copyright = "Allanon" + +; The second step is to build the menu structure of our application where you have to +; define menu items and what they do. You can nest menus and create sub-menus freely. +; The system has an authomatic history system that let you easily allow your user to +; go back to the previous menu simply typing "back" as menu command. + +; Here is an example: +app:menuAdd("Main", ; <- menu name +{ ; Each record is a menu item with the following fields: + ; command: the string the user has to type to select the menu item + ; If command is an empty string the description will be used as a header + ; description: The string printed near the command that describe the menu item. + ; If both command and description are empty strings a horizontal line + ; is drawn. + ; callback: the function to call when the menu item is selected + ; You can pass a string with another menu name to call a submenu. + { command = "", + description = "HEADER 01", + callback = Function() EndFunction }, + { command = "msg", + description = "Show a message", + callback = Function() + app:ShowMessage("Hello! This is a message box!\n\nIsn't it nice?") + EndFunction }, + { command = "noh", + description = "Nothing here!", + callback = Function() EndFunction }, + { command = "", + description = "HEADER 02", + callback = Function() EndFunction }, + { command = "prg", + description = "Test progress bar", + callback = Function() + For Local i = 1 To 100 + app:Progress("Testing the Progress method:" .. i .. "%", i, 100) + Wait(100, #MILLISECONDS) + Next + app:SetStatus("Completed! (waiting 3 seconds)") + Wait(3000, #MILLISECONDS) + EndFunction }, + { command = "aei", + description = "Another empty item", + callback = Function() EndFunction }, + { command = "sub", + description = "Call a submenu", + callback = "Sub1" }, + { command = "", description = "", callback = Function() EndFunction }, + { command = "C1", description = "Nothing here", callback = Function() EndFunction }, + { command = "C2", description = "Type 'back' to quit", callback = Function() EndFunction }, + { command = "C3", description = "followed by ENTER", callback = Function() EndFunction }, + { command = "war", description = "Warning message (bottom)", callback = Function() app:SetWarning("This is the warning line!") EndFunction }, + { command = "sta", description = "Status message (bottom)", callback = Function() app:SetStatus("This is the status line!") EndFunction }, + { command = "inf", description = "Info message (bottom)", callback = Function() app:SetInfo("This is the information line!") EndFunction }, + { command = "clr", + description = "Clear all messages", + callback = Function() + app:ClearWarning() + app:ClearStatus() + app:ClearInfo() + EndFunction }, + { command = "end", description = "Close the app", callback = Function() End EndFunction }, + + }) + + +; Defines the submenu +; This menu is called when the user selected the menu item of the main menu. +app:MenuAdd("Sub1", + { { command = "this", description = "type 'back'", callback = Function() EndFunction }, + { command = "is", description = "to return", callback = Function() EndFunction }, + { command = "a", description = "to the previous", callback = Function() EndFunction }, + { command = "sub", description = "menu", callback = Function() EndFunction }, + { command = "", description = "SEPARATOR", callback = Function() EndFunction }, + { command = "menu", description = "", callback = Function() EndFunction }, + }) + +; And now let's start the application using the first menu level! +app:Start("Main") \ No newline at end of file