clp

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

commit df3e3afd589b2593e0100c5d67c2e29a500498f8
parent 582112b160f4893e45986f26b152679bf67b2208
Author: Jon Eskin <eskinjp@gmail.com>
Date:   Tue, 19 Jul 2022 04:04:30 -0400

refactoring

Diffstat:
MREADME.md | 13+++++++++----
Mlua/clp.lua | 58++++++++++++++++++++--------------------------------------
Mlua/colors.lua | 6------
3 files changed, 29 insertions(+), 48 deletions(-)

diff --git a/README.md b/README.md @@ -28,8 +28,13 @@ $ make $ clp [options] filename ``` -clp will determine how to highlight the file by its extension, but you can -override this behavior with the `-t` flag. +#### -t, --override-filetype {filetype} + +Force a language's syntax for highlighting the file. To see available filetypes, run clp --list-overrides + +#### -h, --highlight-line {number} + +Highlight a non-blank line ## Motivation @@ -57,6 +62,6 @@ or a patch on [Sourcehut](https://git.sr.ht/~eskin/clp). Here are some things that would be especially helpful: -- Adding functionality to highlight lines (without tanking performance!) - Better interface for customizing color themes -- Bugfixes/code quality improvements +- Bugfixes +- Code quality & performance improvements diff --git a/lua/clp.lua b/lua/clp.lua @@ -13,6 +13,7 @@ function write(args) local file = assert(io.open(filename, 'r')) local text = file:read('*all') local syntax + if filetype_override ~= nil then syntax = filetype_override else @@ -23,9 +24,12 @@ function write(args) print(string.format('Failed to load lexer: `%s`', syntax)) return 1 end + + -- if a line was set to be highlighted, make sure it has valid bounds and + -- then highlight it. Otherwise just write normally to stdout. local hl_line_start, hl_line_end - if (highlight_line ~= nil) then - hl_start_pos, hl_end_pos = find_endl(text, highlight_line) + if (highlight_line ~= nil and highlight_line > 0) then + hl_start_pos, hl_end_pos = find_hl_bounds(text, highlight_line) end if (hl_start_pos ~= nil) then write_hl(text, lexer, hl_start_pos, hl_end_pos) @@ -35,35 +39,17 @@ function write(args) file:close() end --- https://github.com/martanne/vis/issues/601#issuecomment-327018674 function write_nohl(text, lexer) - local tokens = lexer:lex(text, 1) - local token_start = 1 - local last = '' - - for i = 1, #tokens, 2 do - local token_end = tokens[i + 1] - 1 - local name = tokens[i] + write_text(text,lexer,default_theme) +end - local style = default_theme[name] - if style ~= nil then - -- Whereas the lexer reports all other syntaxes over - -- the entire span of a token, it reports 'default' - -- byte-by-byte. We emit only the first 'default' of - -- a series in order to properly display multibyte - -- UTF-8 characters. - if not (last == 'default' and name == 'default') then - io.write(tostring(style)) - end - last = name - end - io.write(text:sub(token_start, token_end)) - token_start = token_end + 1 - end +function reset_colors() + io.write(tostring(colors.reset)) end --- find nth and n+1th indices of the endline character in a string -function find_endl(s, n) +-- I think modifying the lexer code to track highlighting location could be +-- more efficient, but this is a quick and dirty approach for now +function find_hl_bounds(s, n) local i = 0 local hl_start_pos if n == 1 then @@ -90,22 +76,18 @@ function find_endl(s, n) end function write_hl(text, lexer, hl_line_start, hl_line_end) - -- gsub the text before, during... can we just say "rest"? - -- then lets print as normal and see if there's a performance hit - -- check if hl_line_end is nil - -- substring from 0 to hl_line_start local pre_hl = text:sub(0, hl_line_start) local hl = text:sub(hl_line_start, hl_line_end) - -- substitute newlines for blanks in hl hl = hl:gsub("\n", "") local post_hl = text:sub(hl_line_end, nil) - write_thing(pre_hl, lexer, default_theme) - if (hl ~= nil) then write_thing(hl, lexer, highlight_theme) end - colors.reset_colors() - if (post_hl ~= nil) then write_thing(post_hl, lexer, default_theme) end + write_text(pre_hl, lexer, default_theme) + if (hl ~= nil) then write_text(hl, lexer, highlight_theme) end + reset_colors() + if (post_hl ~= nil) then write_text(post_hl, lexer, default_theme) end end -function write_thing(text, lexer, style) +-- https://github.com/martanne/vis/issues/601#issuecomment-327018674 +function write_text(text, lexer, style) local tokens = lexer:lex(text, 1) local token_start = 1 local last = '' @@ -132,7 +114,7 @@ function write_thing(text, lexer, style) end function print_available_overrides() - print('Available overrides:') + print('Available filetype overrides:') for lang, _ in pairs(ftdetect.filetypes) do print('- ' .. lang) end diff --git a/lua/colors.lua b/lua/colors.lua @@ -39,10 +39,6 @@ function colormt:__call(s) return self .. s .. colors.reset end -function reset_colors() - io.write(tostring(colors.reset)) -end - local function makecolor(value) return setmetatable({ value = schar(27) .. '[' .. tostring(value) .. 'm' }, colormt) end @@ -84,6 +80,4 @@ for c, v in pairs(colorvalues) do colors[c] = makecolor(v) end -colors.reset_colors = reset_colors - return colors