poke-devel
[Top][All Lists]
Advanced

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

[PATCH v2] cmd: set: Introduce oacutoff parameter to shorten arrays


From: Carlo Caione
Subject: [PATCH v2] cmd: set: Introduce oacutoff parameter to shorten arrays
Date: Thu, 13 Feb 2020 14:02:37 +0100

Displaying large arrays is cumbersome, this patch introduces a new set
command (.set oacutoff N) setting a maximum allowed number of
elements that are displayed when printing the array as part of a struct.

Signed-off-by: Carlo Caione <address@hidden>

  * doc/poke.texi: add documentation
  * src/pk-set.c: new set oacutoff command
  * testsuite/poke.cmd/set-oacutoff.pk: new test file
  * src/pvm.c: helpers to retrieve oacutoff flag
  * src/pvm.h: likewise
  * src/pvm.jitter: new flag in the PVM state struct
  * src/pvm-val.c: support new array cutoff parameter
---
 doc/poke.texi  |  6 ++++++
 src/pk-set.c   | 26 ++++++++++++++++++++++++++
 src/pvm-val.c  | 14 +++++++++++++-
 src/pvm.c      | 14 ++++++++++++++
 src/pvm.h      |  3 +++
 src/pvm.jitter |  2 ++
 6 files changed, 64 insertions(+), 1 deletion(-)

diff --git a/doc/poke.texi b/doc/poke.texi
index ab4ae8d4..daf375fc 100644
--- a/doc/poke.texi
+++ b/doc/poke.texi
@@ -694,6 +694,12 @@ up to the @code{depth}-th level. The default value 
@code{0} means no limit.
 @cindex indent, as number of spaces
 Number defining the number of spaces used for indentation for each level. Only
 values >=1 and <= 10 are valid. Default value is '2'.
+@item oacutoff
+@cindex array cutoff
+When displaying an array as struct field, display only the elements up to the
+@code{cutoff} index and display @code{...} after that. Value of @code{0}
+means no limit. This cutoff value is not used when directly displaying arrays
+content.
 @end table
 
 @node vm command
diff --git a/src/pk-set.c b/src/pk-set.c
index f1df36bd..6d56a364 100644
--- a/src/pk-set.c
+++ b/src/pk-set.c
@@ -230,6 +230,28 @@ pk_cmd_set_pretty_print (int argc, struct pk_cmd_arg 
argv[], uint64_t uflags)
   return 1;
 }
 
