irongit

A Neovim plugin enabling you to solve LeetCode problems.

82 lines1.8 KBLua
1local NuiLine = require("nui.line")
2local NuiText = require("nui.text")
3local Opts = require("leetcode-ui.opts")
4local utils = require("leetcode-ui.utils")
5local log = require("leetcode.logger")
6
7---@class lc.ui.Line : NuiLine
8local Line = NuiLine:extend("LeetLine")
9
10function Line:contents()
11 return self._texts
12end
13
14function Line:longest()
15 if self.class.name == "LeetLine" then
16 return vim.api.nvim_strwidth(self:content())
17 end
18
19 local max_len = 0
20 for _, item in pairs(self:contents()) do
21 max_len = math.max(Line.longest(item), max_len)
22 end
23
24 return max_len
25end
26
27---@param layout lc.ui.Renderer
28---@param opts lc.ui.opts
29function Line:draw(layout, opts)
30 local texts = utils.shallowcopy(self:contents())
31
32 local options = Opts(self._.opts):merge(opts)
33 local pad = options:get_padding()
34
35 if pad then
36 if pad.left then
37 table.insert(texts, 1, NuiText((" "):rep(pad.left)))
38 elseif pad.right then
39 table.insert(texts, NuiText((" "):rep(pad.right)))
40 end
41 end
42
43 local line_idx = layout:get_line_idx(1)
44 NuiLine(texts):render(layout.bufnr, -1, line_idx, line_idx)
45end
46
47function Line:clear() --
48 self._texts = {}
49
50 return self
51end
52
53function Line:replace(texts)
54 self._texts = texts
55
56 return self
57end
58
59function Line:append(content, highlight)
60 Line.super.append(self, content, highlight or self._.opts.hl)
61
62 return self
63end
64
65---@param opts? lc.ui.opts
66function Line:set_opts(opts) --
67 self._.opts = vim.tbl_deep_extend("force", self._.opts or {}, opts or {})
68end
69
70---@param texts? NuiText[]
71---@param opts? lc.ui.opts
72function Line:init(texts, opts) --
73 self._ = {}
74 self:set_opts(opts)
75
76 Line.super.init(self, texts)
77end
78
79---@type fun(texts?: NuiText[], opts?: lc.ui.opts): lc.ui.Line
80local LeetLine = Line
81
82return LeetLine