Skip to content

Commit

Permalink
Adding CONSOLE object and MISC LIB
Browse files Browse the repository at this point in the history
  • Loading branch information
grymmjack committed Feb 10, 2024
1 parent 130efc1 commit 5482827
Show file tree
Hide file tree
Showing 11 changed files with 361 additions and 1 deletion.
36 changes: 36 additions & 0 deletions CONSOLE/CONSOLE.BAS
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
''
' QB64_GJ_LIB
' GRYMMJACK'S CONSOLE LIB
'
' CONSOLE object with debugging example
'
' USAGE:
' Insert '$INCLUDE:'path_to_GJ_LIB/CONSOLE/CONSOLE.BI' at the top of file
' Insert '$INCLUDE:'path_to_GJ_LIB/CONSOLE/CONSOLE.BM' at the bottom of file
'
' @author Rick Christy <grymmjack@gmail.com>
'
$IF GJ_LIB_UNIFIED_TESTING = DEFINED AND GJ_LIB_INC_BI = UNDEFINED THEN
'$INCLUDE:'../_GJ_LIB.BI'
$END IF

_TITLE "QB64_GJ_LIB CONSOLE OBJECT EXAMPLE"

PRINT "This example outputs only this message to the program window."
PRINT "To see the purpose and output look at the terminal window."

'$INCLUDE:'CONSOLE.BI'
CONSOLE.log "This is a CONSOLE.log message."

'If you are used to using console.log() you might want to use CALL to use ()'s
CALL CONSOLE.log("This is another CONSOLE.log message.")

CONSOLE.info "This is a CONSOLE.info message."
CONSOLE.warn "This is a CONSOLE.warn message."
CONSOLE.error "This is a CONSOLE.error message."

'CONSOLE.banner and CONSOLE.box take a color code argument to draw in that color
CONSOLE.banner "This is a CONSOLE.banner message.", 13
CONSOLE.box "This is a CONSOLE.box message.", 5

'$INCLUDE:'CONSOLE.BM'
59 changes: 59 additions & 0 deletions CONSOLE/CONSOLE.BI
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
''
' QB64_GJ_LIB
' GRYMMJACK'S CONSOLE LIB
'
' CONSOLE object with debugging.
'
' USAGE:
' Insert '$INCLUDE:'path_to_GJ_LIB/CONSOLE/CONSOLE.BI' at the top of file
' Insert '$INCLUDE:'path_to_GJ_LIB/CONSOLE/CONSOLE.BM' at the bottom of file
'
' @depends STRINGS/STRINGS.BI
' @author Rick Christy <grymmjack@gmail.com>
'
$LET GJ_LIB_CONSOLE_INC_BI = 1
$IF DEBUGGING = UNDEFINED THEN
$LET DEBUGGING = TRUE
$END IF
$IF DEBUGGING = TRUE THEN
$CONSOLE
$ASSERTS:CONSOLE
_CONSOLE ON
$END IF

'Maps ANSI colors to CGA colors
DIM SHARED ANSI_COLOR(0 TO 15) AS INTEGER
ANSI_COLOR%(0) = 0
ANSI_COLOR%(1) = 4
ANSI_COLOR%(2) = 2
ANSI_COLOR%(3) = 6
ANSI_COLOR%(4) = 1
ANSI_COLOR%(5) = 5
ANSI_COLOR%(6) = 3
ANSI_COLOR%(7) = 7
ANSI_COLOR%(8) = 0
ANSI_COLOR%(9) = 4
ANSI_COLOR%(10) = 2
ANSI_COLOR%(11) = 6
ANSI_COLOR%(12) = 1
ANSI_COLOR%(13) = 5
ANSI_COLOR%(14) = 3
ANSI_COLOR%(15) = 7
CONST BLACK = 0%
CONST BLUE = 1%
CONST GREEN = 2%
CONST CYAN = 3%
CONST RED = 4%
CONST MAGENTA = 5%
CONST YELLOW = 6%
CONST WHITE = 7%
CONST BRIGHT_BLACK = 8%
CONST BRIGHT_BLUE = 9%
CONST BRIGHT_GREEN = 10%
CONST BRIGHT_CYAN = 11%
CONST BRIGHT_RED = 12%
CONST BRIGHT_MAGENTA = 13%
CONST BRIGHT_YELLOW = 14%
CONST BRIGHT_WHITE = 15%

'$INCLUDE:'../STRINGS/STRINGS.BI'
117 changes: 117 additions & 0 deletions CONSOLE/CONSOLE.BM
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
''
' QB64_GJ_LIB
' GRYMMJACK'S CONSOLE LIB
'
' CONSOLE object with debugging.
'
' USAGE:
' Insert '$INCLUDE:'path_to_GJ_LIB/CONSOLE/CONSOLE.BI' at the top of file
' Insert '$INCLUDE:'path_to_GJ_LIB/CONSOLE/CONSOLE.BM' at the bottom of file
'
' @author Rick Christy <grymmjack@gmail.com>
'
$LET GJ_LIB_CONSOLE_INC_BM = 1

