taskGNU Astronomy Utilities - Tasks: task #15138, Color command-line output


You are not allowed to post comments on this tracker with your current authentication level.

task #15138: Color command-line output

Submitter:  Mohammad Akhlaghi <makhlaghi>
Submitted:  Mon 31 Dec 2018 01:09:37 AM UTC
Should Start On:  Mon 31 Dec 2018 12:00:00 AM UTC Should be Finished on:  Mon 31 Dec 2018 12:00:00 AM UTC
Category:  All Gnuastro Priority:  5 - Normal
Item Group:  Enhancement Status:  Postponed
Privacy:  Public Assigned to:  None
Percent Complete:  0% Open/Closed:  Open
Effort:  0.00

Sat 03 Apr 2021 04:51:43 PM UTC, comment #3: 

Thanks a lot Abhishek! This seems like a workable solution, nice work! I think you are ready to try it out in practice in your own development branch (if you haven't done so already).

Here are some commands that can help in the test (using one of the FITS images created during 'make check', and the 'astfits' executable that is in the build directory, so you can play with the source):

# In Gnuastro's build directory (where you ran 'make')
make check -j8

# Write the CHECKSUM and DATASUM keywords for one file
./bin/fits/astfits tests/1_wcscat.fits --write=checksum

# Verify it (both should be correct)
./bin/fits/astfits tests/1_wcscat.fits --verify

# Copy the file (just for testing).
cp tests/1_wcscat.fits tests/1_wcscat_2.fits

# Add an extra keyword (just so 'CHECKSUM' isn't valid any more)
./bin/fits/astfits tests/1_wcscat_2.fits --write=junk,123

# Verify the new file (only DATASUM should be verified)
./bin/fits/astfits tests/1_wcscat_2.fits --verify

You can now use the two calls with '--verify' for test-running your implementation.

Have you created a branch and a fork for your development? If not, see Gnuastro's forking tutorial.

I really look forward to see your fork and testing it! We can then find a generalize-able way to put this in the library and use it in many other Gnuastro programs too.

P.S. Savannah's interface is a little old, but it does have a simple Markup that can make your code blocks look more clear (like the case above). The link to this markup page is just on top of the comment-writing box (to the left of the "Preview" button).

Mohammad Akhlaghi <makhlaghi>
Group administrator
Sat 03 Apr 2021 04:16:16 PM UTC, comment #2: 

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

void red(){

void yellow(){

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 :)

Abhishek Yadav <abhishek_yadav>
Mon 31 Dec 2018 01:11:04 AM UTC, comment #1: 

Another situation to check (for removing color) is in more limited terminals like the internal Emacs terminal (when you run `M-x compile').

Mohammad Akhlaghi <makhlaghi>
Group administrator
Mon 31 Dec 2018 01:09:37 AM UTC, original submission:  

In many scenarios of Gnuastro's programs, it can be really helpful if the output text (on the command-line) is color coded. For example the new `--verify' option to the Fits program.

Fortunately all modern terminal emulators (even on Windows), follow the same ANSI escape codes to define colors (summary, or full Wikipedia description.

So the job isn't too hard, it just needs some checks (to remove when the output isn't being directly printed on a terminal, for example being piped). We should look to see how `ls' (in GNU Coreutils) does it.

Mohammad Akhlaghi <makhlaghi>
Group administrator


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

No files currently attached


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by abhishek_yadav (Posted a comment)
  • -email is unavailable- added by makhlaghi (Submitted the item)

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.


    No changes have been made to this item

    Back to the top

    Powered by Savane 3.13.
    Corresponding source code