diff --git a/lua/core/plugins/alpha/alpha.lua b/lua/core/plugins/alpha/alpha.lua index 3c99b0e8..92d32d6c 100644 --- a/lua/core/plugins/alpha/alpha.lua +++ b/lua/core/plugins/alpha/alpha.lua @@ -5,6 +5,35 @@ if not status_ok then return end +---Make a HTTP request to https://zenquotes.io to get a random quote +---@return string +local function get_quote() + local found_curl, curl = pcall(require, "plenary.curl") + if not found_curl then + error("plenary not found") + return "" + end + + local response = curl.get("https://zenquotes.io/api/random", { + headers = { + ["User-Agent"] = "curl/7.68.0", + }, + }) + + if response.status ~= 200 then + error("Http failed with " .. response.status, 1) + return "" + end + + local json_data = vim.json.decode(response.body, {}) + if json_data == {} or json_data == nil then + error("empty json from quotes API decoded") + return "" + end + + return json_data[1].q +end + ---set a specific or a random header local function get_header() if utils.safe_nested_config(conf, "alpha", "header") then @@ -36,6 +65,15 @@ for _, button in ipairs(dashboard.section.buttons.val) do button.opts.hl_shortcut = "AlphaShortcut" end +-- default is not enabled because of lag from the http call +if conf.alpha.quote then + -- Do not run when a file is directly opened from the command line + -- and alpha is not even shown + if vim.fn.argc(-1) == 0 then + dashboard.section.footer.val = { get_quote() } + end +end + dashboard.section.header.opts.hl = "AlphaHeader" dashboard.section.buttons.opts.hl = "AlphaButtons" dashboard.section.footer.opts.hl = "AlphaFooter"