This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add smart component truncation (#132)
- Loading branch information
Showing
3 changed files
with
251 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
-- This module provides an interface to Neovim's statusline generator using Lua FFI | ||
local M = {} | ||
|
||
local ffi = require('ffi') | ||
|
||
-- Definitions required to use Neovim's build_stl_str_hl function to expand statusline expressions | ||
ffi.cdef [[ | ||
typedef unsigned char char_u; | ||
typedef struct window_S win_T; | ||
typedef struct {} stl_hlrec_t; | ||
typedef struct {} StlClickRecord; | ||
|
||
extern win_T *curwin; | ||
|
||
int build_stl_str_hl( | ||
win_T *wp, | ||
char_u *out, | ||
size_t outlen, | ||
char_u *fmt, | ||
int use_sandbox, | ||
char_u fillchar, | ||
int maxwidth, | ||
stl_hlrec_t **hltab, | ||
StlClickRecord **tabtab | ||
); | ||
]] | ||
|
||
-- Used CType values stored in a local variable to avoid redefining them and improve performance | ||
local char_u_buf_t = ffi.typeof('char_u[?]') | ||
local char_u_str_t = ffi.typeof('char_u*') | ||
|
||
-- Statusline string buffer | ||
local stlbuf_len = 256 | ||
local stlbuf = char_u_buf_t(stlbuf_len) | ||
|
||
-- Expand statusline expression, returns a Lua string containing plaintext with only the characters | ||
-- that'll be displayed in the statusline | ||
function M.expand_statusline_expr(expr) | ||
ffi.C.build_stl_str_hl( | ||
ffi.C.curwin, | ||
stlbuf, | ||
stlbuf_len, | ||
ffi.cast(char_u_str_t, expr), | ||
0, | ||
0, | ||
0, | ||
nil, | ||
nil | ||
) | ||
|
||
return ffi.string(stlbuf) | ||
end | ||
|
||
-- Get display width of statusline expression | ||
function M.get_statusline_expr_width(expr) | ||
return tonumber(ffi.C.build_stl_str_hl( | ||
ffi.C.curwin, | ||
stlbuf, | ||
stlbuf_len, | ||
ffi.cast(char_u_str_t, expr), | ||
0, | ||
0, | ||
0, | ||
nil, | ||
nil | ||
)) | ||
end | ||
|
||
return M |