-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpen-prompt-tablist.el
110 lines (92 loc) · 3.8 KB
/
pen-prompt-tablist.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
(require 'tabulated-list)
(require 'pcsv)
(require 'tablist)
(defvar pen-prompts-tablist-data-command "oci prompts-details -csv")
(defvar pen-prompts-tablist-meta '("prompts" t "30 30 20 10 15 15 15 10"))
(defmacro defcmdmode (cmd &optional cmdtype)
(setq cmd (str cmd))
(setq cmdtype (or cmdtype "term"))
(let* ((cmdslug (slugify (str cmd)))
(modestr (concat cmdslug "-" cmdtype "-mode"))
(modesym (intern modestr))
(mapsym (intern (concat modestr "-map"))))
`(progn
(defvar ,mapsym (make-sparse-keymap)
,(concat "Keymap for `" modestr "'."))
(defvar-local ,modesym nil)
(define-minor-mode ,modesym
,(concat "A minor mode for the '" cmd "' " cmdtype " command.")
:global nil
:init-value nil
:lighter ,(s-upcase cmdslug)
:keymap ,mapsym)
(provide ',modesym))))
(defcmdmode "prompts" "tablist")
(defun tablist-buffer-from-csv-string (csvstring &optional has-header col-sizes)
"This creates a new tabulated list buffer from a CSV string"
(let* ((b (nbfs csvstring "tablist"))
(parsed (pcsv-parse-buffer b))
(header (if has-header
(first parsed)
(mapcar (lambda (s) "") (first parsed))))
(data (if has-header
(-drop 1 parsed)
parsed)))
(switch-to-buffer b)
(with-current-buffer b
(setq-local tabulated-list-format
(si "tabulated-list-format format"
(list2vec
(let* ((sizes
(or col-sizes
(mapcar (lambda (e)
(max my-tablist-min-column-width (min 30 (length e))))
header)))
(trues (mapcar (lambda (e) t)
header)))
(-zip header sizes trues))
)))
(setq-local tabulated-list-sort-key (list (first header)))
(setq-local tabulated-list-entries (-map (lambda (l) (list (first l) (list2vec l))) data))
(tabulated-list-mode)
(tabulated-list-init-header)
(tabulated-list-print)
(tablist-enlarge-column)
(tablist-shrink-column)
(revert-buffer)
(tablist-forward-column 1)
(tabulated-list-revert))
b))
(defun create-tablist (cmd-or-csv-path &optional modename has-header col-sizes-string wd)
"Try to create a tablist from a cmd or a csv path"
(interactive (list (read-string-hist "create-tablist: CMD or CSV path: ")))
(setq has-header
(if (sor has-header)
t
nil))
(let* ((path (if (and
(f-file-p cmd-or-csv-path)
(not (f-executable-p cmd-or-csv-path)))
cmd-or-csv-path))
(cmd (if (not path)
cmd-or-csv-path))
(col-sizes
(if (sor col-sizes-string)
(try (mapcar 'string-to-int (uncmd col-sizes-string))))))
(let ((b (cond ((sor path) (tablist-buffer-from-csv-string (cat path) has-header col-sizes))
((sor cmd) (tablist-buffer-from-csv-string (sn cmd) has-header col-sizes)))))
(if b
(with-current-buffer
b
(if (sor wd) (cd wd))
(let ((modefun (intern (concat (slugify (or modename cmd-or-csv-path)) "-tablist-mode"))))
(if (fboundp modefun)
(funcall modefun)))
b)
(error "tablist not created")
nil))))
(defun pen-prompts-tablist-start ()
(interactive)
(let* ((sh-update (>= (prefix-numeric-value current-global-prefix-arg) 16)))
(apply 'create-tablist (cons pen-prompts-tablist-data-command pen-prompts-tablist-meta))))
(provide 'pen-tablist)