Skip to content

Commit

Permalink
feat(alpha): Show a quote from zenquotes.io
Browse files Browse the repository at this point in the history
This feature is disabled because it causes a small lag from the http
call when starting Neovim. You can enable it in your user config
with plugins.alpha.quote = true
  • Loading branch information
Allaman committed Jul 10, 2024
1 parent e99210e commit 64e4d5b
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lua/core/plugins/alpha/alpha.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 64e4d5b

Please sign in to comment.