From 332f61e366acd2775bb6ee70c2e8148c540981f2 Mon Sep 17 00:00:00 2001 From: Herwin Date: Thu, 1 Jul 2021 22:31:17 +0200 Subject: [PATCH] Updated code headers, demo and added new escape characters and macros --- AinsiEscapeCode.pbi | 22 +++++++----- Console.pbi | 6 +--- Examples/VirtualTerminal/Basic.pb | 59 ++++++++++++++++++++++++++----- VirtualTerminal.pbi | 48 ++++++++++++++++++------- 4 files changed, 101 insertions(+), 34 deletions(-) diff --git a/AinsiEscapeCode.pbi b/AinsiEscapeCode.pbi index 44158f0..087ce9a 100644 --- a/AinsiEscapeCode.pbi +++ b/AinsiEscapeCode.pbi @@ -3,13 +3,10 @@ ; Name: AinsiEscapeCode.pbi ; Version: N/A ; Author: Herwin Bozet -; -; ==- Compatibility -============================= -; Compiler version: PureBasic 5.70 (x86/x64) -; Operating system: Windows 10 21H1 (Previous versions untested) ; ; ==- Links & License -=========================== ; License: Unlicense +; GitHub: https://github.com/aziascreations/PB-ConsoleHelpers ;} ;- Module Declaration @@ -46,13 +43,22 @@ DeclareModule AinsiEscapeCode #CPL$ = "F" ; Cursor Previous Line by n + ;-> Screen Buffer + #SU$ = "S" ; Scroll text up by . Also known as pan down, new lines fill in from the bottom of the screen. + #SD$ = "T" ; croll down by . Also known as pan up, new lines fill in from the top of the screen. + + ;-> Buffer ? #ED$ = "J" ; Erase in Display - #ED_0 = 0 ; 0 from cursor to end of screen - #ED_1 = 1 ; 1 from cursor to beginning of the screen - #ED_2 = 2 ; 2 clear entire screen (May move to 0:0) - #ED_3 = 3 ; 3 clear entire screen and lines saved in the scrollback buffer. (Does not work on Windows as of 21H1 in Windows Terminal) + #ED_0 = 0 ; Clear from cursor to end of screen + #ED_1 = 1 ; Clear from cursor to beginning of the screen + #ED_2 = 2 ; Clear entire screen (May move to 0:0) + #ED_3 = 3 ; Clear entire screen and lines saved in the scrollback buffer. (Does not work on Windows as of 21H1 in Windows Terminal) + #EL$ = "K" ; Erase in Line + #EL_0 = 0 ; Clear from cursor to the end of the line. + #EL_1 = 1 ; Clear from cursor to beginning of the line. + #EL_2 = 2 ; Clear entire line. ;-> Queries #DECXCPR$ = "6n" ; Emit the cursor position as: ESC [ ; R Where = cursor row and = cursor column diff --git a/Console.pbi b/Console.pbi index d94dffa..55409b3 100644 --- a/Console.pbi +++ b/Console.pbi @@ -3,14 +3,10 @@ ; Name: Console.pbi ; Version: N/A ; Author: Herwin Bozet -; -; ==- Compatibility -============================= -; Compiler version: PureBasic 5.70 (x86/x64) -; Operating system: Windows 10 21H1 (Previous versions untested) ; ; ==- Links & License -=========================== ; License: Unlicense -; MSDN: https://docs.microsoft.com/en-us/windows/console/console-functions +; GitHub: https://github.com/aziascreations/PB-ConsoleHelpers ;} ;- Module declaration diff --git a/Examples/VirtualTerminal/Basic.pb b/Examples/VirtualTerminal/Basic.pb index e70dd20..a2aac11 100644 --- a/Examples/VirtualTerminal/Basic.pb +++ b/Examples/VirtualTerminal/Basic.pb @@ -5,6 +5,7 @@ ; ; ==- Links & License -=========================== ; License: Unlicense +; GitHub: https://github.com/aziascreations/PB-ConsoleHelpers ;} ;- Compiler Directives @@ -20,7 +21,7 @@ CompilerEndIf ;- Code -; Safely openning the console +;-> Safely openning the console If OpenConsole("My first title") If Not VirtualTerminal::EnableVirtualTerminalProcessing() VirtualTerminal::WriteErrorN("Unable to activate virtual terminal processing, output is likely piped !") @@ -32,37 +33,77 @@ Else End 1 EndIf -; Clearing the console + +;-> Clearing the console VirtualTerminal::ClearDisplay(AinsiEscapeCode::#ED_2) ; 2 or +ClearDisplayFull() also works just fine VirtualTerminal::CursorTo(0, 0) -; Intro + +;-> Intro VirtualTerminal::WriteOutputN("-===- Welcome to the 'Basic' demo for 'VirtualTerminal' -===-") + +VirtualTerminal::WriteOutputN("This program serves as a short demo of the 'VirtualTerminal' include for PureBasic.") +VirtualTerminal::WriteOutputN("This include and its module aim to bring the functionalities of the escape character sequences"+ + " to PureBasic without using any OS-specific API.") +VirtualTerminal::WriteOutputN("") +VirtualTerminal::WriteOutputN("Some features may not work properly on some third-party consoles such as ConEmu.") +VirtualTerminal::WriteOutputN("The 'GetTerminalWidth()' and 'GetTerminalHeight()' procedure tend to scroll the viewport on them.") +VirtualTerminal::WriteOutputN("") VirtualTerminal::WriteOutputN("Press enter to start...") Input() -; Basic info + +;-> Basic info VirtualTerminal::WriteOutputN("-=- Basic info -=-") -VirtualTerminal::WriteOutputN("Terminal size: "+Str(VirtualTerminal::GetTerminalWidth())+"x"+Str(VirtualTerminal::GetTerminalHeight())) +Define TerminalWidth.i = VirtualTerminal::GetTerminalWidth() +Define TerminalHeight.i = VirtualTerminal::GetTerminalHeight() +VirtualTerminal::WriteOutputN("Terminal size: "+Str(TerminalWidth)+"x"+Str(TerminalHeight)) VirtualTerminal::WriteOutputN("Press enter to continue...") Input() -; Title demo + +;-> Title demo VirtualTerminal::WriteOutputN("-=- Title -=-") VirtualTerminal::WriteOutputN("Press enter to change the title...") Input() VirtualTerminal::SetWindowTitle("My new title") -; ??? -; End +;-> Dynamic UI demo +VirtualTerminal::WriteOutputN("-=- Dynamic UI -=-") +VirtualTerminal::WriteOutputN("Press enter to start...") +Input() + +Define Percentage.i = 0 + +Repeat + Define ProgressBarSize.i = Round((TerminalWidth) * (Percentage / 100), #PB_Round_Down) + + Define OriginalCursorPosition.Console::CursorPosition + VirtualTerminal::GetCursorPosition(@OriginalCursorPosition) + + VirtualTerminal::WriteOutput("["+RSet("", ProgressBarSize, "#")+RSet("", TerminalWidth - ProgressBarSize - 2, " ")+"]") + VirtualTerminal::CursorTo(0, OriginalCursorPosition\Y) + + Percentage = Percentage + 1 + Delay(15) +Until Percentage = 100 + +VirtualTerminal::CursorDownBy(2) + + +;-> ??? + + +;-> Finishing VirtualTerminal::WriteOutputN("-=- End of the demo -=-") Print("Press enter to clear the terminal and exit...") Input() VirtualTerminal::ClearDisplayFull() VirtualTerminal::CursorTo(0, 0) -; Quitting safely + +;-> Quitting safely VirtualTerminal::DisableVirtualTerminalProcessing() CloseConsole() End 0 diff --git a/VirtualTerminal.pbi b/VirtualTerminal.pbi index 881233d..11c112c 100644 --- a/VirtualTerminal.pbi +++ b/VirtualTerminal.pbi @@ -3,13 +3,10 @@ ; Name: VirtualTerminal.pbi ; Version: N/A ; Author: Herwin Bozet -; -; ==- Compatibility -============================= -; Compiler version: PureBasic 5.70 (x86/x64) -; Operating system: Windows 10 21H1 (Previous versions untested) ; ; ==- Links & License -=========================== ; License: Unlicense +; GitHub: https://github.com/aziascreations/PB-ConsoleHelpers ;} ;- Compiler directives @@ -25,6 +22,8 @@ DeclareModule VirtualTerminal ;-> > Console.pbi macros + ;Macro CursorPosition : Console::CursorPosition : EndMacro + Macro IsConsoleApp() : Console::#IsConsoleApp : EndMacro Macro GetOutputHandle() : Console::GetOutputHandle() : EndMacro @@ -66,20 +65,32 @@ DeclareModule VirtualTerminal Macro CursorToColumn(ColumnNumber) : VirtualTerminal::WriteOutput(#ESC$ + AinsiEscapeCode::#CSI$ + Str(ColumnNumber) + AinsiEscapeCode::#CHA$) : EndMacro Macro CursorToLine(LineNumber) : VirtualTerminal::WriteOutput(#ESC$ + AinsiEscapeCode::#CSI$ + Str(LineNumber) + AinsiEscapeCode::#VPA$) : EndMacro - Macro CursorTo(X, Y) : VirtualTerminal::WriteOutput(#ESC$ + AinsiEscapeCode::#CSI$ + Str(Y) + ";" + Str(X) + AinsiEscapeCode::#CUP$) : EndMacro - Macro CursorFlip() : VirtualTerminal::WriteOutput(#ESC$ + AinsiEscapeCode::#RI$) : EndMacro Macro CursorSave() : VirtualTerminal::WriteOutput(#ESC$ + AinsiEscapeCode::#DECSC$) : EndMacro Macro CursorRestore() : VirtualTerminal::WriteOutput(#ESC$ + AinsiEscapeCode::#DECSR$) : EndMacro - ;-> > > Others + ; Does not appear to work properly in cmd.exe + Macro CursorFlip() : VirtualTerminal::WriteOutput(#ESC$ + AinsiEscapeCode::#RI$) : EndMacro + + ;-> > > Screen Buffer + Macro ScrollUpBy(Amount = 1) : VirtualTerminal::WriteOutput(#ESC$ + AinsiEscapeCode::#CSI$ + Str(Amount) + AinsiEscapeCode::#SU$) : EndMacro + Macro ScrollDownBy(Amount = 1) : VirtualTerminal::WriteOutput(#ESC$ + AinsiEscapeCode::#CSI$ + Str(Amount) + AinsiEscapeCode::#SD$) : EndMacro + Macro ScrollUp() : VirtualTerminal::ScrollUpBy() : EndMacro + Macro ScrollDown() : VirtualTerminal::ScrollDownBy() : EndMacro + + ;-> > > Others ? Macro ClearDisplay(Mode = AinsiEscapeCode::#ED_0) : VirtualTerminal::WriteOutput(#ESC$ + AinsiEscapeCode::#CSI$ + Str(Mode) + AinsiEscapeCode::#ED$) : EndMacro Macro ClearDisplayToEnd() : VirtualTerminal::ClearDisplay(AinsiEscapeCode::#ED_0) : EndMacro Macro ClearDisplayToStart() : VirtualTerminal::ClearDisplay(AinsiEscapeCode::#ED_1) : EndMacro Macro ClearDisplayFull() : VirtualTerminal::ClearDisplay(AinsiEscapeCode::#ED_2) : EndMacro Macro ClearDisplayAbsolute() : VirtualTerminal::ClearDisplay(AinsiEscapeCode::#ED_3) : EndMacro + Macro ClearLineDisplay(Mode = AinsiEscapeCode::#EL_0) : VirtualTerminal::WriteOutput(#ESC$ + AinsiEscapeCode::#CSI$ + Str(Mode) + AinsiEscapeCode::#EL$) : EndMacro + Macro ClearLineToEnd() : VirtualTerminal::ClearLineDisplay(AinsiEscapeCode::#EL_0) : EndMacro + Macro ClearLineToStart() : VirtualTerminal::ClearLineDisplay(AinsiEscapeCode::#EL_1) : EndMacro + Macro ClearLineFull() : VirtualTerminal::ClearLineDisplay(AinsiEscapeCode::#EL_2) : EndMacro + ;-> > > Window Macro SetWindowTitleAndIcon(Title) : VirtualTerminal::WriteOutput(#ESC$ + AinsiEscapeCode::#OSC$ + "0;" + Left(Title, 255) + AinsiEscapeCode::#BEL$) : EndMacro Macro SetWindowTitle(Title) : VirtualTerminal::WriteOutput(#ESC$ + AinsiEscapeCode::#OSC$ + "2;" + Left(Title, 255) + AinsiEscapeCode::#BEL$) : EndMacro @@ -89,6 +100,7 @@ DeclareModule VirtualTerminal Declare.b GetCursorPosition(*CursorPosition.Console::CursorPosition, TimeoutMs.i = 1) + ; May mess up some terminals (ConEmu with cmd doesn't like it at all...) Declare.i GetTerminalWidth(RestorePosition.b = #True) Declare.i GetTerminalHeight(RestorePosition.b = #True) EndDeclareModule @@ -199,15 +211,27 @@ CompilerIf #PB_Compiler_IsMainFile End 1 EndIf + Define TerminalWidth.i = VirtualTerminal::GetTerminalWidth(#False) + Define TerminalHeight.i = VirtualTerminal::GetTerminalHeight(#False) + VirtualTerminal::ClearDisplay(AinsiEscapeCode::#ED_2) VirtualTerminal::CursorTo(0, 0) - PrintN("-==- Title Bar -==-") - Print("Size: "+VirtualTerminal::GetTerminalWidth()+"x"+VirtualTerminal::GetTerminalHeight()) + VirtualTerminal::WriteOutput(RSet("", TerminalWidth, "#")) + VirtualTerminal::WriteOutput(LSet("# Application Title", TerminalWidth-1) + "#") + VirtualTerminal::WriteOutput(RSet("", TerminalWidth, "#")) - ;Print("Press enter key to exit...") - Debug Input() - ;FreeMemory(*Buffer) + VirtualTerminal::CursorTo(0, TerminalHeight - 2) + + VirtualTerminal::WriteOutput(RSet("", TerminalWidth, "#")) + VirtualTerminal::WriteOutput(LSet("# Footer text", TerminalWidth-1) + "#") + VirtualTerminal::WriteOutput(RSet("", TerminalWidth, "#")) + VirtualTerminal::CursorTo(0, 4) + + + + Print("Press enter key to exit...") + Input() VirtualTerminal::DisableVirtualTerminalProcessing() CloseConsole()