nvim-lualine / lualine.nvim

A blazing fast and easy to configure neovim statusline plugin written in pure lua.
MIT License
5.93k stars 461 forks source link

Feat: can we show output of "showcmd" on lualine? #868

Closed isaif closed 1 year ago

isaif commented 1 year ago

Requested feature

Is it possible to show the output of set showcmd on lualine in a component? It shows (partial) command in the last line of screen.

Motivation

If we use set cmdheight=0 to hide the command-line we also lose the ability to see the what pending command we may have entered. It is also helpful while using the relative line number for navigation, we can see if we have entered the correct line number.

shadmansaleh commented 1 year ago

you can try grabbing msg_showmode messages with ui_attach.

barebones example:

local ns = vim.api.nvim_create_namespace('showcmd_msg')
local showcmd_msg
vim.ui_attach(ns, {ext_messages=true}, function(event, ...)
  if event == 'msg_showcmd' then
    local content = ...
    showcmd_msg = #content > 0 and content[1][2] or ''
  end
end)
require'lualine'.setup {
  lualine_c = {function() return showcmd_msg end}
}

other than currently showcmd messages aren't exposed anywhere else. but bfredl did leave a comment on it https://github.com/neovim/neovim/blob/2f9b94a26836ecb081c717e23913f5b6576cce99/src/nvim/normal.c#L2827

it's not too hard to add perhaps someone will expose it in core

for example with this patch applied you can just get the showmode info in statusline with %C

diff --git a/src/nvim/normal.c b/src/nvim/normal.c
index e09fb149b..0e29b8af6 100644
--- a/src/nvim/normal.c
+++ b/src/nvim/normal.c
@@ -2861,6 +2861,9 @@ static void display_showcmd(void)
   grid_puts_line_flush(false);
 }

+const char *get_showcmd(void) {
+  return (const char*)showcmd_buf;
+}
 /// When "check" is false, prepare for commands that scroll the window.
 /// When "check" is true, take care of scroll-binding after the window has
 /// scrolled.  Called from normal_cmd() and edit().
diff --git a/src/nvim/option_defs.h b/src/nvim/option_defs.h
index 50f1bc40d..01700c2a7 100644
--- a/src/nvim/option_defs.h
+++ b/src/nvim/option_defs.h
@@ -343,6 +343,7 @@ enum {
   STL_TABPAGENR       = 'T',  ///< Tab page label nr.
   STL_TABCLOSENR      = 'X',  ///< Tab page close nr.
   STL_CLICK_FUNC      = '@',  ///< Click region start.
+  STL_SHOWCMD         = 'C',  /// showcmd output.
 };
 /// C string containing all 'statusline' option flags
 #define STL_ALL ((char[]) { \
@@ -353,7 +354,7 @@ enum {
     STL_PREVIEWFLAG, STL_PREVIEWFLAG_ALT, STL_MODIFIED, STL_MODIFIED_ALT, \
     STL_QUICKFIX, STL_PERCENTAGE, STL_ALTPERCENT, STL_ARGLISTSTAT, STL_PAGENUM, \
     STL_VIM_EXPR, STL_SEPARATE, STL_TRUNCMARK, STL_USER_HL, STL_HIGHLIGHT, \
-    STL_TABPAGENR, STL_TABCLOSENR, STL_CLICK_FUNC, \
+    STL_TABPAGENR, STL_TABCLOSENR, STL_CLICK_FUNC, STL_SHOWCMD,\
     0, \
   })

diff --git a/src/nvim/statusline.c b/src/nvim/statusline.c
index fb49a1b6a..0fedc8be7 100644
--- a/src/nvim/statusline.c
+++ b/src/nvim/statusline.c
@@ -24,6 +24,7 @@
 #include "nvim/ui.h"
 #include "nvim/undo.h"
 #include "nvim/vim.h"
+#include "nvim/normal.h"
 #include "nvim/window.h"

 // Determines how deeply nested %{} blocks will be evaluated in statusline.
@@ -1404,6 +1405,10 @@ int build_stl_str_hl(win_T *wp, char *out, size_t outlen, char *fmt, int use_san
       }
       continue;
     }
+    case STL_SHOWCMD: {
+      str = get_showcmd();
+      break;
+    }
     }

     // If we made it this far, the item is normal and starts at

I agree this will be good to have in statusline with cmdheight 0 but without nvim properly exposing that info this will not be implemented in lualine. the ui_attach solution pretty much works but it also disables cmdline so with this something will have to implement ext_cmdline portion too that's just irrelevant to lualine. Though you can totally use it with something like noice.nvim . I guess you could try asking nvim to expose e way to display showmode in statusline.

folke commented 1 year ago

Noice exposes a showcmd statusline component as well :)

https://github.com/folke/noice.nvim#-statusline-components