How to use vt100 escape sequence for text color

Linux specific questions.
Post Reply
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

How to use vt100 escape sequence for text color

Post by marpon »

in c i can use the vt100 escape sequences to get console text in different colors or attributes ,
not been able to do it with freebasic print !"\027[7mtest\n\027[0m"

what is wrong?
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: How to use vt100 escape sequence for text color // SOLVED

Post by marpon »

in fact its quite simple
using the printf c lib function...

Code: Select all

#include "crt/stdio.bi"
printf(!"\027[31;1mHello \027[33;1mGuys\027[0m\n")
just notice the '!' before the string format to let the escape sequence working

it's strange not working with normal print function at least on Linux os

i can understand the windows version not working because the console does not support vt100 escape sequence but why not with Linux ?
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: How to use vt100 escape sequence for text color

Post by TeeEmCee »

It's because in FB PRINT does not output strings straight to stdout, instead there is a layer of indirection to handle COLOR, POS, etc; it escapes your escape codes so that they won't be interpreted by the terminal.
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

Re: How to use vt100 escape sequence for text color

Post by nimdays »

This is from my Aterm on TInyCore 8 32 bit, Red Hello and yellow Guys.

Image

I think printf doesn't use ncurses.
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: How to use vt100 escape sequence for text color

Post by marpon »

@nimdays

it is what was expected , the vt100 escape sequences can only work using the printf " c function "

what surprised me coming from the windows world
is why the normal print freebasic function is not able to manage that directly
it seems evident for me as Linux console natively can have that feature

I even can imagine the normal print freebasic function could have generalize the vt100 escape sequences for windows too...
(because it is quite easy to make a wrapper to the windows functions emulating the text color /attributes)

but it was the choice done, at least with the work-arround using printf the vt100 escape sequences can be used directly on Linux.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: How to use vt100 escape sequence for text color

Post by jj2007 »

marpon wrote:it is quite easy to make a wrapper to the windows functions emulating the text color attributes
It is indeed not so difficult. But should a 40 year old standard become a core function of FreeBasic? How many people would be able to use it? You can always implement it as a .bi file and offer it under "miscellaneous tools". But I don't see myself indulging in creating text files that change colours using VT100 escape sequences (are there editors that can do that?). Printing a line, changing the colour with something like ConsoleColor, printing another line etc sounds more straightforward, especially for a language that claims to be OK for beginners.

There is a page describing the codes here. Example (pretty old-fashioned for my taste):

Code: Select all

resetrep DECARM       Reset auto-repeat mode                 ^[[?8l
resetinter DECINLM    Reset interlacing mode                 ^[[?9l

altkeypad DECKPAM     Set alternate keypad mode              ^[=
numkeypad DECKPNM     Set numeric keypad mode                ^[>

setukg0               Set United Kingdom G0 character set    ^[(A
setukg1               Set United Kingdom G1 character set    ^[)A
setusg0               Set United States G0 character set     ^[(B
setusg1               Set United States G1 character set     ^[)B
setspecg0             Set G0 special chars. & line set       ^[(0
setspecg1             Set G1 special chars. & line set       ^[)0
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: How to use vt100 escape sequence for text color

Post by marpon »

@jj2007
as often ... your reaction is strange

BASIC is also more than 40 years old, but still some people interrested

on the Dos days the Vt escape sequences where working on the terminal
on Linux , Mac , and i think under Android the terminal accept those sequences
it is only after win 95/98 Windows not supporting them

so, its normal i'm surprised that native feature in Linux is not taken on FreeBasic
because it's very simple to have text colored on console mode, specially on Linux wich is naturally console mode

And thanks for the extract quote and the link, i know them quite well...

to finish, its not an issue for me , i'm confortable with printf for Linux and with setconsoletextattribute on windows
and if beginners need that, some define could make the trick easily.


Hope my answer did not offense you
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: How to use vt100 escape sequence for text color

Post by jj2007 »

marpon wrote:Hope my answer did not offense you
No, don't worry. My concern is to make FB as simple as possible for a beginner or a hobby programmer. It's BASIC, and yes it has become a niche language. But inside its little niche it should be a great language, simple and elegant, the best BASIC that is available.

Those who feel stronger can go for C++, or hack together VT100 sequences, and use them with an additional VT100 library. But putting such exotic functions into the core of the language? Do you think it's a good idea, do you think it's necessary?
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to use vt100 escape sequence for text color

Post by dodicat »

With Win 10 fire a blank shell then proceed.

Code: Select all




#include "crt.bi"

shell " "

printf(!"%s\n",!"\027[7mtest\n\027[0m")

printf(!"%s\n",!"\027[31;1mHello \027[33;1mGuys\027[0m\n")
sleep 
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to use vt100 escape sequence for text color

Post by dodicat »

Here are some console controls
Clearscreen and locate.
I use windows.bi for locate.
There must be a vt100 command for this?

Edit (found the command)

Code: Select all






#include "crt.bi"

#define locateC(y,x)   printf(!"\027["+str(y)+";"+str(x)+"f")
#define ClearScreen    printf(!"\027[2J\027[f") 

shell 
locateC(2,10)
puts !"\027[4mPress a key to start\027[0m\n"
sleep
ClearScreen
locateC(5,20)
printf(!"%s\n",!"\027[7mtest\n\027[0m")

locateC(8,20)
printf(!"%s\n",!"\027[31;1mHello \027[33;1mGuys\027[0m")


locateC(15,20)
printf(!"\027[32;4mFrom http://wiki.bash-hackers.org/scripting/terminalcodes\027[0m\n")

locateC(17,20)
printf (!"%s\n", !"\027[1mIt is \027[31mnot\027[39m intelligent to use \027[32mhardcoded ANSI\027[39m codes!\027[0m")

sleep


 
marpon
Posts: 342
Joined: Dec 28, 2012 13:31
Location: Paris - France

Re: How to use vt100 escape sequence for text color

Post by marpon »

@dodicat
nice, but limited to win10 ,

for all win versions what about ?

Code: Select all

#include "windows.bi"

#define LRED setconsoletextattribute(hwincons, 12)
#define LGREEN setconsoletextattribute(hwincons, 10)
#define LYELLOW setconsoletextattribute(hwincons, 14)
#define LNORMAL setconsoletextattribute(hwincons, 7)

dim as handle hwincons = Getstdhandle(STD_OUTPUT_HANDLE )

LYELLOW  
print "Hello "; 
LGREEN
print "Guys ";
LRED
print "How ara you today ?"
LNORMAL

'just to wait and see the stdout and stderr handles
messagebox(0,"stdout  = " & Getstdhandle(STD_OUTPUT_HANDLE ) & chr(10) & "stderr = " & Getstdhandle(STD_ERROR_HANDLE),"close",0)
Post Reply