''
' Log a boxed message to console if DEBUGGING
' @param STRING msg message to send
' @param INTEGER kolor% color to use
'
SUB console.box(msg$, kolor%)
$IF DEBUGGING = TRUE THEN
DIM AS STRING e, color_code, lines, intensity
DIM AS INTEGER fg, afg
intensity$ = "0"
e$ = CHR$(27)
afg% = ANSI_COLOR%(kolor%)
color_code$ = e$ + "["
IF kolor% > 7 THEN intensity$ = "1"
color_code$ = color_code$ + intensity$ + ";3"
color_code$ = color_code$ + _TRIM$(STR$(afg%)) + "m"
lines$ = "+" + STRING$(LEN(msg$)+2, "-") + "+"
_ECHO color_code$ + lines$
_ECHO "| " + msg$ + " |"
_ECHO lines$ + e$ + "[0m"
$END IF
msg$ = ""
END SUB


''
' Log a banner to console if DEBUGGING
' @param STRING msg message to send
' @param INTEGER kolor% color to use
'
SUB console.banner(msg$, kolor%)
$IF DEBUGGING = TRUE THEN
DIM AS STRING e, color_code, intensity
DIM AS INTEGER fg, afg
intensity$ = "0"
e$ = CHR$(27)
afg% = ANSI_COLOR(kolor%)
msg$ = STR.replace$(msg$, "\n", CHR$(10), -1)
msg$ = STR.replace$(msg$, "\t", CHR$(9), -1)
color_code$ = e$ + "["
IF kolor% > 7 THEN intensity$ = "1"
color_code$ = color_code$ + intensity$ + ";3"
color_code$ = color_code$ + _TRIM$(STR$(afg%)) + "m"
_ECHO color_code$ + msg$ + e$ + "[0m"
$END IF
msg$ = ""
END SUB


''
' Log to console if DEBUGGING
' @param STRING msg message to send
'
SUB console.log(msg$)
$IF DEBUGGING = TRUE THEN
_ECHO msg$
$END IF
msg$ = ""
END SUB


''
' Log to console as info if DEBUGGING
' @param STRING msg message to send
'
SUB console.info(msg$)
$IF DEBUGGING = TRUE THEN
DIM AS STRING e
e$ = CHR$(27)
_ECHO e$ + "[1;36m" + msg$ + e$ + "[0m"
$END IF
msg$ = ""
END SUB


''
' Log to console as warning if DEBUGGING
' @param STRING msg message to send
'
SUB console.warn(msg$)
$IF DEBUGGING = TRUE THEN
DIM AS STRING e
e$ = CHR$(27)
_ECHO e$ + "[1;33m" + msg$ + e$ + "[0m"
$END IF
msg$ = ""
END SUB


''
' Log to console as error if DEBUGGING
' @param STRING msg message to send
'
SUB console.error(msg$)
$IF DEBUGGING = TRUE THEN
DIM AS STRING e
e$ = CHR$(27)
_ECHO e$ + "[1;31m" + msg$ + e$ + "[0m"
$END IF
msg$ = ""
END SUB

'$INCLUDE:'../STRINGS/STRINGS.BM'
51 changes: 51 additions & 0 deletions CONSOLE/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# [QB64_GJ_LIB](../README.md)
## GRYMMJACK'S CONSOLE LIBRARY

> CONSOLE object with debugging.
The idea of this library and is to provide a elegant and simple way to include
console output conditionally by setting one `$LET DEBUGGING = TRUE` in your code
and if you do not have this set, just including the library will enable it by
default. This will let you simply include the library and get busy using it.

So what does it do? Well it provides QB64PE with functions similar to that of
a web developer is spoiled by with a console object to use.

In this case, we simply send strings to the console terminal of various colors
with ANSI codes.

I've extended the original idea of the web console.log a bit with a banner, and
a box utility which is handy for seeing output separated from the rest.

See the example and screenshot for more information.


## WHAT'S IN THE LIBRARY
| SUB / FUNCTION | NOTES |
|----------------|-------|
| CONSOLE.log | Log to console if DEBUGGING |
| CONSOLE.info | Log to console as info if DEBUGGING |
| CONSOLE.warn | Log to console as warning if DEBUGGING |
| CONSOLE.error | Log to console as error if DEBUGGING |
| CONSOLE.banner | Log a banner to console if DEBUGGING |
| CONSOLE.box | Log a boxed message to console if DEBUGGING |



### USAGE for CONSOLE LIB (separately)
```basic
'Insert at top of code:
'$INCLUDE:'path_to_GJ_LIB/CONSOLE/CONSOLE.BM'
'...your code here...
'Insert at bottom of code:
'$INCLUDE:'path_to_GJ_LIB/CONSOLE/CONSOLE.BM'
```



