goolord / alpha-nvim

a lua powered greeter like vim-startify / dashboard-nvim
MIT License
1.82k stars 109 forks source link

feat: command output as a header #124

Closed olimorris closed 2 years ago

olimorris commented 2 years ago

Firstly, I want to say this is heavily inspired by the awesome work on #63 by Ben. This pull request builds on that. Secondly, under no pressure to merge this into main at all. This is simply my itch that I wanted to scratch :wink:.

Screen Shot 2022-07-13 at 19 47 10@2x

Goal

The primary aim of this pull request is to create a simple API (fitting in with the existing API) which displays the output from a command as my header in my existing dashboard.

Context

I tried out dashboard.nvim again recently and the only positive it has over this plugin is its ability to display the output of a given command. It turned out it mattered to me quite a lot as I chase a glorious aesthetic of a Neovim dashboard.

Summary

I analysted #63 quite significantly alongside the dashboard.nvim implementation and in truth, borrowed a significant chunk of the actual logic to display the command from the latter. Changes to alpha.lua are pretty minimal as most of the logic resides in a term.lua file.

But...in essence, the user passes a command to alpha-nvim which then runs it asynchronously before displaying it.

My config

My config which I used in testing:

  local dashboard = require("alpha.themes.dashboard")

  -- Terminal header
  dashboard.section.terminal.command = "cat | lolcat -F 0.3 " .. os.getenv("HOME") .. "/.config/nvim/static/neovim.cat"
  dashboard.section.terminal.opts = {
    width = 69,
    height = 8,
  }

  local function button(sc, txt, keybind, keybind_opts)
    local b = dashboard.button(sc, txt, keybind, keybind_opts)
    b.opts.hl = "AlphaButtonText"
    b.opts.hl_shortcut = "AlphaButtonShortcut"
    return b
  end
  dashboard.section.buttons.val = {
    button("l", "   Load session", ':lua require("persisted").load()<CR>'),
    button("s", "   Find session", ":Telescope persisted<CR>"),
    button("n", "   New file", ":ene <BAR> startinsert <CR>"),
    button("b", "   Bookmarks", ":Telescope harpoon marks<CR>"),
    button("r", "   Recently used files", ":Telescope frecency<CR>"),
    button("f", "   Find file", ":Telescope find_files hidden=true path_display=smart<CR>"),
    button("u", "   Update plugins", ":lua om.PackerSync()<CR>"), -- Packer sync
    button("q", "   Quit Neovim", ":qa<CR>"),
  }
  dashboard.section.buttons.opts = {
    spacing = 0,
  }

  -- Footer
  local function footer()
    local total_plugins = #vim.tbl_keys(packer_plugins)
    local version = vim.version()
    local nvim_version_info = "  Neovim v" .. version.major .. "." .. version.minor .. "." .. version.patch

    return " " .. total_plugins .. " plugins" .. nvim_version_info
  end
  dashboard.section.footer.val = footer()
  dashboard.section.footer.opts.hl = "AlphaFooter"

  -- Layout
  dashboard.config.layout = {
    { type = "padding", val = 1 },
    dashboard.section.terminal,
    { type = "padding", val = 9 },
    dashboard.section.buttons,
    { type = "padding", val = 1 },
    dashboard.section.footer,
  }

  dashboard.opts.opts.noautocmd = true

  alpha.setup(dashboard.opts)

with neovim.cat being the file I serve up with lolcat.

Next steps

There's not a whole lot else I would add to this so keen to seek your views on how we could make it slicker or more in keeping with your API. Finally, thanks for such a great and well written plugin.

goolord commented 2 years ago

https://user-images.githubusercontent.com/24906808/178824631-6fb8b792-7a9a-41b6-841d-cb699e4a6e6b.mp4

there's a few other things i'll do to clean this up later & add some documentation, but other than that yeah this is a decent simplification of this feature

goolord commented 2 years ago

i'm unconvinced this is for the best but i pushed this commit https://github.com/goolord/alpha-nvim/pull/124/commits/8c373b14ab3fd8dbfdcd34526f6e92055636c266 which pushes all of the logic for the terminal type into alpha.term. pros

cons

    local alpha = require'alpha'
    require'alpha.term'
goolord commented 2 years ago

since it's its own file, anyone who wants to use this before it gets merged can just copy the file somewhere and use it today 👍

olimorris commented 2 years ago

I much prefer shifting all of the logic away from alpha.lua. Keeps it much cleaner. The require("alpha.term") makes sense too.

As for the damn scrolling 😆 . Tell me about it! I'm out of ideas on that. Dashboard hasn't worked it out either.

olimorris commented 2 years ago

My workflow is I use the shortcuts to access buttons in my dashboard so don't even use j or k.

I would love to be able to use your nifty longest_line method combined with total lines in a file to auto-populate the width and height.

olimorris commented 2 years ago

You can do some weird stuff with lolcat 😆 :

https://user-images.githubusercontent.com/9512444/178848403-796b3093-c23c-4c1f-8f27-845e80cb12b8.mp4

goolord commented 2 years ago

i changed the api around a little to make it more consistent with the other element types, now you write

  dashboard.section.terminal.command = "cat | lolcat -F 0.3 " .. os.getenv("HOME") .. "/.config/nvim/static/neovim.cat"
  dashboard.section.terminal.width = 69
  dashboard.section.terminal.height = 8

and the window configuration lives in opts.window_configuration

olimorris commented 2 years ago

Just pushed a fix to term.lua as the vim.tbl_extend in the last commit was throwing an error when I amended my config.

New API is brilliant btw.

Pytness commented 1 year ago
  • the terminal does show a little 'process exited with code x' string at the bottom of the command, but maybe that's not a huge deal since you have to specify the height anyways

My solution to this is to use a pipe to cat so the process never exits. cat | lolcat banner.txt cat | cat myfile