gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master c813876: Text library: no white-space when a s


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master c813876: Text library: no white-space when a single number is printed on stdout
Date: Sat, 29 Feb 2020 07:26:46 -0500 (EST)

branch: master
commit c813876f40480817bc79aa217d3292452c3e92ec
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    Text library: no white-space when a single number is printed on stdout
    
    Until now, when a single number is printed on the standard output, the text
    library would print it just like it prints many columns in a text file: it
    would add extra white space after the column value. This was very annoying
    when used in scripts: the white space needed to be trimmed when the value
    is printed in a higher-level structure.
    
    With this commit, it now checks if the output is on printed on the standard
    output and if it is only a single number. In this scenario, it will just
    use the `gal_type_to_string' function to generate the data's string and
    prints that string with no extra white space.
---
 lib/txt.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/lib/txt.c b/lib/txt.c
index e354b13..15ff79b 100644
--- a/lib/txt.c
+++ b/lib/txt.c
@@ -1479,14 +1479,26 @@ gal_txt_write(gal_data_t *input, gal_list_str_t 
*comment, char *filename,
   switch(input->ndim)
     {
     case 1:
-      for(i=0;i<input->size;++i)                        /* Row.    */
-        {
-          j=0;
-          for(data=input;data!=NULL;data=data->next)    /* Column. */
-            txt_print_value(fp, data->array, data->type, i,
+      /* When the dataset is bring printed on standard output and its a
+         single number, don't print the column structure, because it will
+         add white-space characters which can be annoying when used in an
+         automatic script. */
+      if(fp==stdout && input->size==1 && input->next==NULL)
+        fprintf(fp, "%s\n",
+                gal_type_to_string(input->array, input->type, 0));
+
+      /* Dataset has more than one row AND more than one column, so follow
+         the basic text formatting (like extra white space to keep the
+         columns under each other). */
+      else
+        for(i=0;i<input->size;++i)                        /* Row.    */
+          {
+            j=0;
+            for(data=input;data!=NULL;data=data->next)    /* Column. */
+              txt_print_value(fp, data->array, data->type, i,
                             fmts[j++ * FMTS_COLS]);
-          fprintf(fp, "\n");
-        }
+            fprintf(fp, "\n");
+          }
       break;
 
 



reply via email to

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