[Gambas-user] My module for simple console output

Rolf-Werner Eilert eilert-sprachen at ...221...
Thu Jun 3 10:24:34 CEST 2010


Referring to another thread I had here, I'd just like to show my 
(simple) solution for console output. I was looking for some attributes 
and CLS and so on. If you want to have smarter solutions, there is 
ncurses, but for the quick and dirty project my way might do.

All these consoles in Linux simulate at least a VT100 terminal which 
knows a set of simple escape codes to control the output. I found this 
wonderful site http://ascii-table.com/ansi-escape-sequences-vt-100.php 
having everything I needed.

What I made is a module named "Screen" with the code quoted hereunder. 
In FMain I made a loop with LINE INPUT and a simple SELECT CASE for the 
user to type in commands for testing purposes. Within this SELECT CASE 
thing, the returned strings are printed and so on, rather simple.

This is not a complete application yet. What I miss is a way to read 
single keystrokes instantly, but that's tricky as Benoit said.

So this is my Screen module:

' Gambas module file

PRIVATE esc AS String = Chr$(27) & "["

PUBLIC CONST Black AS String = "0"
PUBLIC CONST Red AS String = "1"
PUBLIC CONST Green AS String = "2"
PUBLIC CONST Yellow AS String = "3"
PUBLIC CONST Blue AS String = "4"
PUBLIC CONST Magenta AS String = "5"
PUBLIC CONST Cyan AS String = "6"
PUBLIC CONST White AS String = "7"

PUBLIC CONST Foreground AS String = "3"
PUBLIC CONST Background AS String = "4"


PUBLIC FUNCTION Cls() AS String

   RETURN esc & "2J" & esc & "H"

END

PUBLIC FUNCTION Normal() AS String

   RETURN esc & "0m"

END

PUBLIC FUNCTION Bold() AS String

   RETURN esc & "1m"

END

PUBLIC FUNCTION Gray() AS String

   RETURN esc & "2m"

END


PUBLIC FUNCTION Underline() AS String

   RETURN esc & "4m"

END

PUBLIC FUNCTION Blink() AS String

   RETURN esc & "5m"

END


PUBLIC FUNCTION Reverse() AS String

   RETURN esc & "7m"

END


PUBLIC FUNCTION MoveTo(row AS Integer, col AS Integer) AS String

   IF row < 1 THEN row = 1
   IF row > 25 THEN row = 25
   IF col < 1 THEN col = 1
   IF col > 80 THEN col = 80

   RETURN esc & CStr(row) & ";" & CStr(col) & "f"

END


PUBLIC FUNCTION Color(FgBg AS String, Colorvalue AS String) AS String

   RETURN esc & FgBg & Colorvalue & "m"

END


First trials with it show it's working, so if you can use it make the 
best out of it and tell me what you think :-)

Regards

Rolf




More information about the User mailing list