clp

command line syntax highlighter
git clone git://jeskin.net/clp.git
README | Log | Files | Refs | LICENSE

commit ad1e0537774dac0ddf7de4d196e78e07d7c86906
parent ec534b8e4ca38ef9afe31fb39374b250aa789dce
Author: Jon Eskin <eskinjp@gmail.com>
Date:   Mon, 29 Aug 2022 06:29:39 -0400

improve line highlighting

Diffstat:
Mlua/ansi_codes.lua | 8+++++---
Mlua/clp.lua | 13+++++++------
Mlua/style.lua | 46+++++++++++++++++++++-------------------------
3 files changed, 33 insertions(+), 34 deletions(-)

diff --git a/lua/ansi_codes.lua b/lua/ansi_codes.lua @@ -1,7 +1,7 @@ local M = {} local rgb_to_ansi = {} --- https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters + local sgr_params = { reset = 0, clear = 0, @@ -76,10 +76,12 @@ local function ansi_string_24b(r,g,b) return string.char(27) .."[38;2;" .. tostring(r) .. ';' .. tostring(g).. ';' .. tostring(b).. 'm' end +M.begin_line_hl_ansi = string.char(27) .. "[47m" .. string.char(27) .. "[30m" +M.end_line_hl_ansi = string.char(27) .. "[K" .. string.char(27) .. "[0m" + M.ansi_string = ansi_string M.rgb_to_ansi = rgb_to_ansi -M.sgr_params = sgr_params -M.reset_sequence = ansi_string(sgr_params.reset) +M.reset_sequence = ansi_string(0) M.hex_to_rgb = hex_to_rgb M.ansi_string_24b = ansi_string_24b M.ansi_string_4b = ansi_string_4b diff --git a/lua/clp.lua b/lua/clp.lua @@ -58,7 +58,7 @@ function write(args) end function write_nohl(text, lexer, theme) - write_text(text,lexer,theme) + write_styled(text,lexer,theme) end function reset_colors() @@ -95,14 +95,15 @@ function write_hl(text, lexer, hl_line_start, hl_line_end, lang_theme) local hl = text:sub(hl_line_start, hl_line_end) hl = hl:gsub("\n", "") local post_hl = text:sub(hl_line_end, nil) - write_text(pre_hl, lexer, lang_theme) - if (hl ~= nil) then write_text(hl, lexer, line_highlight_style) end - reset_colors() - if (post_hl ~= nil) then write_text(post_hl, lexer, lang_theme) end + write_styled(pre_hl, lexer, lang_theme) + if (hl ~= nil) then + io.write(ansi_codes.begin_line_hl_ansi .. hl .. ansi_codes.end_line_hl_ansi) + end + if (post_hl ~= nil) then write_styled(post_hl, lexer, lang_theme) end end -- https://github.com/martanne/vis/issues/601#issuecomment-327018674 -function write_text(text, lexer, local_style) +function write_styled(text, lexer, local_style) local tokens = lexer:lex(text, 1) local token_start = 1 local last = '' diff --git a/lua/style.lua b/lua/style.lua @@ -7,10 +7,6 @@ local selected_theme_path = 'themes/' .. selected_theme_name local theme = require(selected_theme_path).theme local theme_escape_codes = {} -for sgr_name,sgr_number in pairs(ansi_codes.sgr_params) do - ansi_colors[sgr_name] = ansi_codes.ansi_string(sgr_number) -end - for token, color in pairs(theme) do if type(color) == 'table' then theme_escape_codes[token] = ansi_codes.ansi_string_4b(color) @@ -21,27 +17,27 @@ for token, color in pairs(theme) do end local line_highlight_style = { - ['default'] = ansi_colors.black .. ansi_colors.onwhite, - ['nothing'] = '' .. ansi_colors.onwhite, - ['class'] = ansi_colors.black .. ansi_colors.onwhite, - ['comment'] = ansi_colors.black .. ansi_colors.onwhite, - ['constant'] = ansi_colors.black .. ansi_colors.onwhite, - ['definition'] = ansi_colors.black .. ansi_colors.onwhite, - ['error'] = ansi_colors.black .. ansi_colors.onwhite, - ['function'] = ansi_colors.black .. ansi_colors.onwhite, - ['keyword'] = ansi_colors.black .. ansi_colors.onwhite, - ['label'] = ansi_colors.black .. ansi_colors.onwhite, - ['number'] = ansi_colors.black .. ansi_colors.onwhite, - ['operator'] = ansi_colors.black .. ansi_colors.onwhite, - ['regex'] = ansi_colors.black .. ansi_colors.onwhite, - ['string'] = ansi_colors.black .. ansi_colors.onwhite, - ['preprocessor'] = ansi_colors.black .. ansi_colors.onwhite, - ['tag'] = ansi_colors.black .. ansi_colors.onwhite, - ['type'] = ansi_colors.black .. ansi_colors.onwhite, - ['variable'] = ansi_colors.black .. ansi_colors.onwhite, - ['whitespace'] = '' .. ansi_colors.onwhite, - ['embedded'] = ansi_colors.black .. ansi_colors.onwhite, - ['identifier'] = ansi_colors.black .. ansi_colors.onwhite, + ['default'] = ansi_colors.black, + ['nothing'] = '', + ['class'] = ansi_colors.black, + ['comment'] = ansi_colors.black, + ['constant'] = ansi_colors.black, + ['definition'] = ansi_colors.black, + ['error'] = ansi_colors.black, + ['function'] = ansi_colors.black, + ['keyword'] = ansi_colors.black, + ['label'] = ansi_colors.black, + ['number'] = ansi_colors.black, + ['operator'] = ansi_colors.black, + ['regex'] = ansi_colors.black, + ['string'] = ansi_colors.black, + ['preprocessor'] = ansi_colors.black, + ['tag'] = ansi_colors.black, + ['type'] = ansi_colors.black, + ['variable'] = ansi_colors.black, + ['whitespace'] = '', + ['embedded'] = ansi_colors.black, + ['identifier'] = ansi_colors.black, } style.line_highlight_style = line_highlight_style