Changeset: 6e2859f319f4 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=6e2859f319f4
Modified Files:
        clients/ChangeLog
        clients/mapiclient/mclient.1
        clients/mapiclient/mclient.c
Branch: default
Log Message:

Implemented a new output format in mclient: --format=expanded.
This is based on a suggestion with accompanying code from Richard
Hughes, but with some changes to deal with multi-line values.  See
bug 3673.


diffs (142 lines):

diff --git a/clients/ChangeLog b/clients/ChangeLog
--- a/clients/ChangeLog
+++ b/clients/ChangeLog
@@ -1,3 +1,7 @@
 # ChangeLog file for clients
 # This file is updated with Maddlog
 
+* Thu Feb 19 2015 Sjoerd Mullender <sjo...@acm.org>
+- Added a new output format to mclient: --format=expanded (or -fx).
+  In this format, column values are shown in full and below each other.
+
diff --git a/clients/mapiclient/mclient.1 b/clients/mapiclient/mclient.1
--- a/clients/mapiclient/mclient.1
+++ b/clients/mapiclient/mclient.1
@@ -173,6 +173,8 @@ default was found in .monetdb file.
 Specify the output format.
 The possible values are
 .BR sql ,
+.BR expanded ,
+.BR x ,
 .BR csv ,
 .BR tab ,
 .BR raw ,
@@ -186,7 +188,13 @@ is tab-separated values,
 is no special formatting (data is dumped the way the server sends it
 to the client),
 .B sql
-is a pretty format which is meant for human consumption, and
+is a pretty format which is meant for human consumption where columns
+are clearly shown,
+.B expanded
+and
+.B x
+are synonyms and are another pretty format meant for human consumption
+where column values are printed in full and below each other, and
 .B xml
 is a valid (in the XML sense) document.
 In addition to plain \fBcsv\fP, two other forms are possible.
diff --git a/clients/mapiclient/mclient.c b/clients/mapiclient/mclient.c
--- a/clients/mapiclient/mclient.c
+++ b/clients/mapiclient/mclient.c
@@ -113,7 +113,8 @@ enum formatters {
        TESTformatter,
        CLEANformatter,
        TIMERformatter,
-       SAMformatter
+       SAMformatter,
+       EXPANDEDformatter
 };
 static enum formatters formatter = NOformatter;
 char *separator = NULL;                /* column separator for CSV/TAB format 
*/
@@ -663,6 +664,61 @@ XMLrenderer(MapiHdl hdl)
 }
 
 static void
+EXPANDEDrenderer(MapiHdl hdl)
+{
+       int i, fields, fieldw, rec = 0;
+
+       fields = mapi_get_field_count(hdl);
+       fieldw = 0;
+       for (i = 0; i < fields; i++) {
+               int w = (int) utf8strlen(mapi_get_name(hdl, i), NULL);
+               if (w > fieldw)
+                       fieldw = w;
+       }
+       while (!mnstr_errnr(toConsole) && (fields = fetch_row(hdl)) != 0) {
+               int valuew = 0, len;
+               ++rec;
+               for (i = 0; i < fields; i++) {
+                       char *data = mapi_fetch_field(hdl, i);
+                       char *edata;
+                       int w;
+
+                       if (data == NULL)
+                               data = nullstring;
+                       do {
+                               edata = utf8skip(data, ~(size_t)0);
+                               w = utf8strlen(data, edata);
+                               if (w > valuew)
+                                       valuew = w;
+                               data = edata;
+                               if (*data)
+                                       data++;
+                       } while (*edata);
+               }
+               len = mnstr_printf(toConsole, "-[ RECORD %d ]-", rec);
+               while (len++ < fieldw + valuew + 3)
+                       mnstr_write(toConsole, "-", 1, 1);
+               mnstr_write(toConsole, "\n", 1, 1);
+               for (i = 0; i < fields; i++) {
+                       char *data = mapi_fetch_field(hdl, i);
+                       char *edata;
+                       const char *name = mapi_get_name(hdl, i);
+                       if (data == NULL)
+                               data = nullstring;
+                       do {
+                               edata = utf8skip(data, ~(size_t)0);
+                               mnstr_printf(toConsole, "%-*s | %.*s\n", 
fieldw, name, (int) (edata - data), data);
+                               name = "";
+                               data = edata;
+                               if (*data)
+                                       data++;
+                       } while (*edata);
+               }
+       }
+       mnstr_flush(toConsole);
+}
+
+static void
 CSVrenderer(MapiHdl hdl)
 {
        int fields;
@@ -1478,6 +1534,8 @@ setFormatter(const char *s)
                formatter = TIMERformatter;
        } else if (strcmp(s, "sam") == 0) {
                formatter = SAMformatter;
+       } else if (strcmp(s, "x") == 0 || strcmp(s, "expanded") == 0) {
+               formatter = EXPANDEDformatter;
        } else {
                mnstr_printf(toConsole, "unsupported formatter\n");
        }
@@ -1702,6 +1760,9 @@ format_result(Mapi mid, MapiHdl hdl, cha
                        case SAMformatter:
                                SAMrenderer(hdl);
                                break;
+                       case EXPANDEDformatter:
+                               EXPANDEDrenderer(hdl);
+                               break;
                        default:
                                RAWrenderer(hdl);
                                break;
@@ -2640,6 +2701,9 @@ doFile(Mapi mid, const char *file, int u
                                                case XMLformatter:
                                                        mnstr_printf(toConsole, 
"xml\n");
                                                        break;
+                                               case EXPANDEDformatter:
+                                                       mnstr_printf(toConsole, 
"expanded\n");
+                                                       break;
                                                default:
                                                        mnstr_printf(toConsole, 
"none\n");
                                                        break;
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to