emacs-elpa-diffs
[Top][All Lists]
Advanced

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

[elpa] externals/kubed b9c361be89 1/4: New command 'kubed-list-fit-colum


From: ELPA Syncer
Subject: [elpa] externals/kubed b9c361be89 1/4: New command 'kubed-list-fit-column-width-to-content'
Date: Tue, 6 Aug 2024 12:58:31 -0400 (EDT)

branch: externals/kubed
commit b9c361be89390d3304f3de479cd26557ad14c99d
Author: Eshel Yaron <me@eshelyaron.com>
Commit: Eshel Yaron <me@eshelyaron.com>

    New command 'kubed-list-fit-column-width-to-content'
    
    * 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   | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 kubed.texi |  7 +++++++
 2 files changed, 67 insertions(+)

diff --git a/kubed.el b/kubed.el
index 953e75708a..aaffbb547b 100644
--- 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
diff --git a/kubed.texi b/kubed.texi
index a542933d0d..3d0187f1d1 100644
--- a/kubed.texi
+++ b/kubed.texi
@@ -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 @{



reply via email to

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