clp

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

commit a6a5d501b4ecdcb91303afd8b359e5ea7ac516a5
parent ffef5160952c1bc69f8630ee02b1725ab44a067f
Author: Jon Eskin <eskinjp@gmail.com>
Date:   Sat,  6 Aug 2022 09:37:21 -0400

add light theme and configure with clprc.lua

Diffstat:
MTHIRD-PARTY-NOTICE | 29-----------------------------
Mlua/clp.lua | 22+++++++---------------
Alua/clprc.lua | 3+++
Dlua/colors.lua | 83-------------------------------------------------------------------------------
Alua/style.lua | 138+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dlua/theme.lua | 64----------------------------------------------------------------
Alua/util.lua | 7+++++++
Mman/clp.1 | 23++++++++++++++++++++++-
8 files changed, 177 insertions(+), 192 deletions(-)

diff --git a/THIRD-PARTY-NOTICE b/THIRD-PARTY-NOTICE @@ -8,8 +8,6 @@ please create an issue or contact me directly at eskinjp@gmail.com. 2. musl (https://git.musl-libc.org/cgit/musl) 3. Scintillua (https://github.com/orbitalquark/scintillua) 4. optparse (https://github.com/skeeto/optparse) -5. lua-term (https://github.com/hoelzro/lua-term) - %% vis NOTICES AND INFORMATION BEGIN HERE ========================================= @@ -318,30 +316,3 @@ For more information, please refer to <http://unlicense.org/> ========================================= END OF optparse NOTICES AND INFORMATION - -%% lua-term NOTICES AND INFORMATION BEGIN HERE -========================================= - -Copyright (c) 2009 Rob Hoelz <rob@hoelzro.net> - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - -========================================= -END OF lua-term NOTICES AND INFORMATION - diff --git a/lua/clp.lua b/lua/clp.lua @@ -1,19 +1,11 @@ clp = {} -local theme = require('theme') -local colors = require('colors') +local colors = require('style') local ftdetect = require('ftdetect') local lexers = require('lexer') -local default_theme = theme.default_theme -local highlight_theme = theme.highlight_theme - -local function copy_table(t) - local t2 = {} - for k, v in pairs(t) do - t2[k] = v - end - return t2 -end +local syntax_highlight_style = colors.syntax_highlight_style +local highlighted_line_style = colors.line_highlight_style +require('util') function expand_theme(theme, lexer) local extra_styles = lexer._EXTRASTYLES @@ -40,7 +32,7 @@ function write(args) syntax = ftdetect.lookup_lexer(filename) end local lexer = lexers.load(syntax) - local lang_theme = expand_theme(default_theme, lexer) + local lang_theme = expand_theme(syntax_highlight_style, lexer) if not lexer then print(string.format('Failed to load lexer: `%s`', syntax)) return 1 @@ -66,7 +58,7 @@ function write_nohl(text, lexer, theme) end function reset_colors() - io.write(tostring(colors.reset)) + io.write(tostring(colors.reset_sequence)) end -- I think modifying the lexer code to track highlighting location could be @@ -103,7 +95,7 @@ function write_hl(text, lexer, hl_line_start, hl_line_end, lang_theme) 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, highlight_theme) end + if (hl ~= nil) then write_text(hl, lexer, highlighted_line_style) end reset_colors() if (post_hl ~= nil) then write_text(post_hl, lexer, lang_theme) end end diff --git a/lua/clprc.lua b/lua/clprc.lua @@ -0,0 +1,3 @@ +clprc = {} +clprc.custom_theme = "dark" +return clprc diff --git a/lua/colors.lua b/lua/colors.lua @@ -1,83 +0,0 @@ --- Copyright (c) 2009 Rob Hoelz <rob@hoelzro.net> --- --- Permission is hereby granted, free of charge, to any person obtaining a copy --- of this software and associated documentation files (the "Software"), to deal --- in the Software without restriction, including without limitation the rights --- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell --- copies of the Software, and to permit persons to whom the Software is --- furnished to do so, subject to the following conditions: --- --- The above copyright notice and this permission notice shall be included in --- all copies or substantial portions of the Software. --- --- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR --- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, --- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE --- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER --- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, --- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN --- THE SOFTWARE. - -local pairs = pairs -local tostring = tostring -local setmetatable = setmetatable -local schar = string.char - -local colors = {} - -local colormt = {} - -function colormt:__tostring() - return self.value -end - -function colormt:__concat(other) - return tostring(self) .. tostring(other) -end - -function colormt:__call(s) - return self .. s .. colors.reset -end - -local function makecolor(value) - return setmetatable({ value = schar(27) .. '[' .. tostring(value) .. 'm' }, colormt) -end - -local colorvalues = { - -- attributes - reset = 0, - clear = 0, - default = 0, - bright = 1, - dim = 2, - underscore = 4, - blink = 5, - reverse = 7, - hidden = 8, - - -- foreground - black = 30, - red = 31, - green = 32, - yellow = 33, - blue = 34, - magenta = 35, - cyan = 36, - white = 37, - - -- background - onblack = 40, - onred = 41, - ongreen = 42, - onyellow = 43, - onblue = 44, - onmagenta = 45, - oncyan = 46, - onwhite = 47, -} - -for c, v in pairs(colorvalues) do - colors[c] = makecolor(v) -end - -return colors diff --git a/lua/style.lua b/lua/style.lua @@ -0,0 +1,138 @@ +local style = {} +local ansi_colors = {} +local clprc = require('clprc') + +-- https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters +local sgr_params = { + reset = 0, + clear = 0, + default = 0, + bright = 1, + dim = 2, + underscore = 4, + blink = 5, + reverse = 7, + hidden = 8, + + -- TODO: get foreground values from external theme + -- e.g. black = theme.black_sgr_number + + -- foreground + black = 30, + red = 31, + green = 32, + yellow = 33, + blue = 34, + magenta = 35, + cyan = 36, + white = 37, + + -- background + onblack = 40, + onred = 41, + ongreen = 42, + onyellow = 43, + onblue = 44, + onmagenta = 45, + oncyan = 46, + onwhite = 47, +} + +-- converts a SGR parameter to an ANSI escape string +-- https://en.wikipedia.org/wiki/ANSI_escape_code#Colors +local function ansi_string(sgr_number) + if(sgr_number > 47) then + sgr_number = "38;5;" .. tostring(sgr_number) + end + return string.char(27) .. '[' .. tostring(sgr_number) .. 'm' +end + +for sgr_name,sgr_number in pairs(sgr_params) do + ansi_colors[sgr_name] = ansi_string(sgr_number) +end + +-- TODO: get these values from external theme +-- e.g. ['default'] = ansi_colors.default_color +local dark_mode_syntax_highlight_style = { + ['default'] = ansi_colors.white, + ['nothing'] = '', + ['class'] = ansi_colors.yellow .. ansi_colors.bright, + ['comment'] = ansi_colors.blue .. ansi_colors.bright, + ['constant'] = ansi_colors.cyan .. ansi_colors.bright, + ['definition'] = ansi_colors.blue .. ansi_colors.bright, + ['error'] = ansi_colors.red .. ansi_colors.underscore, + ['function'] = ansi_colors.blue .. ansi_colors.bright, + ['keyword'] = ansi_colors.yellow .. ansi_colors.bright, + ['label'] = ansi_colors.green .. ansi_colors.bright, + ['number'] = ansi_colors.red .. ansi_colors.bright, + ['operator'] = ansi_colors.cyan .. ansi_colors.bright, + ['regex'] = ansi_colors.green .. ansi_colors.bright, + ['string'] = ansi_colors.red .. ansi_colors.bright, + ['preprocessor'] = ansi_colors.magenta .. ansi_colors.bright, + ['tag'] = ansi_colors.red .. ansi_colors.bright, + ['type'] = ansi_colors.green .. ansi_colors.bright, + ['variable'] = ansi_colors.blue .. ansi_colors.bright, + ['whitespace'] = '', + ['embedded'] = ansi_colors.magenta .. ansi_colors.bright, + ['identifier'] = ansi_colors.white, +} + +local light_mode_syntax_highlight_style = { + ['default'] = ansi_colors.black, + ['nothing'] = '', + ['class'] = ansi_colors.magenta .. ansi_colors.bright, + ['comment'] = ansi_colors.blue .. ansi_colors.bright, + ['constant'] = ansi_colors.cyan .. ansi_colors.bright, + ['definition'] = ansi_colors.blue .. ansi_colors.bright, + ['error'] = ansi_colors.red .. ansi_colors.underscore, + ['function'] = ansi_colors.blue .. ansi_colors.bright, + ['keyword'] = ansi_colors.blue.. ansi_colors.bright, + ['label'] = ansi_colors.green .. ansi_colors.bright, + ['number'] = ansi_colors.red .. ansi_colors.bright, + ['operator'] = ansi_colors.black .. ansi_colors.bright, + ['regex'] = ansi_colors.green .. ansi_colors.bright, + ['string'] = ansi_colors.red .. ansi_colors.bright, + ['preprocessor'] = ansi_colors.magenta .. ansi_colors.bright, + ['tag'] = ansi_colors.red .. ansi_colors.bright, + ['type'] = ansi_colors.cyan .. ansi_colors.bright, + ['variable'] = ansi_colors.blue .. ansi_colors.bright, + ['whitespace'] = '', + ['embedded'] = ansi_colors.magenta .. ansi_colors.bright, + ['identifier'] = ansi_colors.black, +} + +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, +} + +style.reset_sequence = ansi_string(sgr_params.reset) +style.line_highlight_style = line_highlight_style + +-- TODO: temporary hack, remove when external themes implemented +if clprc ~= nil and clprc.custom_theme == "light" then + style.syntax_highlight_style = light_mode_syntax_highlight_style +else + style.syntax_highlight_style = dark_mode_syntax_highlight_style +end + +return style diff --git a/lua/theme.lua b/lua/theme.lua @@ -1,64 +0,0 @@ -local colors = require('colors') -local theme = {} - --- I'd like to revamp the architecture of coloring and add configurable themes --- The existing design is entirely placeholder and is expected to be overhauled - -local default_theme = { - -- bold => bright - -- italics => underscore - ['default'] = colors.white, - ['nothing'] = '', - ['class'] = colors.yellow .. colors.bright, - ['comment'] = colors.blue .. colors.bright, - ['constant'] = colors.cyan .. colors.bright, - ['definition'] = colors.blue .. colors.bright, - ['error'] = colors.red .. colors.underscore, - ['function'] = colors.blue .. colors.bright, - ['keyword'] = colors.yellow .. colors.bright, - ['label'] = colors.green .. colors.bright, - ['number'] = colors.red .. colors.bright, - ['operator'] = colors.cyan .. colors.bright, - ['regex'] = colors.green .. colors.bright, - ['string'] = colors.red .. colors.bright, - ['preprocessor'] = colors.magenta .. colors.bright, - ['tag'] = colors.red .. colors.bright, - ['type'] = colors.green .. colors.bright, - ['variable'] = colors.blue .. colors.bright, - ['whitespace'] = '', - ['embedded'] = colors.magenta .. colors.bright, - ['identifier'] = colors.white, -} - -local highlight_theme = { - -- bold => bright - -- italics => underscore - ['default'] = colors.black .. colors.onwhite, - ['nothing'] = '' .. colors.onwhite, - ['class'] = colors.black .. colors.onwhite, - ['comment'] = colors.black .. colors.onwhite, - ['constant'] = colors.black .. colors.onwhite, - ['definition'] = colors.black .. colors.onwhite, - ['error'] = colors.black .. colors.onwhite, - ['function'] = colors.black .. colors.onwhite, - ['keyword'] = colors.black .. colors.onwhite, - ['label'] = colors.black .. colors.onwhite, - ['number'] = colors.black .. colors.onwhite, - ['operator'] = colors.black .. colors.onwhite, - ['regex'] = colors.black .. colors.onwhite, - ['string'] = colors.black .. colors.onwhite, - ['preprocessor'] = colors.black .. colors.onwhite, - ['tag'] = colors.black .. colors.onwhite, - ['type'] = colors.black .. colors.onwhite, - ['variable'] = colors.black .. colors.onwhite, - ['whitespace'] = '' .. colors.onwhite, - ['embedded'] = colors.black .. colors.onwhite, - ['identifier'] = colors.black .. colors.onwhite, -} - - -theme['default_theme'] = default_theme -theme['highlight_theme'] = highlight_theme -theme['colors'] = colors - -return theme diff --git a/lua/util.lua b/lua/util.lua @@ -0,0 +1,7 @@ +function copy_table(t) + local t2 = {} + for k, v in pairs(t) do + t2[k] = v + end + return t2 +end diff --git a/man/clp.1 b/man/clp.1 @@ -17,8 +17,29 @@ See below. \fB\-t\fR, \fB\-\-override\-filetype\fR {filetype} .IP Force a language's syntax for highlighting the file. To see available filetypes, run \fBclp \-\-list\-overrides\fR\ - +. .HP \fB\-h\fR, \fB\-\-highlight\-line\fR {number} .IP Highlight a non-blank line +.SH CONFIGURATION +\fBclp\fR is configured by the \fBclprc.lua\fR file described in the \fBFILES\fR section. +.HP +To use a light color theme, set clprc.custom_theme = "light" +.SH FILES +Upon startup +.Nm +will source the first +\fBclprc.lua\fR +configuration file found from these locations. +.Bl -bullet +.IP \[bu] 2 +\fB$CLP_PATH\fR if set or \fB/usr/local/share/clp\fR or if not +.IP \[bu] 2 +The source directory where the project was built (when built from source) +.IP \[bu] 2 +\fB$XDG_CONFIG_HOME/clp\fR +where +\fBXDG_CONFIG_HOME\fR refers to \fB$HOME/.config\fR if\fBXDG_CONFIG_HOME\fR not set +.HP +When creating a new \fBclprc.lua\fR be sure to copy the structure from here.