From 3b29ac7b7f529098279ad70a4931519593d91fbd Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Sun, 2 May 2021 00:49:45 +0200
Subject: [PATCH] LKT: Add the 'auto' way of setting the row printer

---
 src/main/lkt.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/src/main/lkt.c b/src/main/lkt.c
index 494ed7b6..b3fb82ff 100644
--- a/src/main/lkt.c
+++ b/src/main/lkt.c
@@ -7,6 +7,7 @@
 #include <lektor/segv.h>
 
 #include <arpa/inet.h>
+#include <sys/ioctl.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
@@ -35,6 +36,7 @@ typedef struct {
 
 #define ROW_FORMAT_ONELINE "oneline"
 #define ROW_FORMAT_PRETTY  "pretty"
+#define ROW_FORMAT_AUTO    "auto"
 typedef void (*row_printer)(char *);
 row_printer row_print = NULL;
 
@@ -49,6 +51,19 @@ static const char *row_format;
 /* Default queue length to query */
 static const char *LKT_QUEUE_DEFAULT[] = { "10" };
 
+/* Small screen column count, to autoset the 'pretty' printer. The standard
+ * 80x25 terminal is small! */
+#define LKT_SMALL_SCREEN_THRESHOLD 82
+#define IS_SMALL_SCREEN (___column_count <= LKT_SMALL_SCREEN_THRESHOLD)
+static unsigned ___column_count = 0;
+CONSTRUCTOR_FUNCTION
+setup_column_count(void)
+{
+    struct winsize size;
+    if (0 == ioctl(STDOUT_FILENO, TIOCGWINSZ, &size))
+        ___column_count = size.ws_col;
+}
+
 /* Row printer, should be queue/playlist/search agnostic, i.e. should handle
  * the following lines in a similar way (be aware of the space):
  * - 7452 vo - multi / Code Geass - IS1 - Continued Story [Kubat]
@@ -107,7 +122,10 @@ ___row_printer_pretty(char *line)
 PRIVATE_FUNCTION void
 setup_row_printer(void)
 {
-    if (NULL == row_format || STR_MATCH(row_format, ROW_FORMAT_ONELINE))
+    if (NULL == row_format || STR_MATCH(row_format, ROW_FORMAT_AUTO))
+        row_print = IS_SMALL_SCREEN ? ___row_printer_pretty : ___row_printer_online;
+
+    else if (NULL == row_format || STR_MATCH(row_format, ROW_FORMAT_ONELINE))
         row_print = ___row_printer_online;
 
     else if (STR_MATCH(row_format, ROW_FORMAT_PRETTY))
-- 
GitLab