gnuastro-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[task #15138] Color command-line output


From: Abhishek Yadav
Subject: [task #15138] Color command-line output
Date: Sat, 3 Apr 2021 12:16:16 -0400 (EDT)
User-agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:85.0) Gecko/20100101 Firefox/85.0

Follow-up Comment #2, task #15138 (project gnuastro):

Hi! I tried to color code the various outputs by creating functions of
different colors that switch the output color.
For example :

void red(){
printf("\033[1;31m");
}

void yellow(){
printf("\033[1;33m");
}

Then before printing the desired output we can simply call one of these to
activate the desired output color. Now to check that the output is printed on
terminal and not being piped we can use the "isatty(1)" function where "1" is
the parameter that checks that stdout is on terminal.
For example in the  -- verify option in the fits program:

+static int
+keywords_verify(struct fitsparams *p, fitsfile **fptr)
+{
+  int dataok, hduok, status=0;
+
+  /* Ask CFITSIO to verify the two keywords. */
+  if( fits_verify_chksum(*fptr, &dataok, &hduok, &status) )
+    gal_fits_io_error(status, NULL);
+
+  /*Applying a check to see that output is being printed on terminal itself
*/
+  if(isatty(1)){
+    /* calling the desired color function */
+    red();
+  }
+ 
+  /* Print the verification result. */
+  printf("DATASUM:  %s\n", ( dataok==1
+                             ? "Verified"
+                             : ( dataok==0 ? "NOT-PRESENT" : "INCORRECT"
)));
+  printf("CHECKSUM: %s\n", ( hduok==1
+                             ? "Verified"
+                             : ( hduok==0  ? "NOT-PRESENT" : "INCORRECT"
)));
+
+  /* Some further information. */
+  if(!p->cp.quiet)
+    printf("\n--------\n"
+           " - DATASUM:  calculated only from the HDU/extension's data (not
"
+           "keywords).\n"
+           " - CHECKSUM: calculated from the full header (data and "
+           "keywords).\n\n");
+
+  /* Return failure if any of the keywords are not verified. */
+  return (dataok==-1 || hduok==-1) ? EXIT_FAILURE : EXIT_SUCCESS;
+}

will this work for the issue ? Please give suggestions :)

    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/task/?15138>

_______________________________________________
  Message sent via Savannah
  https://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]