-- The start screen: the mark, the name and version, and what to do next, one key each. -- Shown when the IDE starts without a file; ih brings it back. local M = {} local ns = vim.api.nvim_create_namespace("llembas_ide_dashboard") local function act(fn) return function() vim.schedule(fn) end end -- { key, what it does, action } M.actions = { { "n", "New file", function() vim.cmd.enew() end }, { "f", "Find a file", function() require("mini.pick").builtin.files() end }, { "r", "Recent files", function() require("mini.extra").pickers.oldfiles() end }, { "s", "Restore this directory's session", function() require("llembas_ide.sessions").restore() end }, { "l", "LLeMbas chat", function() require("llembas_ide.llembas").cmd("LLeMbasPrompt")() end }, { "i", "Sign in to LLeMbas", function() require("llembas_ide.llembas").sign_in() end }, { "k", "Keys", function() require("llembas_ide.keys").show() end }, { "d", "Check the installation", function() vim.cmd("LLeMbasIdeDoctor") end }, { "q", "Quit", function() vim.cmd("confirm qall") end }, } local function first_run() local marker = vim.fn.stdpath("state") .. "/welcomed" if vim.fn.filereadable(marker) == 1 then return false end vim.fn.mkdir(vim.fn.stdpath("state"), "p") pcall(vim.fn.writefile, { os.date("%Y-%m-%d") }, marker) return true end -- The block: the mark, the name and the actions share one column of a known width, so the keys -- line up by construction. Notes are short lines centred on their own. local ACTION_WIDTH = 38 --- The screen's rows: { text, { {from, to, group}, … }, in_block }. function M.content(welcome) local ide, brand = require("llembas_ide"), require("llembas_ide.brand") local llembas = require("llembas_ide.llembas") local rows = {} local function row(text, marks, in_block) table.insert(rows, { text, marks or {}, in_block == true }) end local function inside(text) -- centred within the block's own width return string.rep(" ", math.max(0, math.floor((ACTION_WIDTH - vim.fn.strdisplaywidth(text)) / 2))) end local function note(text, group) row(text, { { 0, -1, group or "LLeMbasIdeMuted" } }) end for _, pair in ipairs(brand.mark) do local pad = inside(brand.mark[1][1]) row(pad .. pair[1] .. pair[2], { { #pad, #pad + #pair[1], "LLeMbasIdeLeaf" }, { #pad + #pair[1], #pad + #pair[1] + #pair[2], "LLeMbasIdeSign" }, }, true) end row("") local title = brand.name .. " " .. ide.version local pad = inside(title) row(pad .. title, { { #pad, #pad + #brand.name, "LLeMbasIdeTitle" }, { #pad + #brand.name, #pad + #title, "LLeMbasIdeVersion" } }, true) pad = inside(brand.welcome) row(pad .. brand.welcome, { { #pad, -1, "LLeMbasIdeNote" } }, true) row("") for _, a in ipairs(M.actions) do local text = ("%s %-" .. (ACTION_WIDTH - 4) .. "s"):format(a[1], a[2]) row(text, { { 0, 1, "LLeMbasIdeKey" }, { 1, #text, "LLeMbasIdeAction" } }, true) end row("") if not llembas.loaded then note("The LLeMbas plugin is missing. The editor works.") note("Press d to check the installation.") elseif not llembas.has_cli() then note("The LLeMbas CLI is not installed.") note("The editor works without it. Press i.") else -- Whether there is a sign-in is looked up on this machine once; until it is known, and when -- it cannot be told, nothing is claimed either way. local signed = llembas.signed_in(M.redraw) if signed == true then note("LLeMbas is signed in: press l to chat.") elseif signed == false then note("LLeMbas needs a server and a sign-in: press i.") note("The editor works without them.") else note("LLeMbas: press l to chat, i to sign in.") end end if welcome then row("") note("Welcome. is the leader key:") note("press it and wait to see what follows.") note("Your own settings: iu") note("Nothing leaves this machine until you use LLeMbas.") end if #ide.problems > 0 then row("") note((#ide.problems == 1 and "1 part" or (#ide.problems .. " parts")) .. " did not start: :LLeMbasIdeProblems", "LLeMbasIdeWarn") end return rows end local function draw(buf, win, welcome) local rows = M.content(welcome) local width, height = vim.api.nvim_win_get_width(win), vim.api.nvim_win_get_height(win) local block_pad = math.max(0, math.floor((width - ACTION_WIDTH) / 2)) local pad_top = math.max(0, math.floor((height - #rows) / 2)) local lines, pads = {}, {} for _ = 1, pad_top do table.insert(lines, "") end for i, r in ipairs(rows) do pads[i] = r[3] and block_pad or math.max(0, math.floor((width - vim.fn.strdisplaywidth(r[1])) / 2)) table.insert(lines, r[1] == "" and "" or string.rep(" ", pads[i]) .. r[1]) end vim.bo[buf].modifiable = true vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines) vim.bo[buf].modifiable = false vim.bo[buf].modified = false vim.api.nvim_buf_clear_namespace(buf, ns, 0, -1) for i, r in ipairs(rows) do for _, m in ipairs(r[2]) do vim.api.nvim_buf_add_highlight(buf, ns, m[3], pad_top + i - 1, pads[i] + m[1], m[2] == -1 and -1 or pads[i] + m[2]) end end end --- Draw the start screen again where it is shown (its notes changed). function M.redraw() for _, w in ipairs(vim.api.nvim_list_wins()) do local b = vim.api.nvim_win_get_buf(w) if vim.bo[b].filetype == "llembas-ide-dashboard" then draw(b, w, vim.b[b].llembas_ide_welcome == true) end end end function M.open(welcome) local buf = vim.api.nvim_create_buf(false, true) vim.b[buf].llembas_ide_welcome = welcome == true vim.bo[buf].bufhidden = "wipe" vim.bo[buf].buftype = "nofile" vim.bo[buf].swapfile = false vim.bo[buf].filetype = "llembas-ide-dashboard" vim.api.nvim_set_current_buf(buf) local win = vim.api.nvim_get_current_win() for opt, value in pairs({ number = false, relativenumber = false, cursorline = false, signcolumn = "no", list = false, wrap = false }) do vim.api.nvim_set_option_value(opt, value, { scope = "local", win = win }) end draw(buf, win, welcome == true) for _, a in ipairs(M.actions) do vim.keymap.set("n", a[1], act(a[3]), { buffer = buf, nowait = true, silent = true, desc = a[2] }) end local group = vim.api.nvim_create_augroup("llembas_ide_dashboard_" .. buf, { clear = true }) -- Drawn again when the screen changes size, and when the bars beside it have been laid out. vim.api.nvim_create_autocmd({ "VimResized", "User" }, { group = group, pattern = { "*", "LLeMbasLayout" }, callback = function(ev) if ev.event == "User" and ev.match ~= "LLeMbasLayout" then return end local w = vim.fn.bufwinid(buf) if w ~= -1 then draw(buf, w, welcome == true) end end, }) vim.api.nvim_create_autocmd("BufWipeout", { group = group, buffer = buf, callback = function() pcall(vim.api.nvim_del_augroup_by_id, group) end, }) return buf end function M.setup() vim.api.nvim_create_autocmd("VimEnter", { group = vim.api.nvim_create_augroup("llembas_ide_dashboard", { clear = true }), once = true, callback = function() -- Only on an empty start: no file named, nothing piped in, nothing typed into the buffer. if vim.fn.argc() > 0 or vim.g.llembas_ide_no_dashboard then return end if vim.api.nvim_buf_get_name(0) ~= "" or vim.bo.modified or vim.bo.filetype ~= "" then return end if vim.api.nvim_buf_line_count(0) > 1 or vim.api.nvim_buf_get_lines(0, 0, 1, false)[1] ~= "" then return end M.open(first_run()) end, }) end return M