about summary refs log tree commit diff
path: root/fnl/nvrc/utils.fnl
blob: 52ca4543399767ddc7fec802c08f38cd08fe6f78 (plain)
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
(fn tbl? [x]
  (= :table (type x)))

(fn count [xs]
  (if (tbl? xs) (table.maxn xs)
      (not xs) 0
      (length xs)))

(fn run! [f xs]
  "Execute the function (for side effects) for every xs."
  (when xs
    (let [nxs (count xs)]
      (when (> nxs 0)
        (for [i 1 nxs]
          (f (. xs i)))))))

(fn reduce [f init xs]
  "Reduce xs into a result by passing each subsequent value into the fn with
  the previous value as the first arg. Starting with init."
  (var result init)
  (run! (fn [x]
          (set result (f result x))) xs)
  result)

(fn merge! [base ...]
  (reduce (fn [acc m]
            (when m
              (each [k v (pairs m)]
                (tset acc k v)))
            acc) (or base {}) [...]))

(fn merge [...]
  (merge! {} ...))

(fn lsp_fidget []
  "Simple implementation of LSP fidget. Returns current LSP status with a spinner"
  (let [lsp (. (vim.lsp.util.get_progress_messages) 1)]
    (when lsp
      (let [msg (or lsp.message "")
            percentage (or lsp.percentage 0)
            title (or lsp.title "")
            spinners {1 "⠋"
                      2 "⠙"
                      3 "⠹"
                      4 "⠸"
                      5 "⠼"
                      6 "⠴"
                      7 "⠦"
                      8 "⠧"
                      9 "⠇"
                      10 "⠏"}
            success-icon "^.^!"
            ms (/ (vim.loop.hrtime) 1000000)
            frame (% (math.floor (/ ms 120)) (length spinners))]
        (when (>= percentage 70)
          (let [ertn [(string.format " %%<%s %s %s (%s%%%%) " success-icon
                                     title msg percentage)]]
            (lua "return (table.unpack or _G.unpack)(ertn)")))
        (let [ertn [(string.format " %%<%s %s %s (%s%%%%) "
                                   (. spinners (+ frame 1)) title msg percentage)]]
          (lua "return (table.unpack or _G.unpack)(ertn)")))))
  "")

(fn will_it_fit [width winid]
  "Returns whether this module will fit in the given width"
  (> (vim.api.nvim_win_get_width (or (tonumber winid) 0)) width))

(fn del_buf []
  (let [buflisted (vim.fn.getbufinfo {:buflisted 1})
        (cur-winnr cur-bufnr) (values (vim.fn.winnr) (vim.fn.bufnr))]
    (when (< (length buflisted) 2)
      (vim.cmd "confirm qall")
      (lua "return "))
    (each [_ winid (ipairs (. (. (vim.fn.getbufinfo cur-bufnr) 1) :windows))]
      (vim.cmd (string.format "%d wincmd w" (vim.fn.win_id2win winid)))
      (vim.cmd (or (and (= cur-bufnr (. (. buflisted (length buflisted)) :bufnr))
                    :bp) :bn)))
    (vim.cmd (string.format "%d wincmd w" cur-winnr))
    (local is-terminal (= (vim.fn.getbufvar cur-bufnr :&buftype) :terminal))
    (vim.cmd (or (and is-terminal "bd! #") "silent! confirm bd #"))))

(fn defer_unpack [pack after]
  (when pack
    (set-forcibly! after (or after 0))
    (vim.defer_fn (fn []
                    ((. (require :packer) :loader) pack))
                  after)))

{: merge : lsp_fidget : will_it_fit : del_buf : defer_unpack}