### EXAMPLE
> Screenshot of output from [CONSOLE.BAS](CONSOLE.BAS)
![](CONSOLE.png)
13 changes: 13 additions & 0 deletions MISC/MISC.BI
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
''
' QB64_GJ_LIB
' GRYMMJACK'S MISC LIB
'
' Miscellaneous helpful functions and subs that don't fit anywhere else :)
'
' USAGE:
' Insert '$INCLUDE:'path_to_GJ_LIB/MISC/MISC.BI' at the top of file
' Insert '$INCLUDE:'path_to_GJ_LIB/MISC/MISC.BM' at the bottom of file
'
' @author Rick Christy <grymmjack@gmail.com>
'
$LET GJ_LIB_MISC_INC_BI = 1
42 changes: 42 additions & 0 deletions MISC/MISC.BM
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
''
' QB64_GJ_LIB
' GRYMMJACK'S MISC LIB
'
' Miscellaneous helpful functions and subs that don't fit anywhere else :)
'
' USAGE:
' Insert '$INCLUDE:'path_to_GJ_LIB/MISC/MISC.BI' at the top of file
' Insert '$INCLUDE:'path_to_GJ_LIB/MISC/MISC.BM' at the bottom of file
'
' @author Rick Christy <grymmjack@gmail.com>
'
$LET GJ_LIB_MISC_INC_BM = 1


''
' Returns a number rounded to a fixed number of decimal places
' @param DOUBLE num Number to round
' @param INTEGER places number of places to round to
' @return DOUBLE rounded number
'
FUNCTION num_fix#(num#, places%)
DIM AS DOUBLE multiplier
DIM AS STRING mult_str
mult_str$ = _TRIM$("1" + STRING$(places%, "0"))
multiplier# = VAL(mult_str$)
num_fix# = FIX(num# * multiplier# + SGN(num#) * .5) / multiplier#
END FUNCTION


''
' Clamps a INTEGER between a minimum and maximum range
' @param INTEGER num number to clamp
' @param INTEGER min minimum allowed
' @param INTEGER max maximum allowed
' @return INTEGER clamped number
'
FUNCTION clamp_int%(num%, min%, max%)
IF num% > max% THEN num% = max%
IF num% < min% THEN num% = min%
clamp_int% = num%
END FUNCTION
22 changes: 22 additions & 0 deletions MISC/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# [QB64_GJ_LIB](../README.md)
## GRYMMJACK'S MISC LIBRARY

> Miscellaneous helpful functions and subs that don't fit anywhere else :)


## WHAT'S IN THE LIBRARY
| SUB / FUNCTION | NOTES |
|----------------|-------|
| num_fix | Returns a number rounded to a fixed number of decimal places |
| clamp_int | Clamps a INTEGER between a minimum and maximum range |



### USAGE for MISC LIB (separately)
```basic
'...your code here...
'Insert at bottom of code:
'$INCLUDE:'path_to_GJ_LIB/MISC/MISC.BM'
```
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ in it's current state.
| [SYS](SYS/README.md) | System stuff | Contains misc. helpful utils/tools |
| [VECT2D](VECT2D/README.md) | 2D Vector support | Thanks to William Barnes and Evan Shortiss |
| [ASEPRITE](ASEPRITE/README.md) | Adds ASEPRITE support to QB64 | See https://www.aseprite.org |
| [CONSOLE](CONSOLE/README.md) | CONSOLE object with debugging. |
| [MISC](MISC/README.md) | Miscellaneous helpful functions and subs that don't fit anywhere else :) |



Expand Down
6 changes: 6 additions & 0 deletions _GJ_LIB.BI
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ $END IF
$IF GJ_LIB_VECT2D_INC_BI = UNDEFINED THEN
'$INCLUDE:'VECT2D/VECT2D.BI'
$END IF
$IF GJ_LIB_CONSOLE_INC_BI = UNDEFINED THEN
'$INCLUDE:'CONSOLE/CONSOLE.BI'
$END IF
$IF GJ_LIB_MISC_INC_BI = UNDEFINED THEN
'$INCLUDE:'MISC/MISC.BI'
$END IF
6 changes: 6 additions & 0 deletions _GJ_LIB.BM
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,9 @@ $END IF
$IF GJ_LIB_VECT2D_INC_BM = UNDEFINED THEN
'$INCLUDE:'VECT2D/VECT2D.BM'
$END IF
$IF GJ_LIB_CONSOLE_INC_BM = UNDEFINED THEN
'$INCLUDE:'CONSOLE/CONSOLE.BM'
$END IF
$IF GJ_LIB_MISC_INC_BM = UNDEFINED THEN
'$INCLUDE:'MISC/MISC.BM'
$END IF
8 changes: 7 additions & 1 deletion _GJ_LIB_COMMON.BI
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ $END IF
$IF FALSE = UNDEFINED AND TRUE = UNDEFINED THEN
$LET TRUE = TRUE
CONST FALSE = 0 : CONST TRUE = NOT FALSE
$END IF
$END IF

$IF WIN THEN
CONST SLASH$ = "\"
$ELSE
CONST SLASH$ = "/"
$END IF

0 comments on commit 5482827

Please sign in to comment.