Files
dep/lua/dep/ui/init.lua

208 lines
4.8 KiB
Lua

local h = require("dep.helpers")
local page = require("dep.ui.page")
local logger = require("dep.log")
local format = require("dep.ui.format")
---@class ui
---@field bufnr number
---@field winnr number
---@field header_bufnr number
---@field header_winnr number
---@field pages page[]
local ui = {}
-- the active page being displayed
local active_page
-- all the pages
local pages = {}
-- the header ext mark
local header_ext_id
local function page_packages(packager)
local p = page:new("Packages", "P")
for _, pkg in pairs(packager.get_packages()) do
p:new_line({
{ pkg.id, "@conditional" },
{ " loaded: ", "" },
format.bool(pkg.loaded),
{ " lazy: ", "" },
format.bool(pkg.lazy)
})
end
return p
end
local function page_log()
local p = page:new("Log", "L")
local f = io.open(logger.path, "r")
if not f then
return
end
-- put the cursor at the bottom of the page after drawing
p.post_draw = function()
if ui.winnr then
vim.api.nvim_win_set_cursor(ui.winnr, { #p.content, 0 })
end
end
-- read in the contents of the file, and keep watching for updates
local function update_contents()
repeat
local line = f:read("*l")
-- if the line isn't empty we shouldn't draw it
if line then
p:new_line(format.log_line(line))
end
until not line
end
update_contents()
local fullpath = vim.api.nvim_call_function(
"fnamemodify", { logger.path, ":p" })
h.uv.new_fs_event():start(fullpath, {}, vim.schedule_wrap(function()
update_contents()
-- if the log is currently being displayed then make sure to draw it when
-- it updates
if active_page == p then
p:draw(ui.bufnr)
end
end))
return p
end
--- set the current page
---@param p string|page page to set
function ui.set_page(p)
if type(p) == "string" then
for _, v in ipairs(pages) do
if p == v.kb then
v:draw(ui.bufnr)
active_page = v
break
end
end
elseif type(p) == "table" then
p:draw(ui.bufnr)
active_page = p
end
-- color the header text
local txt = vim.api.nvim_buf_get_text(ui.header_bufnr, 0, 0, -1, -1, {})[1]
local start_range = (string.find(txt, active_page.name)) - 2
local end_range = #active_page.name + start_range + 2
if header_ext_id then
vim.api.nvim_buf_del_extmark(ui.header_bufnr, active_page.hlns, header_ext_id)
end
header_ext_id = vim.api.nvim_buf_set_extmark(ui.header_bufnr,
active_page.hlns, 0, start_range, {
hl_mode = "replace",
hl_group = "CurSearch",
end_col = end_range,
})
end
local setup
--- setup all the pages
---@param packager package the packager
local function setup_pages(packager)
if setup then
return
end
local header_text = ""
table.insert(pages, page_packages(packager))
table.insert(pages, page_log())
for _, v in ipairs(pages) do
header_text = header_text.." "..v.name.." "
vim.keymap.set("n", v.kb, function()
ui.set_page(v)
end, { buffer = ui.bufnr })
end
-- set the header text
vim.api.nvim_buf_set_lines(ui.header_bufnr, 0, -1, false, { header_text })
-- add keymaps
vim.keymap.set("n", "q", function()
vim.api.nvim_win_close(ui.winnr, false)
ui.winnr = nil
end, { buffer = ui.bufnr })
setup = true
end
--- setup the ui
---@param packager package
function ui.open(packager)
if not ui.bufnr then
ui.bufnr = vim.api.nvim_create_buf(false, true)
end
if not ui.header_bufnr then
ui.header_bufnr = vim.api.nvim_create_buf(false, true)
end
local header_height = 1
local width = math.floor(vim.o.columns * 0.8)
local height = math.floor(vim.o.lines * 0.8) - header_height
if not ui.winnr then
ui.winnr = vim.api.nvim_open_win(ui.bufnr, true, {
relative = "editor",
row = (vim.o.lines - height) / 2,
col = (vim.o.columns - width) / 2,
width = width,
height = height,
border = "solid",
zindex = 998,
})
end
if not ui.header_winnr then
ui.header_winnr = vim.api.nvim_open_win(ui.header_bufnr, false, {
relative = "editor",
row = ((vim.o.lines - height) / 2) - (header_height * 2),
col = (vim.o.columns - width) / 2,
width = width,
height = header_height,
border = "solid",
zindex = 999,
focusable = false
})
end
vim.api.nvim_win_set_buf(ui.winnr, ui.bufnr)
vim.api.nvim_win_set_buf(ui.header_winnr, ui.header_bufnr)
-- make sure the header closes when the body does and vice versa
local function cb()
vim.api.nvim_win_close(ui.header_winnr, false)
ui.header_winnr = nil
vim.api.nvim_win_close(ui.winnr, false)
ui.winnr = nil
end
vim.api.nvim_create_autocmd("WinClosed", {
pattern = ui.winnr.."",
callback = cb
})
vim.api.nvim_create_autocmd("WinClosed", {
pattern = ui.header_winnr.."",
callback = cb
})
setup_pages(packager)
end
return ui