1
0

Updated code headers, demo and added new escape characters and macros

This commit is contained in:
Herwin Bozet (NibblePoker) 2021-07-01 22:31:17 +02:00
parent 83f1a1f3e1
commit 332f61e366
4 changed files with 101 additions and 34 deletions

View File

@ -3,13 +3,10 @@
; Name: AinsiEscapeCode.pbi ; Name: AinsiEscapeCode.pbi
; Version: N/A ; Version: N/A
; Author: Herwin Bozet ; Author: Herwin Bozet
;
; ==- Compatibility -=============================
; Compiler version: PureBasic 5.70 (x86/x64)
; Operating system: Windows 10 21H1 (Previous versions untested)
; ;
; ==- Links & License -=========================== ; ==- Links & License -===========================
; License: Unlicense ; License: Unlicense
; GitHub: https://github.com/aziascreations/PB-ConsoleHelpers
;} ;}
;- Module Declaration ;- Module Declaration
@ -46,13 +43,22 @@ DeclareModule AinsiEscapeCode
#CPL$ = "F" ; Cursor Previous Line by n #CPL$ = "F" ; Cursor Previous Line by n
;-> Screen Buffer
#SU$ = "S" ; Scroll text up by <n>. Also known as pan down, new lines fill in from the bottom of the screen.
#SD$ = "T" ; croll down by <n>. Also known as pan up, new lines fill in from the top of the screen.
;-> Buffer ? ;-> Buffer ?
#ED$ = "J" ; Erase in Display #ED$ = "J" ; Erase in Display
#ED_0 = 0 ; 0 from cursor to end of screen #ED_0 = 0 ; Clear from cursor to end of screen
#ED_1 = 1 ; 1 from cursor to beginning of the screen #ED_1 = 1 ; Clear from cursor to beginning of the screen
#ED_2 = 2 ; 2 clear entire screen (May move to 0:0) #ED_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_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 ;-> Queries
#DECXCPR$ = "6n" ; Emit the cursor position as: ESC [ <r> ; <c> R Where <r> = cursor row and <c> = cursor column #DECXCPR$ = "6n" ; Emit the cursor position as: ESC [ <r> ; <c> R Where <r> = cursor row and <c> = cursor column

View File

@ -3,14 +3,10 @@
; Name: Console.pbi ; Name: Console.pbi
; Version: N/A ; Version: N/A
; Author: Herwin Bozet ; Author: Herwin Bozet
;
; ==- Compatibility -=============================
; Compiler version: PureBasic 5.70 (x86/x64)
; Operating system: Windows 10 21H1 (Previous versions untested)
; ;
; ==- Links & License -=========================== ; ==- Links & License -===========================
; License: Unlicense ; License: Unlicense
; MSDN: https://docs.microsoft.com/en-us/windows/console/console-functions ; GitHub: https://github.com/aziascreations/PB-ConsoleHelpers
;} ;}
;- Module declaration ;- Module declaration

View File

