ConsoleTools - set of macros and procedures for console applications.

Share your advanced PureBasic knowledge/code with the community.
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

ConsoleTools - set of macros and procedures for console applications.

Post by Quin »

I just spun this up this evening. It's a small set of macros and procedures for making console applications easier in PB. Comments explain each function, as well as its license.
Enjoy :)

Code: Select all

; ConsoleTools
; Copyright (c) 2025 by Quin.
; Public domain.
; Credit is greatly appreciated, but not required.

DeclareModule ConsoleTools
  EnableExplicit
  
  ;{ Public Macros
  ; Exits the application and displays an error message.
  Macro Die(_ErrorText)
    ConsoleError(_ErrorText)
    End 1
  EndMacro
  
  ; Exits the application, printing a message before doing so.
  Macro Exit(_Message)
    PrintN(_Message)
    End
  EndMacro
  
  ; Displays the text "Press any key to continue . . ." and waits for keyboard input, like the pause command in batch scripts.
  Macro Pause
    PrintN("Press any key to continue . . .")
    Repeat
      Delay(10)
    Until Inkey() <> "" Or RawKey()
  EndMacro
  ;}
  
  ;{ Declares
  Declare.i Menu(Prompt$, Items$)
  Declare$ Prompt(Message$)
  ;}
EndDeclareModule

Module ConsoleTools
  EnableExplicit
  
  ;{ Public Procedures
  ; Displays a menu to the user, letting them choose an option by typing a number. Menu items are separated by the vertical bar (|).
  Procedure.i Menu(Prompt$, Items$)
    Protected.i i, Count
    Count = CountString(Items$, "|")
    Protected Dim Items.s(Count)
    For i = 0 To Count
      Items(i) = StringField(Items$, i + 1, "|")
    Next
    PrintN(Prompt$)
    For i = 0 To Count
      PrintN(Str(i + 1) + " = " + Items(i))
    Next
    ProcedureReturn Val(Input())
  EndProcedure
  
  ; Prints a message and reads a string from stdin.
  Procedure$ Prompt(Message$)
    Print(Message$)
    ProcedureReturn Input()
  EndProcedure
EndModule

CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
  
  UseModule ConsoleTools
  
  OpenConsole("Test")
  Define Name$ = Prompt("What is your name?")
  If Name$ = ""
    Die("You must enter a name")
  EndIf
  Select Menu("Okay " + Name$ + ", do you love PureBasic?", "Yes|No|Dunno")
    Case 1
      PrintN("Glad to hear it!")
    Case 2
      PrintN("Oh no! 😦")
    Case 3
      PrintN("Okay then. 😕")
  EndSelect
  Pause
  Exit("Goodbye!")
CompilerEndIf
Little John
Addict
Addict
Posts: 4786
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: ConsoleTools - set of macros and procedures for console applications.

Post by Little John »

I just stumbled across this nice contribution. Small but beautiful. :-) Thanks!
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: ConsoleTools - set of macros and procedures for console applications.

Post by Quin »

Little John,
Glad to hear you found it useful, and thanks! :)
Post Reply