rdline: support pager during completion
[libcmdline.git] / src / lib / cmdline_rdline.h
index 23b8cb6..152c8b5 100644 (file)
@@ -87,7 +87,7 @@
 #define RDLINE_VT100_BUF_SIZE  8
 #define RDLINE_HISTORY_BUF_SIZE BUFSIZ
 #define RDLINE_HISTORY_MAX_LINE 64
-#define RDLINE_MAX_LINES 39
+#define RDLINE_MAX_LINES 23
 
 enum rdline_status {
        RDLINE_INIT,
@@ -102,7 +102,7 @@ struct rdline;
  * help. The first argument is an opaque pointer. The other args
  * are buffer and size.
  */
-typedef ssize_t (rdline_write_t)(const struct rdline *, void *, size_t);
+typedef ssize_t (rdline_write_t)(struct rdline *, void *, size_t);
 
 
 typedef void (rdline_validate_t)(struct rdline *rdl,
@@ -112,6 +112,8 @@ typedef int (rdline_complete_t)(struct rdline *rdl, const char *buf,
 typedef int (rdline_help_t)(struct rdline *rdl, const char *buf,
                            rdline_write_t *write, void *opaque);
 
+typedef void (rdline_asyncpager_cb_t)(struct rdline *, void *);
+
 struct rdline {
        enum rdline_status status;
        int fd_in;
@@ -153,6 +155,8 @@ struct rdline {
        int pager_len; /* total len of buffer */
        int pager_off; /* offset of next data */
        int pager_lines; /* number of lines displayed */
+       rdline_asyncpager_cb_t *pager_cb;
+       void *pager_arg;
 #endif
 };
 
@@ -359,6 +363,27 @@ void rdline_clear_history(struct rdline *rdl);
 char *rdline_get_history_item(struct rdline *rdl, unsigned int i);
 
 #ifndef NO_PAGER
+/**
+ * Write data asynchronously (using pager if needed)
+ *
+ * If there is enough place to print data on the current page, it is
+ * printed synchronously. Else, a temporary buffer is allocated and
+ * the data is stored in it. When the main rdline is called again, the
+ * pager is flushed before parsing any other commands.
+ *
+ * @param rdl
+ *   The rdline descriptor
+ * @param buf
+ *   Buffer to be sent
+ * @param len
+ *   Length of buffer to be sent
+ * @return
+ *   On success, the number of bytes written is returned (zero
+ *   indicates nothing was written). On error, -1 is returned, and
+ *   errno is set appropriately
+ */
+ssize_t rdline_asyncpager_write(struct rdline *rdl, void *buf, size_t len);
+
 /**
  * Print data asynchronously (using pager if needed)
  *
@@ -377,6 +402,23 @@ char *rdline_get_history_item(struct rdline *rdl, unsigned int i);
  *   output to strings). On error, a negative value is returned.
  */
 int rdline_asyncpager_printf(struct rdline *rdl, const char *fmt, ...);
+
+/**
+ * Set the callback for the pager
+ *
+ * If there is some data in the pager to be printed, set a callback
+ * function that will be called when all the data will be printed. If
+ * the pager is empty, don't do anything and return -1.
+ * @param rdl
+ *   The rdline descriptor
+ * @return
+ *   - 0 if there is some data in the pager buffer and the callback
+ *     is loaded
+ *   - -1 if there is no data in pager buffer (in this case the callback
+ *     is not called)
+ */
+int rdline_asyncpager_set_cb(struct rdline *rdl, rdline_asyncpager_cb_t *cb,
+                            void *arg);
 #endif