making sure program is in a terminal

Abhinav Gauniyal

I was trying to add colors to some strings that have to be displayed in a terminal using ansi escape code. So far I haven't grasped the whole ascii escapes code thing, just trying out by copy pasting some escape codes. Then saw this answer which asked to verify that program should check that its being executed in a terminal or else continue without polluting strings with escape codes?

Answer explains to use a *nix based function isatty() which I found out resides in unistd.h which in turn wasn't promoted to cunistd by cpp standard based on my understanding that it wasn't in c's standard at first place.I tried to search SO again but wasn't able to understand well. Now I have two questions regarding this :

  • In what environment(right word?) can a program - using ascii escape codes, be executed that it requires an initial check? since I'm bulding for cli only.
  • What would be a proper solution according to ISO cpp standards for handling this issue? using unistd.h? would this use confine to modern cpp practices?

Also is there anything I should read/understand before dealing with ansi/colors related thing?

Some programmer dude

On a POSIX system (like Linux or OSX) the isatty function is indeed the correct function to determine if you're outputting to a terminal or not.

Use it as this

if (isatty(STDOUT_FILENO))
{
    // Output using VT100 control codes
}
else
{
    // Output is not a TTY, could be a pipe or redirected to a file
    // Use normal output without control codes
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Making sure a connection is closed

From Dev

Making Sure That an Input is a Float

From Dev

making sure a word is a specific length

From Dev

Making sure a var is bound in Clojure

From Dev

Making sure android app is running

From Dev

Making sure a user is authenticated in Angular

From Dev

Making Sure JSON Objects Exist

From Dev

loading scripts and making sure they are present

From Dev

making sure a word is a specific length

From Dev

making go terminal default terminal

From Dev

I need help making my program more streamline, as I am sure where are many things I am missing

From Dev

Loop making program freeze

From Dev

Making a slideshow program

From Dev

Making a GUI for python program

From Dev

Making a GUI for python program

From Dev

Making a directory with a c program

From Dev

A program for making ubuntu installers

From Dev

Making Directory in C Program

From Dev

Making sure a value is equal to one of many in LINQ

From Dev

making sure a given docker container is running

From Dev

Making sure insert happens to multiple tables

From Dev

Merging master into develop branch - making sure it works

From Dev

Making sure move constructor gets called

From Dev

Making sure only a particular group of characters are in a string

From Dev

Making sure custom tag does not have subtags

From Dev

addthis and making sure page doesnt hang

From Dev

making sure a function does not use a global variable

From Dev

Rails: Making sure two tables are "sync'd"

From Dev

making sure a generated array key is unique