From 720c84b4c534acb2e3947f82d1d964aa78f6d53d Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Wed, 3 Jun 2020 17:55:57 -0400 Subject: [PATCH] Add support to view full log in json-navigator --- structlog-mode.el | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/structlog-mode.el b/structlog-mode.el index e5fbf90..6d66be8 100644 --- a/structlog-mode.el +++ b/structlog-mode.el @@ -4,7 +4,9 @@ ;; Author: Jeremy Dormitzer ;; Version: 0.1.0 -;; Package-Requires ((dash "2.17.0") (s "1.12.0")) +;; Package-Requires ((dash "2.17.0") +;; (s "1.12.0") +;; (json-navigator "0.1.1")) ;; Keywords: data ;; This program is free software; you can redistribute it and/or modify @@ -30,6 +32,7 @@ (require 'cl-lib) (require 's) (require 'dash) +(require 'json-navigator) (defvar structlog-fields nil "Currently selected structlog fields") @@ -97,13 +100,17 @@ (defun structlog--parse-json (raw) "Parses the string `raw' as JSON and returns an alist" (if (fboundp 'json-parse-string) - (json-parse-string raw - :object-type 'alist - :null-object nil - :false-object nil) + (json-parse-string raw :object-type 'alist) (require 'json) (json-read-from-string raw))) +(defun structlog--serialize-json (alist) + "Serializes the `alist' to a JSON string" + (if (fboundp 'json-serialize) + (json-serialize alist) + (require 'json) + (json-encode alist))) + (defun structlog--get-query-fields (query) "Returns the fields selected by `query'" (plist-get query :select)) @@ -183,11 +190,20 @@ (new-query (read-string "Query: " query-str))) (structlog-query new-query))) +(defun structlog-view-log-at-point () + "Opens the log at point in a json-navigator buffer" + (interactive) + (let* ((log (tabulated-list-get-id)) + (json (structlog--serialize-json log))) + (json-navigator-display-tree + (json-navigator--read-string json)))) + (defvar structlog-mode-map (let ((map (make-sparse-keymap))) (set-keymap-parent map tabulated-list-mode-map) (define-key map "m" #'structlog-query) (define-key map "M" #'structlog-edit-query) + (define-key map (kbd "RET") #'structlog-view-log-at-point) map) "Local keymap for `structlog-mode' buffers") @@ -195,6 +211,7 @@ (evil-define-key 'normal structlog-mode-map "m" #'structlog-query "M" #'structlog-edit-query + (kbd "RET") #'structlog-view-log-at-point "S" #'tabulated-list-sort "{" #'tabulated-list-narrow-current-column "}" #'tabulated-list-widen-current-column))