From 8de12a2e19dc745a133068c5d33f7d2857a32a1b Mon Sep 17 00:00:00 2001 From: Eshel Yaron Date: Wed, 7 Aug 2024 11:38:36 +0200 Subject: [PATCH] New list filter operators '<' and '>' * kubed.el (kubed-list-filter-lt-operator) (kubed-list-filter-gt-operator): New functions. (kubed-list-filter-operator-alist): Add them. * kubed.texi (List Filter): Document them. --- kubed.el | 20 ++++++++++++++++---- kubed.texi | 9 +++++---- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/kubed.el b/kubed.el index 073e6d8..fa9da90 100644 --- a/kubed.el +++ b/kubed.el @@ -319,17 +319,29 @@ should only be available in buffers that display Kuberenetes resources." revert-buffer-function #'kubed-display-resource-revert bookmark-make-record-function #'kubed-display-resource-make-bookmark))) +(defun kubed-list-filter-lt-operator (v s) + "Return non-nil if S is less than V as a number or as a string." + (let ((l (string-to-number s)) (r (string-to-number v))) + (if (= l r) (string< s v) (< l r)))) + +(defun kubed-list-filter-gt-operator (v s) + "Return non-nil if S is greater than V as a number or as a string." + (let ((l (string-to-number s)) (r (string-to-number v))) + (if (= l r) (string> s v) (> l r)))) + (defcustom kubed-list-filter-operator-alist - '((= . string=) - (~ . string-match-p)) + `((= . string=) + (~ . string-match-p) + (< . kubed-list-filter-lt-operator) + (> . kubed-list-filter-gt-operator)) "Association list of filter operators and functions that implement them. -Element of this list are cons cells (OP . FN), where OP is a symbol that +Elements of the list are cons cells (OP . FN), where OP is a symbol that is used as a filter operator and FN is a function that implements OP. FN takes two arguments, a string STR and a parameter VAL. FN should return non-nil if STR and VAL are related according to OP: to determine if a line in which column COL is STR satisfies the filter (OP COL VAL), -Kubed checks if the form (FN STR VAL) evaluates to non-nil." +Kubed checks if the form (FN VAL STR) evaluates to non-nil." :type '(alist :key-type (symbol :tag "Operator") :value-type function)) (defun kubed-list-interpret-atomic-filter (atom) diff --git a/kubed.texi b/kubed.texi index 86be0b5..8642f40 100644 --- a/kubed.texi +++ b/kubed.texi @@ -428,13 +428,14 @@ conditions are hidden, so you can focus on those that do. Filters can be simple (@dfn{atomic filters}) or complex (@dfn{composed filters}). In the simple case, you enter a filter in the minibuffer in the format @w{@samp{@var{op} @var{col} @var{val}}}, where @var{op} -is a comparison operator, either @samp{=} or @samp{~}, @var{col} is a -column name, and @var{val} is a value to compare to values of -@var{col} with @var{op}. +is a comparison operator, one of @samp{=}, @samp{~}, @samp{<} and +@samp{>}; @var{col} is a column name; and @var{val} is a value to +compare to values of @var{col} with @var{op}. If @var{op} is @samp{=}, it says to keep only lines whose @var{col} equals @var{val}. If @var{op} is @samp{~}, it says to keep lines -whose @var{col} matches the @var{val} as a regular expression. For +whose @var{col} matches the @var{val} as a regular expression. +@samp{<} and @samp{>} compare column values to numeric values. For example, the filter @samp{= Name foobar} keeps only resources whose name is @samp{foobar}. To include whitespace in @var{val}, wrap @var{val} in double quotes: @samp{= Name "foo bar"}. -- 2.39.2