poke-devel
[Top][All Lists]
Advanced

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

[COMMITTED] poke: new module pk-table.pk


From: Jose E. Marchesi
Subject: [COMMITTED] poke: new module pk-table.pk
Date: Mon, 17 Jan 2022 03:47:35 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

This commit introduces a new Poke facility in the `poke' application,
pk-table.pk, that provides a Pk_Table to ease the output of tabulated
data.  It is basically the Poke counterpart of pk-table.[ch].

The pk_settings_dump (which implements the .set command without
arguments) is also modified to use Pk_Table in order to emit a
well-tabulated relation of all the available settings and their
values.

Note how "execute" terminal hyperlinks are generated in the setting
names to execute ".help SETTING".

2022-01-17  Jose E. Marchesi  <jemarch@gnu.org>

        * poke/pk-table.pk: New file.
        * poke/Makefile.am (dist_pkgdata_DATA): Add pk-table.pk.
        * poke/pk-settings.pk (pk_settings_dump): Use pk_table.
---
 ChangeLog           |   6 +++
 poke/Makefile.am    |   2 +-
 poke/pk-settings.pk |  24 ++++++-----
 poke/pk-table.pk    | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 poke/poke.pk        |   4 ++
 5 files changed, 139 insertions(+), 10 deletions(-)
 create mode 100644 poke/pk-table.pk

diff --git a/ChangeLog b/ChangeLog
index 3e95cb1a..a482f7a6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2022-01-17  Jose E. Marchesi  <jemarch@gnu.org>
 
+       * poke/pk-table.pk: New file.
+       * poke/Makefile.am (dist_pkgdata_DATA): Add pk-table.pk.
+       * poke/pk-settings.pk (pk_settings_dump): Use pk_table.
+
+2022-01-17  Jose E. Marchesi  <jemarch@gnu.org>
+
        * poke/pk-map.c (pk_map_alien_token_handler): Remove spurious
        trace.
 
diff --git a/poke/Makefile.am b/poke/Makefile.am
index 960de891..b38bae86 100644
--- a/poke/Makefile.am
+++ b/poke/Makefile.am
@@ -28,7 +28,7 @@ EXTRA_DIST =
 
 dist_pkgdata_DATA = pk-cmd.pk pk-dump.pk pk-save.pk pk-copy.pk \
                     pk-extract.pk pk-scrabble.pk poke.pk pk-settings.pk \
-                    pk-help.pk pk-map.pk
+                    pk-help.pk pk-map.pk pk-table.pk
 
 bin_PROGRAMS = poke
 poke_SOURCES = poke.c poke.h \
diff --git a/poke/pk-settings.pk b/poke/pk-settings.pk
index f4192d49..264b3bc5 100644
--- a/poke/pk-settings.pk
+++ b/poke/pk-settings.pk
@@ -525,16 +525,22 @@ This setting is `no' by default.",
 
 fun pk_settings_dump = void:
   {
+    var table = Pk_Table { num_columns = 2 };
+
     for (setting in pk_settings.entries)
       {
-        if (setting.kind == POKE_SETTING_INT)
-          printf ("%s %i32d\n", setting.name, setting.getter as int);
-        else if (setting.kind == POKE_SETTING_BOOL)
-          printf ("%s %s\n", setting.name,
-                  setting.getter as int ? "yes" : "no");
-        else if (setting.kind == POKE_SETTING_STR)
-          printf ("%s %s\n", setting.name, setting.getter as string);
-        else
-          assert (0, "uknown kind");
+        var setting_value = (setting.kind == POKE_SETTING_INT
+                             ? format ("%v", setting.getter as int)
+                             : setting.kind == POKE_SETTING_BOOL
+                             ? format ("%s", setting.getter as int ? "yes" : 
"no")
+                             : setting.kind == POKE_SETTING_STR
+                             ? format ("%v", setting.getter as string)
+                             : "<unknown>");
+        table.row;
+        table.column (setting.name, "",
+                      hserver_make_hyperlink ('e', ".help " + setting.name));
+        table.column (setting_value);
       }
+
+    table.print_table;
   }
diff --git a/poke/pk-table.pk b/poke/pk-table.pk
new file mode 100644
index 00000000..1a4611e6
--- /dev/null
+++ b/poke/pk-table.pk
@@ -0,0 +1,113 @@
+/* pk-table.pk - Output tabulated data to the terminal.  */
+
+/* `Because tabs SUCK ass', Poke version.  */
+
+/* Copyright (C) 2022 Jose E. Marchesi */
+
+/* This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+type Pk_Table_Entry =
+  struct
+  {
+    string style;
+    string hyperlink;
+    string str;
+  };
+
+type Pk_Table_Row =
+  struct
+  {
+    string style;
+    Pk_Table_Entry[] entries;
+  };
+
+type Pk_Table =
+ struct
+ {
+   int num_columns;
+   int next_column_index;
+   int[num_columns] column_widths;
+   Pk_Table_Row[] rows;
+
+   method row = (string style = "") void:
+   {
+     /* Make sure the current row is complete.  */
+     assert (rows'length == 0
+             || next_column_index == num_columns);
+
+     rows += [Pk_Table_Row { style = style }];
+     next_column_index = 0;
+   }
+
+   method column = (string str, string style = "",
+                    string hyperlink = "") void:
+   {
+     var row = rows[rows'length - 1];
+
+     row.entries += [Pk_Table_Entry { style = style,
+                                      hyperlink = hyperlink,
+                                      str = str }];
+     next_column_index += 1;
+   }
+
+   method print_table = void:
+   {
+     /* Calculate the column widths.  */
+     for (var i = 0; i < num_columns; ++i)
+     {
+       var cwidth = 0;
+
+       for (row in rows)
+       {
+         var entry_width = row.entries[i].str'length + 2;
+         if (entry_width > cwidth)
+           cwidth = entry_width;
+       }
+
+       column_widths[i] = cwidth;
+     }
+
+     /* Now print out the stuff.  */
+     for (row in rows)
+     {
+       if (row.style != "")
+         term_begin_class (row.style);
+
+       var j = 0;
+       for (entry in row.entries)
+       {
+         if (entry.style != "")
+           term_begin_class (entry.style);
+         if (entry.hyperlink != "")
+           term_begin_hyperlink (entry.hyperlink);
+
+         print entry.str;
+
+         if (entry.hyperlink != "")
+           term_end_hyperlink;
+         if (entry.style != "")
+           term_end_class (entry.style);
+
+         var fill = column_widths[j++] - entry.str'length + 1;
+         print " " * fill;
+       }
+
+       if (row.style != "")
+         term_end_class (row.style);
+
+       print "\n";
+     }
+   }
+ };
diff --git a/poke/poke.pk b/poke/poke.pk
index cf3f9f3e..dea07940 100644
--- a/poke/poke.pk
+++ b/poke/poke.pk
@@ -43,6 +43,10 @@ var pk_doc_viewer = "info";
 /* Whether poke was built with support for libNBD.  */
 var pk_have_libnbd_p = 0;
 
+/**** Utilitty to print tabulated data.  ****/
+
+load "pk-table.pk";
+
 /**** Online help system.  ****/
 
 load "pk-help.pk";
-- 
2.11.0




reply via email to

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