]> git.eshelyaron.com Git - kubed.git/commitdiff
New command 'kubed-list-fit-column-width-to-content'
authorEshel Yaron <me@eshelyaron.com>
Tue, 6 Aug 2024 14:59:25 +0000 (16:59 +0200)
committerEshel Yaron <me@eshelyaron.com>
Tue, 6 Aug 2024 15:01:59 +0000 (17:01 +0200)
* kubed.el (kubed-list-column-number-at-point): New function.
(kubed-list-fit-column-width-to-content): New command.
(kubed-list-mode-map): Bind it.
* kubed.texi (Browse Resources): Document it.

kubed.el
kubed.texi

index 953e75708a8afafff53379d79dcf1e1f641c6b2e..aaffbb547b2e01f81f04e71ab88cb1e23d96be8e 100644 (file)
--- a/kubed.el
+++ b/kubed.el
@@ -768,6 +768,65 @@ regardless of QUIET."
               0)))
     (user-error "No Kubernetes resource at point")))
 
+(defun kubed-list-column-number-at-point ()
+  "Return table column number at point."
+  (let ((start (current-column))
+        (nb-cols (1- (length tabulated-list-format)))
+        (col-nb 0)
+        (total-width tabulated-list-padding)
+        (found nil))
+    (while (and (not found) (< col-nb nb-cols))
+      (if (>= start
+              (setq total-width
+                    (+ total-width
+                       (cadr (aref tabulated-list-format col-nb))
+                       (or (plist-get (nthcdr 3 (aref tabulated-list-format
+                                                      col-nb))
+                                      :pad-right)
+                           1))))
+          (setq col-nb (1+ col-nb))
+        (setq found t)))
+    col-nb))
+
+(defun kubed-list-fit-column-width-to-content (n)
+  "Fit width of Nth table column to its content.
+
+If N is negative, fit all columns.  Interactively, N is the column
+number at point, or the numeric prefix argument if you provide one."
+  (interactive
+   (list (if current-prefix-arg
+             (prefix-numeric-value current-prefix-arg)
+           (kubed-list-column-number-at-point)))
+   kubed-list-mode)
+  (if (< n 0)
+      ;; Fit all columns.
+      (let* ((num-cols (length tabulated-list-format))
+             (widths (apply #'vector (seq-map
+                                      ;; +2 to make room for sorting icon.
+                                      (lambda (col) (+ 2 (length (car col))))
+                                      tabulated-list-format))))
+        (save-excursion
+          (goto-char (point-min))
+          (while-let ((entry (tabulated-list-get-entry)))
+            (dotimes (i num-cols)
+              (aset widths i (max (aref widths i) (length (aref entry i)))))
+            (forward-line)))
+        (setq tabulated-list-format (copy-tree tabulated-list-format t))
+        (dotimes (i num-cols)
+          (setf (cadr (aref tabulated-list-format i))
+                (1+ (aref widths i)))))
+    ;; Fit Nth column.
+    (let* ((width (+ 2 (length (car (aref tabulated-list-format n))))))
+      (save-excursion
+        (goto-char (point-min))
+        (while-let ((entry (tabulated-list-get-entry)))
+          (setq width (max width (length (aref entry n))))
+          (forward-line)))
+      (setq tabulated-list-format (copy-tree tabulated-list-format t))
+      (setf (cadr (aref tabulated-list-format n)) (1+ width))))
+  (tabulated-list-print t)
+  (tabulated-list-init-header))
+
 (defvar-keymap kubed-list-mode-map
   :doc "Common keymap for Kubernetes resource list buffers."
   "RET" #'kubed-list-select-resource
@@ -780,6 +839,7 @@ regardless of QUIET."
   "!" #'kubed-list-kubectl-command
   "G" #'kubed-list-update
   "/" #'kubed-list-set-filter
+  "|" #'kubed-list-fit-column-width-to-content
   "d" #'kubed-list-mark-for-deletion
   "u" #'kubed-list-unmark
   "w" #'kubed-list-copy-as-kill
index a542933d0d6c7366f5f0ac576c81477e8c24bf80..3d0187f1d1c6013f6ff3845d535e100d56d61c56 100644 (file)
@@ -386,6 +386,13 @@ column, move to the last column of the previous line instead
 @findex tabulated-list-sort
 @item S
 Sort lines by the column at point (@code{tabulated-list-sort}).
+@kindex |
+@findex kubed-list-fit-column-width-to-content
+@item |
+Fit width of column at point to its content
+(@code{kubed-list-fit-column-width-to-content}).  With non-negative
+prefix argument @var{n} (for example, @kbd{0 |}), fit @var{n}th
+column.  With negative prefix argument (@kbd{- |}), fit all columns.
 @kindex @{
 @findex tabulated-list-narrow-current-column
 @item @{