Page 1 of 1

EnableGraphicalConsole() for Linux (and MAC)

Posted: Sun Jan 17, 2010 10:44 am
by infratec
Hi,

I had to write a small console program in Linux.
At the end I used 'C' with my own escape sequences for the terminal.
(Since ncurses forms takes to long to bring it to work correct)

I was wondering, why the 3 'graphic' commands of PB are not available in Linux,
since we only have to 'print' some strings to have them work:

ClearConsole() : Chr(27) + "[2J"
ConsoleColor(f, b) : Chr(27) + "[" + str(f + 40) + ";" + str(b + 30) + "m"
ConsoleLocate(x, y) : Chr(27) + "[" + str(x) + ";" + str(y) + "f"

Not tested yet, but it will work.

for more informations look here:
http://ascii-table.com/ansi-escape-sequences.php

Bernd

Re: EnableGraphicalConsole() for Linux (and MAC)

Posted: Sun Jan 17, 2010 11:08 am
by infratec
Hi,

I just checked it:

Code: Select all

Procedure T_ConsoleClear()
 Print(Chr(27) + "[2J")
EndProcedure

Procedure T_ConsoleLocate(x, y)
 Print(Chr(27) + "[" + Str(y) + ";" + Str(x) + "f")
EndProcedure

Procedure T_ConsoleColor(fg, bg)
; fg & 7 ; only 7 colors in Linux :(
; bg & 7 
 If fg < 8
  fg + 30
 Else
  fg + 82
 EndIf
 If bg < 8
  bg + 40
 Else
  bg + 92
 EndIf

 Print(Chr(27) + "[" + Str(fg) + ";" + Str(bg) + "m")
EndProcedure

Procedure T_ConsoleCursor(Height)
;0   Default
;1   invisible
;2   underscore
;3   lower_third
;4   lower_half
;5   two_thirds
;6   full block
Cursor$ = "2"
 Select Height
  Case 0
   Cursor$ = "1"
  Case 1
   Cursor$ = "2"
  Case 5
   Cursor$ = "4"
  Case 10
   Cursor$ = "6"
  EndSelect
 If Height = 0
  Print(Chr(27) + "[?25l")
 Else
  Print(Chr(27) + "[?25h")
 EndIf
 Print(Chr(27) + "[?" + Cursor$ + ";4;16c")
EndProcedure


Procedure T_ConsoleTitle(Title$)
 Print(Chr(27) + "]0;" + Title$ + Chr(7))
EndProcedure



If OpenConsole()
    EnableGraphicalConsole(1)
    
    T_ConsoleClear()
    
    For i = 0 To 200
      T_ConsoleLocate(Random(79), Random(24))
      Print("*")
    Next
    
    T_ConsoleLocate(30, 10)
    PrintN("Press return to exit")
    Input()
    
    T_ConsoleClear()
    T_ConsoleColor(0, 7)
    PrintN("Test")
    T_ConsoleColor(7, 0)
    PrintN("Press return to exit")
    Input()
    
    For fg = 0 To 15
     For bg = 0 To 15
      T_ConsoleColor(fg, bg)
      Print("O")
     Next bg
     PrintN("")
    Next fg
    
    T_ConsoleColor(7, 0)
    PrintN("")
    
    Print("Hide cursor")
    T_ConsoleCursor(0)
    Input()
    PrintN("");
    Print("Normal cursor")
    T_ConsoleCursor(1)
    Input()
    PrintN("");
    Print("Half cursor")
    T_ConsoleCursor(5)
    Input()
    PrintN("");
    Print("Full cursor")
    T_ConsoleCursor(10)
    Input()
    PrintN("");
    T_ConsoleCursor(1)
 
    T_ConsoleTitle("This is done in PurBASIC")
    PrintN("Look at the title of the window")
    Input()
    
  EndIf
It works as expected.

Bernd

Re: EnableGraphicalConsole() for Linux (and MAC)

Posted: Sun Jan 17, 2010 11:30 am
by Fred
This is interesting, i will add this, thanks.

Re: EnableGraphicalConsole() for Linux (and MAC)

Posted: Sun Jan 17, 2010 1:25 pm
by infratec
Hi,

I just extended the functions. (see above)
Now also ConsoleCursor() is available.

But :cry:
It works not in the graphical xterm
xtrem did not provide the neccessary functions.

But :D
It works on the normal linux text console.
(press CTRL + ALT + F1, if you are in a GUI (KDE or Gnome for example)
than you can login to the standard text console.
To switch back, logout and press ALT + F7)

Bernd

Re: EnableGraphicalConsole() for Linux (and MAC)

Posted: Sun Jan 17, 2010 1:58 pm
by infratec
Hi, hi,

now I found a way to hide and show the cursor in xterm.
So 0 as parameter is working now also in X.
(See T_ConsoleCursor() above.)

And I think that's the most required function:
Switch on and of the cursor.

Bernd

Re: EnableGraphicalConsole() for Linux (and MAC)

Posted: Sun Jan 17, 2010 3:38 pm
by infratec
All depnds on the used Terminal in Linux.

I added now some stuuf which allows you in the X terminals to use
16 colors, but not on the 'real' console terminal.

Also with the 'real' xterm you can now set the title of the window.

Bernd

Re: EnableGraphicalConsole() for Linux (and MAC)

Posted: Sun Jan 17, 2010 3:58 pm
by jamba
so, does this work for gnome terminal also, or only on the console like tty1(-6) with GUI running on 7?

I'll have to give this a try :D