From 9429f9447558797bf1852ce52c481ff1445a4c53 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Mon, 13 Jan 2025 01:33:01 +0100 Subject: try vtable on cmk package --- elisp/cmk.el | 75 +++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 44 insertions(+), 31 deletions(-) diff --git a/elisp/cmk.el b/elisp/cmk.el index 4df85c2..790aa8b 100644 --- a/elisp/cmk.el +++ b/elisp/cmk.el @@ -9,7 +9,7 @@ ;; Version: 0.0.1 ;; Keywords: abbrev bib c calendar comm convenience data docs emulations extensions faces files frames games hardware help hypermedia i18n internal languages lisp local maint mail matching mouse multimedia news outlines processes terminals tex tools unix vc wp ;; Homepage: https://github.com/titan/cmk -;; Package-Requires: ((emacs "27.1")) +;; Package-Requires: ((emacs "29.1")) ;; ;; This file is not part of GNU Emacs. ;; @@ -18,16 +18,18 @@ ;; CMK livestatus ;; ;;; Code: -(require 'subr-x) + (require 'cl-lib) -(require 'dash) -(require 'company) +;; (require 'company) +(require 'map) (require 'csv-mode) +(require 'dash) +(require 'subr-x) +(require 'vtable) (defun cmk-state-coloring (state) "Font color for numeric STATE input as string." - (cl-check-type state string) - (pcase (string-to-number state) + (pcase state (0 (propertize "OK" 'face 'font-lock-string-face)) (1 (propertize "WARN" 'face 'font-lock-warning-face)) (2 (propertize "CRIT" 'face 'font-lock-keyword-face)) @@ -65,7 +67,8 @@ Default is \"%Y-%m-%d %H:%M\"." (concat "Columns: " (mapconcat (lambda (column) - (or (plist-get (cdddr column) :column) (car column))) + (or (plist-get column :column) + (plist-get column :name))) spec " "))) @@ -75,25 +78,25 @@ Default is \"%Y-%m-%d %H:%M\"." ("Service" 20 t :column "service_description") ("Since" 16 ,(lambda (a b) (time-less-p - (encode-time (parse-time-string (elt (cadr a) 3))) - (encode-time (parse-time-string (elt (cadr b) 3))))) + (encode-time (parse-time-string (elt (cadr a) 3))) + (encode-time (parse-time-string (elt (cadr b) 3))))) :column "service_last_state_change" :parser cmk-timestamp-to-date) ("Check output" 18 t :column "service_plugin_output" :parser cmk-purge-bangs))) (defconst cmk-log-col-spec - '(;("host_state" 5 t :column "host_state" :parser cmk-state-coloring) - ("STATE" 5 t :column "log_state" :parser cmk-state-coloring) - ;; ("lsttype" 8 t :column "log_state_type") - ("Host" 9 t :column "host_name") - ("Service" 12 t :column "service_description") - ("log_time" 16 t :column "log_time" :parser cmk-timestamp-to-date) - ;; ("log_command_name" 10 t :column "log_command_name") - ;; ("log_comment" 15 t :column "log_comment") - ;; ("lineno" 8 t :column "log_lineno") - ("log_type" 16 t :column "log_type") - ;; ("host_scheduled_downtime_depth" 5 t :column "host_scheduled_downtime_depth") - ;; ("host_has_been_checked" 5 t :column "host_has_been_checked") - ("log_plugin_output" 5 t :column "log_plugin_output"))) + '(;("host_state" :column "host_state" :parser cmk-state-coloring) + (:name "STATE" :min-width 5 :column "log_state" :formatter cmk-state-coloring) + ;; ("lsttype" :column "log_state_type") + (:name "Host" :column "host_name") + (:name "Service" :column "service_description") + (:name "log_time" :min-width 18 :column "log_time" :formatter cmk-timestamp-to-date) + ;; ("log_command_name" :column "log_command_name") + ;; ("log_comment" :column "log_comment") + ;; ("lineno" :column "log_lineno") + (:name "log_type" :column "log_type") + ;; ("host_scheduled_downtime_depth" :column "host_scheduled_downtime_depth") + ;; ("host_has_been_checked" :column "host_has_been_checked") + (:name "log_plugin_output" :column "log_plugin_output"))) (defun cmk-parse-rows (buffer column-spec) "Return tabulated entries from BUFFER according to COLUMN-SPEC." @@ -151,6 +154,7 @@ Default is \"%Y-%m-%d %H:%M\"." (,(concat "^" (regexp-opt cmk-livestatus-headers 'words) ":[[:space:]]") . font-lock-function-name-face) (,(rx bol (or "And" "Or" "Negate") ":" whitespace) . font-lock-keyword-face) (,(rx whitespace (or "~" "=" "=~" "~~" "<" ">" "<=" ">=") whitespace) . font-lock-keyword-face))))) + (defun cmk-custom-livestatus-query () (interactive) (let ((query cmk-livestatus-query) @@ -202,23 +206,32 @@ Default is \"%Y-%m-%d %H:%M\"." (kill-buffer) (switch-to-buffer buffer) (setq-local cmk-livestatus-query new-query) - (cmk-table--refresh))))) + (vtable-revert-command))))) (switch-to-buffer edit-buffer))) (defun cmk-view (table spec filters) "Render a tabular view for livestatus TABLE with SPEC columns using FILTERS." (with-current-buffer (get-buffer-create "*CMK View*") - (tabulated-list-mode) + (fundamental-mode) + (erase-buffer) (setq-local cmk-livestatus-query - (format "GET %s\n%s\n%s\n\n" + (format "GET %s\n%s\n%s\n%s\n\n" table (cmk-colums-from-spec spec) - filters)) - (setq tabulated-list-format (vconcat spec)) - (add-hook 'tabulated-list-revert-hook #'cmk-table--refresh nil t) - (local-set-key "Q" #'cmk-edit-lq) - (tabulated-list-init-header) - (cmk-table--refresh) + filters + "OutputFormat: json")) + (make-vtable + :columns (mapcar (lambda (li) (map-delete (map-copy li) :column)) spec) + :use-header-line nil + :objects-function + (lambda () + (let ((cmks (cmk-livestatus-query cmk-livestatus-query))) + (accept-process-output cmks 0.1) + (with-current-buffer (process-buffer cmks) + (goto-char (point-min)) + (json-parse-buffer :array-type 'list)))) + :keymap (define-keymap + "Q" #'cmk-edit-lq)) (switch-to-buffer (current-buffer)))) (defun cmk-problems () -- cgit v1.2.3