Skip to content

Commit

Permalink
add light theme and configure with clprc.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
jpe90 committed Aug 6, 2022
1 parent ffef516 commit a6a5d50
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 192 deletions.
29 changes: 0 additions & 29 deletions THIRD-PARTY-NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ please create an issue or contact me directly at [email protected].
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
=========================================
Expand Down Expand Up @@ -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 <[email protected]>

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

22 changes: 7 additions & 15 deletions lua/clp.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lua/clprc.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
clprc = {}
clprc.custom_theme = "dark"
return clprc
83 changes: 0 additions & 83 deletions lua/colors.lua

This file was deleted.

138 changes: 138 additions & 0 deletions lua/style.lua
Original file line number Diff line number Diff line change
@@ -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
64 changes: 0 additions & 64 deletions lua/theme.lua

This file was deleted.

7 changes: 7 additions & 0 deletions lua/util.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function copy_table(t)
local t2 = {}
for k, v in pairs(t) do
t2[k] = v
end
return t2
end
Loading

0 comments on commit a6a5d50

Please sign in to comment.