@ -5,6 +5,7 @@
; ;
; ==- Links & License -=========================== ; ==- Links & License -===========================
; License: Unlicense ; License: Unlicense
; GitHub: https://github.com/aziascreations/PB-ConsoleHelpers
;} ;}
;- Compiler Directives ;- Compiler Directives
@ -20,7 +21,7 @@ CompilerEndIf
;- Code ;- Code
; Safely openning the console ;-> Safely openning the console
If OpenConsole("My first title") If OpenConsole("My first title")
If Not VirtualTerminal::EnableVirtualTerminalProcessing() If Not VirtualTerminal::EnableVirtualTerminalProcessing()
VirtualTerminal::WriteErrorN("Unable to activate virtual terminal processing, output is likely piped !") VirtualTerminal::WriteErrorN("Unable to activate virtual terminal processing, output is likely piped !")
@ -32,37 +33,77 @@ Else
End 1 End 1
EndIf EndIf
; Clearing the console
;-> Clearing the console
VirtualTerminal::ClearDisplay(AinsiEscapeCode::#ED_2) ; 2 or +ClearDisplayFull() also works just fine VirtualTerminal::ClearDisplay(AinsiEscapeCode::#ED_2) ; 2 or +ClearDisplayFull() also works just fine
VirtualTerminal::CursorTo(0, 0) VirtualTerminal::CursorTo(0, 0)
; Intro
;-> Intro
VirtualTerminal::WriteOutputN("-===- Welcome to the 'Basic' demo for 'VirtualTerminal' -===-") 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...") VirtualTerminal::WriteOutputN("Press enter to start...")
Input() Input()
; Basic info
;-> Basic info
VirtualTerminal::WriteOutputN("-=- 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...") VirtualTerminal::WriteOutputN("Press enter to continue...")
Input() Input()
; Title demo
;-> Title demo
VirtualTerminal::WriteOutputN("-=- Title -=-") VirtualTerminal::WriteOutputN("-=- Title -=-")
VirtualTerminal::WriteOutputN("Press enter to change the title...") VirtualTerminal::WriteOutputN("Press enter to change the title...")
Input() Input()
VirtualTerminal::SetWindowTitle("My new title") 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 -=-") VirtualTerminal::WriteOutputN("-=- End of the demo -=-")
Print("Press enter to clear the terminal and exit...") Print("Press enter to clear the terminal and exit...")
Input() Input()
VirtualTerminal::ClearDisplayFull() VirtualTerminal::ClearDisplayFull()
VirtualTerminal::CursorTo(0, 0) VirtualTerminal::CursorTo(0, 0)
; Quitting safely
;-> Quitting safely
VirtualTerminal::DisableVirtualTerminalProcessing() VirtualTerminal::DisableVirtualTerminalProcessing()
CloseConsole() CloseConsole()
End 0 End 0

View File

@ -3,13 +3,10 @@
; Name: VirtualTerminal.pbi ; Name: VirtualTerminal.pbi
; Version: N/A ; Version: N/A
; Author: Herwin Bozet ; Author: Herwin Bozet
;
; ==- Compatibility -=============================
; Compiler version: PureBasic 5.70 (x86/x64)
; Operating system: Windows 10 21H1 (Previous versions untested)
; ;
; ==- Links & License -=========================== ; ==- Links & License -===========================
; License: Unlicense ; License: Unlicense
; GitHub: https://github.com/aziascreations/PB-ConsoleHelpers
;} ;}
;- Compiler directives ;- Compiler directives
@ -25,6 +22,8 @@ DeclareModule VirtualTerminal
;-> > Console.pbi macros ;-> > Console.pbi macros
;Macro CursorPosition : Console::CursorPosition : EndMacro
Macro IsConsoleApp() : Console::#IsConsoleApp : EndMacro Macro IsConsoleApp() : Console::#IsConsoleApp : EndMacro
Macro GetOutputHandle() : Console::GetOutputHandle() : 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 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 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 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 CursorSave() : VirtualTerminal::WriteOutput(#ESC$ + AinsiEscapeCode::#DECSC$) : EndMacro
Macro CursorRestore() : VirtualTerminal::WriteOutput(#ESC$ + AinsiEscapeCode::#DECSR$) : 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 ClearDisplay(Mode = AinsiEscapeCode::#ED_0) : VirtualTerminal::WriteOutput(#ESC$ + AinsiEscapeCode::#CSI$ + Str(Mode) + AinsiEscapeCode::#ED$) : EndMacro
Macro ClearDisplayToEnd() : VirtualTerminal::ClearDisplay(AinsiEscapeCode::#ED_0) : EndMacro Macro ClearDisplayToEnd() : VirtualTerminal::ClearDisplay(AinsiEscapeCode::#ED_0) : EndMacro
Macro ClearDisplayToStart() : VirtualTerminal::ClearDisplay(AinsiEscapeCode::#ED_1) : EndMacro Macro ClearDisplayToStart() : VirtualTerminal::ClearDisplay(AinsiEscapeCode::#ED_1) : EndMacro
Macro ClearDisplayFull() : VirtualTerminal::ClearDisplay(AinsiEscapeCode::#ED_2) : EndMacro Macro ClearDisplayFull() : VirtualTerminal::ClearDisplay(AinsiEscapeCode::#ED_2) : EndMacro
Macro ClearDisplayAbsolute() : VirtualTerminal::ClearDisplay(AinsiEscapeCode::#ED_3) : 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 ;-> > > Window
Macro SetWindowTitleAndIcon(Title) : VirtualTerminal::WriteOutput(#ESC$ + AinsiEscapeCode::#OSC$ + "0;" + Left(Title, 255) + AinsiEscapeCode::#BEL$) : EndMacro 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 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) 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 GetTerminalWidth(RestorePosition.b = #True)
Declare.i GetTerminalHeight(RestorePosition.b = #True) Declare.i GetTerminalHeight(RestorePosition.b = #True)
EndDeclareModule EndDeclareModule
@ -199,15 +211,27 @@ CompilerIf #PB_Compiler_IsMainFile
End 1 End 1
EndIf EndIf
Define TerminalWidth.i = VirtualTerminal::GetTerminalWidth(#False)
Define TerminalHeight.i = VirtualTerminal::GetTerminalHeight(#False)
VirtualTerminal::ClearDisplay(AinsiEscapeCode::#ED_2) VirtualTerminal::ClearDisplay(AinsiEscapeCode::#ED_2)
VirtualTerminal::CursorTo(0, 0) 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...") VirtualTerminal::CursorTo(0, TerminalHeight - 2)
Debug Input()
;FreeMemory(*Buffer) 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() VirtualTerminal::DisableVirtualTerminalProcessing()
CloseConsole() CloseConsole()