1
0
Fork 0
dotfiles/home/config/nvim/nifoc/statusline.fnl

307 lines
15 KiB
Text
Raw Normal View History

2022-05-31 13:46:32 +00:00
(import-macros {: deferred_cmd} :../macros/cmd)
2022-05-25 20:34:02 +00:00
(let [mod {}
2022-05-25 18:19:11 +00:00
api vim.api
heirline-utils (require :heirline.utils)
heirline-conditions (require :heirline.conditions)
web-devicons (require :nvim-web-devicons)
2022-08-29 20:36:11 +00:00
colors (. (require :nifoc.theme) :colors)
2022-05-31 13:46:32 +00:00
formatting (require :nifoc.formatting)
2022-08-24 20:39:31 +00:00
nifoc-treesitter (require :nifoc.treesitter)]
2022-04-05 19:57:44 +00:00
(fn buffer-variable-exists? [key]
2022-05-25 18:19:11 +00:00
(not= (. vim :b key) nil))
2022-04-05 19:57:44 +00:00
2022-05-25 18:19:11 +00:00
(fn get-current-line []
(let [cl (api.nvim_win_get_cursor 0)]
(. cl 1)))
2022-04-05 19:57:44 +00:00
2022-05-25 18:19:11 +00:00
(fn get-total-lines []
2022-05-25 20:34:02 +00:00
(api.nvim_buf_line_count 0))
2022-04-05 19:57:44 +00:00
2022-08-29 20:36:11 +00:00
(fn max-number [nums]
(math.max (unpack nums)))
2022-05-25 18:19:11 +00:00
;; Utils
2022-05-25 20:34:02 +00:00
(set mod.default-hl (fn []
{:bg colors.black}))
(set mod.space {:provider " "})
(set mod.spacer {:provider " " :hl {:fg colors.bg :bg colors.bg}})
(set mod.push-right {:provider "%="})
2022-08-29 20:36:11 +00:00
(set mod.space-if-count {:condition #(> $1.check-count 0) :provider " "})
(set mod.space-if-length {:condition #(> $1.check-length 0) :provider " "})
(set mod.space-if-count-or-length
{:condition #(or (> $1.check-count 0) (> $1.check-length 0))
:provider " "})
2022-05-25 18:19:11 +00:00
;; Mode
2022-05-25 20:34:02 +00:00
(set mod.vi-mode {:init (fn [self]
(let [mode (. (api.nvim_get_mode) :mode)]
(set self.mode mode)))
2022-06-05 20:51:14 +00:00
:update :ModeChanged
2022-05-25 20:34:02 +00:00
:static {:mode-names {:n :NORMAL
:no :O-PENDING
:nov :O-PENDING
:noV :O-PENDING
"no\022" :O-PENDING
:niI :NORMAL
:niR :NORMAL
:niV :NORMAL
:nt :NORMAL
:v :VISUAL
:vs :VISUAL
:V :V-LINE
:Vs :V-LINE
"\022" :V-BLOCK
"\022s" :V-BLOCK
:s :SELECT
:S :S-LINE
"\019" :S-BLOCK
:i :INSERT
:ic :INSERT
:ix :INSERT
:R :REPLACE
:Rc :REPLACE
:Rx :REPLACE
:Rv :V-REPLACE
:Rvc :V-REPLACE
:Rvx :V-REPLACE
:c :COMMAND
:cv :EX
:ce :EX
:r :REPLACE
:rm :MORE
:r? :CONFIRM
:! :SHELL
:t :TERMINAL}
:mode-hl {:n {:fg colors.black
:bg colors.purple
:bold true}
:i {:fg colors.black
:bg colors.green
:bold true}
:v {:fg colors.black
:bg colors.pink
:bold true}
:V {:fg colors.black
:bg colors.pink
:bold true}
"\022" {:fg colors.black
:bg colors.pink
:bold true}
:c {:fg colors.black
:bg colors.yellow
:bold true}
:s {:fg colors.black
:bg colors.orange
:bold true}
:S {:fg colors.black
:bg colors.orange
:bold true}
"\019" {:fg colors.black
:bg colors.orange
:bold true}
:R {:fg colors.black
:bg colors.yellow
:bold true}
:r {:fg colors.black
:bg colors.yellow
:bold true}
:! {:fg colors.black
:bg colors.purple
:bold true}
:t {:fg colors.black
:bg colors.purple
:bold true}}}
2022-05-31 13:46:32 +00:00
:provider #(.. " " (. $1 :mode-names $1.mode) " ")
2022-05-25 20:34:02 +00:00
:hl (fn [self]
(let [short-mode (self.mode:sub 1 1)]
(. self :mode-hl short-mode)))})
2022-05-25 18:19:11 +00:00
;; Filetype
2022-05-25 20:34:02 +00:00
(set mod.filetype-block
2022-05-25 18:19:11 +00:00
{:init (fn [self]
(set self.filename (api.nvim_buf_get_name 0)))})
2022-05-25 20:34:02 +00:00
(set mod.file-icon {:init (fn [self]
(let [filename self.filename
ext (vim.fn.fnamemodify filename ":e")
(icon color) (web-devicons.get_icon_color filename
ext
{:default true})]
(set self.icon icon)
(set self.icon-color color)))
:provider (fn [self]
(when self.icon
(.. self.icon " ")))
:hl (fn [self]
{:fg self.icon-color})})
(set mod.filetype {:provider (fn []
(let [ft vim.bo.filetype]
(if (> (ft:len) 0) ft "no ft")))
:hl {:fg colors.white}})
(set mod.encoding {:provider (fn []
(let [vim-enc (if (not= vim.bo.fenc nil)
vim.bo.fenc
vim.o.enc)
enc (if (> (vim-enc:len) 0) vim-enc
"no enc")]
(.. enc " ")))
:hl {:fg colors.white}})
2022-05-25 20:34:02 +00:00
(set mod.filetype-block
(heirline-utils.insert mod.filetype-block mod.file-icon mod.filetype
{:provider " | " :hl {:fg colors.white}}
mod.encoding))
2022-05-25 18:19:11 +00:00
;; git
2022-05-25 20:34:02 +00:00
(set mod.git {:condition heirline-conditions.is_git_repo
:init (fn [self]
(let [git-status vim.b.gitsigns_status_dict]
(set self.git-head git-status.head)
(set self.git-added (or git-status.added 0))
(set self.git-removed (or git-status.removed 0))
2022-08-29 20:36:11 +00:00
(set self.git-changed (or git-status.changed 0))
(set self.check-count
(max-number [self.git-added
self.git-removed
self.git-changed]))
(set self.check-length (length self.git-head))))
1 mod.space-if-count-or-length
2 {:provider #(.. "  " $1.git-head " ")
2022-08-24 20:39:31 +00:00
:hl {:fg colors.black :bg colors.orange :bold true}}
2022-08-29 20:36:11 +00:00
3 mod.space
4 {:provider (fn [self]
2022-06-05 20:51:14 +00:00
(.. " " self.git-added " "))
2022-05-25 20:34:02 +00:00
:hl {:fg colors.bright_green}}
2022-08-29 20:36:11 +00:00
5 {:provider (fn [self]
2022-06-05 20:51:14 +00:00
(.. " " self.git-removed " "))
2022-05-25 20:34:02 +00:00
:hl {:fg colors.bright_red}}
2022-08-29 20:36:11 +00:00
6 {:provider (fn [self]
2022-06-05 20:51:14 +00:00
(.. " " self.git-changed))
2022-05-25 20:34:02 +00:00
:hl {:fg colors.cyan}}})
2022-05-25 18:19:11 +00:00
;; Diagnostics
2022-05-25 20:34:02 +00:00
(set mod.diagnostics {:condition heirline-conditions.has_diagnostics
:init (fn [self]
(let [d vim.diagnostic]
(set self.errors
(length (d.get 0
{:severity d.severity.ERROR})))
(set self.warnings
(length (d.get 0
{:severity d.severity.WARN})))
(set self.info
(length (d.get 0
{:severity d.severity.INFO})))
(set self.hints
(length (d.get 0
2022-08-29 20:36:11 +00:00
{:severity d.severity.HINT})))
(set self.check-count
(max-number [self.errors
self.warnings
self.info
self.hints]))))
2022-06-05 20:51:14 +00:00
:update [:DiagnosticChanged :BufEnter]
2022-08-29 20:36:11 +00:00
1 mod.space-if-count
2 {:provider (fn [self]
2022-05-25 20:34:02 +00:00
(let [spacer (if (or (> self.warnings 0)
(> self.info 0)
(> self.hints 0))
" " "")]
(when (> self.errors 0)
(.. " " self.errors spacer))))
:hl {:fg colors.red}}
2022-08-29 20:36:11 +00:00
3 {:provider (fn [self]
2022-05-25 20:34:02 +00:00
(let [spacer (if (or (> self.info 0)
(> self.hints 0))
" " "")]
(when (> self.warnings 0)
(.. " " self.warnings spacer))))
:hl {:fg colors.yellow}}
2022-08-29 20:36:11 +00:00
4 {:provider (fn [self]
2022-05-25 20:34:02 +00:00
(let [spacer (if (> self.hints 0) " " "")]
(when (> self.info 0)
(.. " " self.info spacer))))
:hl {:fg colors.cyan}}
2022-08-29 20:36:11 +00:00
5 {:provider (fn [self]
2022-05-25 20:34:02 +00:00
(when (> self.hints 0)
(.. " " self.hints)))
:hl {:fg colors.cyan}}})
2022-05-25 18:19:11 +00:00
;; Current Function
2022-05-25 20:34:02 +00:00
(set mod.current-function
2022-05-25 18:19:11 +00:00
{:condition heirline-conditions.lsp_attached
2022-08-29 20:36:11 +00:00
:init (fn [self]
(let [ctx vim.b.nifoc_lsp_current_context]
(if (and (not= ctx nil) (> (ctx:len) 0))
(do
(set self.current-ctx ctx)
(set self.check-length 1))
(do
(set self.current-ctx nil)
(set self.check-length 0)))))
1 mod.space-if-length
2 {:provider (fn [self]
self.current-ctx)
:hl {:fg colors.white}}})
2022-05-25 18:19:11 +00:00
;; Buffer Options
(set mod.buffer-options
{:static {:format {:dos "" :unix "" :mac ""}}
:hl {:fg colors.black :bg colors.orange}
1 mod.space
2022-05-31 13:46:32 +00:00
2 {:condition #(buffer-variable-exists? :nifoc_lsp_enabled)
:provider " "
:on_click {:name :heirline_buffer_options_lsp
:callback #(deferred_cmd {:cmd :LspInfo} 200)}}
3 {:condition #(formatting.active?) :provider " "}
4 {:condition #(nifoc-treesitter.active?) :provider " "}
5 {:condition #vim.wo.spell :provider "暈"}
6 {:provider (fn [self]
(let [f vim.bo.fileformat]
(.. (. self :format f) " ")))}})
2022-05-25 18:19:11 +00:00
;; Position
2022-05-25 20:34:02 +00:00
(set mod.position
2022-05-25 18:19:11 +00:00
{:init (fn [self]
2022-05-25 20:34:02 +00:00
(let [pos (api.nvim_win_get_cursor 0)]
(set self.position-line (tostring (. pos 1)))
(set self.position-column (tostring (. pos 2)))))
2022-05-25 18:19:11 +00:00
:provider (fn [self]
2022-05-25 20:34:02 +00:00
(string.format " %3s:%-3s " self.position-line
self.position-column))
:hl {:fg colors.black :bg colors.purple :bold true}})
;; Scrollbar
(set mod.scrollbar {:init (fn [self]
(set self.current-line (get-current-line))
(set self.total-lines (get-total-lines)))
:static {:scrollbar-icons ["🭶"
"🭷"
"🭸"
"🭹"
"🭺"
"🭻"]}
:provider (fn [self]
(let [i (+ (math.floor (* (/ (- self.current-line
1)
self.total-lines)
(length self.scrollbar-icons)))
1)
new-scrollbar (. self :scrollbar-icons
i)]
(string.rep new-scrollbar 2)))
:hl {:fg colors.purple}})
;; Search count
(set mod.search-count
{:condition #(> vim.v.hlsearch 0)
:init (fn [self]
(set self.count (vim.fn.searchcount {:timeout 5})))
:provider (fn [self]
(string.format "[%s/%s]" self.count.current
self.count.total))
:hl {:fg colors.black :bg colors.purple}})
2022-05-25 18:19:11 +00:00
;; Custom Mode
2022-04-05 19:57:44 +00:00
2022-05-25 20:34:02 +00:00
(fn mod.custom-mode [str fg bg]
2022-05-25 18:19:11 +00:00
{:provider (.. " " str " ")
2022-05-25 20:34:02 +00:00
:hl {:fg (. colors fg) :bg (. colors bg) :bold true}})
(fn mod.shell-mode [fg bg]
{:provider (fn []
(.. " " vim.b.nifoc_shell_mode " "))
:hl {:fg (. colors fg) :bg (. colors bg) :bold true}})
2022-05-07 21:31:42 +00:00
2022-04-05 19:57:44 +00:00
mod)