+static int
+pk_cmd_set_oacutoff (int argc, struct pk_cmd_arg argv[], uint64_t uflags)
+{
+  /* set oacutoff [CUTOFF]  */
+
+  assert(argc == 1);
+
+  int cutoff = PK_CMD_ARG_INT (argv[0]);
+
+  if (cutoff < 0)
+    {
+      pk_term_class ("error");
+      pk_puts ("error: ");
+      pk_term_end_class ("error");
+      pk_puts (_(" cutoff should be >=0.\n"));
+      return 0;
+    }
+
+  pvm_set_oacutoff (poke_vm, cutoff);
+  return 1;
+}
+
 static int
 pk_cmd_set_odepth (int argc, struct pk_cmd_arg argv[], uint64_t uflags)
 {
@@ -369,6 +391,9 @@ pk_cmd_set_error_on_warning (int argc, struct pk_cmd_arg 
argv[],
 
 extern struct pk_cmd null_cmd; /* pk-cmd.c  */
 
+struct pk_cmd set_oacutoff_cmd =
+  {"oacutoff", "i", "", 0, NULL, pk_cmd_set_oacutoff, "set oacutoff [CUTOFF]"};
+
 struct pk_cmd set_oindent_cmd =
   {"oindent", "i", "", 0, NULL, pk_cmd_set_oindent, "set oindent [INDENT]"};
 
@@ -397,6 +422,7 @@ struct pk_cmd set_error_on_warning_cmd =
 
 struct pk_cmd *set_cmds[] =
   {
+   &set_oacutoff_cmd,
    &set_obase_cmd,
    &set_omode_cmd,
    &set_odepth_cmd,
diff --git a/src/pvm-val.c b/src/pvm-val.c
index 1d727102..41277760 100644
--- a/src/pvm-val.c
+++ b/src/pvm-val.c
@@ -30,6 +30,8 @@
 
 #define STREQ(a, b) (strcmp (a, b) == 0)
 
+unsigned int pk_odepth;
+
 pvm_val
 pvm_make_int (int32_t value, int size)
 {
@@ -794,6 +796,17 @@ pvm_print_val (pvm_val val, int base, int flags)
 
           if (idx != 0)
             pk_puts (",");
+
+          if ((pvm_oacutoff (poke_vm) != 0) &&
+             ((pvm_oacutoff (poke_vm) <= idx) &&
+              (pk_odepth != 0)))
+            {
+              pk_term_class ("ellipsis");
+              pk_puts("...");
+              pk_term_end_class ("ellipsis");
+              break;
+            }
+
           pvm_print_val (elem_value, base, flags);
 
           if (flags & PVM_PRINT_F_MAPS && elem_offset != PVM_NULL)
@@ -818,7 +831,6 @@ pvm_print_val (pvm_val val, int base, int flags)
       pvm_val struct_type = PVM_VAL_SCT_TYPE (val);
       pvm_val struct_type_name = PVM_VAL_TYP_S_NAME (struct_type);
       pvm_val pretty_printer = pvm_get_struct_method (val, "_print");
-      static unsigned int pk_odepth;
 
       /* If the struct has a pretty printing method (called _print)
          then use it, unless the PVM is configured to not do so.
diff --git a/src/pvm.c b/src/pvm.c
index bb041f15..68fff1d6 100644
--- a/src/pvm.c
+++ b/src/pvm.c
@@ -45,6 +45,8 @@
   ((PVM)->pvm_state.pvm_state_runtime.odepth)
 #define PVM_STATE_OINDENT(PVM)                          \
   ((PVM)->pvm_state.pvm_state_runtime.oindent)
+#define PVM_STATE_OACUTOFF(PVM)                         \
+  ((PVM)->pvm_state.pvm_state_runtime.oacutoff)
 
 struct pvm
 {
@@ -210,6 +212,18 @@ pvm_set_odepth (pvm apvm, unsigned int odepth)
   PVM_STATE_ODEPTH (apvm) = odepth;
 }
 
+unsigned int
+pvm_oacutoff (pvm apvm)
+{
+  return PVM_STATE_OACUTOFF (apvm);
+}
+
+void
+pvm_set_oacutoff (pvm apvm, unsigned int cutoff)
+{
+  PVM_STATE_OACUTOFF (apvm) = cutoff;
+}
+
 void
 pvm_assert (int expression)
 {
diff --git a/src/pvm.h b/src/pvm.h
index f0df8ff9..ec4e4118 100644
--- a/src/pvm.h
+++ b/src/pvm.h
@@ -111,6 +111,9 @@ void pvm_set_oindent (pvm apvm, unsigned int oindent);
 unsigned int pvm_odepth (pvm apvm);
 void pvm_set_odepth (pvm apvm, unsigned int odepth);
 
+unsigned int pvm_oacutoff (pvm apvm);
+void pvm_set_oacutoff (pvm apvm, unsigned int cutoff);
+
 /* Set the current negative encoding for PVM.  NENC should be one of
  * the IOS_NENC_* values defined in ios.h */
 
diff --git a/src/pvm.jitter b/src/pvm.jitter
index 96c48d01..0e8a9609 100644
--- a/src/pvm.jitter
+++ b/src/pvm.jitter
@@ -570,6 +570,7 @@ state-struct-runtime-c
       enum pvm_omode omode;
       uint32_t odepth;
       uint32_t oindent;
+      uint32_t oacutoff;
   end
 end
 
@@ -585,6 +586,7 @@ state-initialization-c
       jitter_state_runtime->omode = PVM_PRINT_FLAT;
       jitter_state_runtime->odepth = 0;
       jitter_state_runtime->oindent = 2;
+      jitter_state_runtime->oacutoff = 0;
   end
 end
 
-- 
2.20.1




reply via email to

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