|
function string:split(pat) |
|
pat = pat or '%s+' |
|
local st, g = 1, self:gmatch('()(' .. pat .. ')') |
|
local function getter(segs, seps, sep, cap1, ...) |
|
st = sep and seps + #sep |
|
return self:sub(segs, (seps or 0) - 1), cap1 or sep, ... |
|
end |
|
return function() |
|
if st then |
|
return getter(st, g()) |
|
end |
|
end |
|
end |
|
|
|
local function Text(text) |
|
return { Text = text } |
|
end |
|
|
|
local function make_single_table(name, value) |
|
local ret = {} |
|
ret[name] = value |
|
return ret |
|
end |
|
|
|
local function Attribute(name, value) |
|
return make_single_table('Attribute', make_single_table(name, value)) |
|
end |
|
|
|
local function Underline(type) |
|
return Attribute('Underline', type) |
|
end |
|
|
|
local function Intensity(level) |
|
return Attribute('Intensity', level) |
|
end |
|
|
|
local function Italic(on) |
|
return Attribute('Italic', on) |
|
end |
|
|
|
local function ForegroundColour(colour) |
|
return make_single_table('Foreground', make_single_table('Color', colour)) |
|
end |
|
|
|
local function BackgroundColour(colour) |
|
return make_single_table('Background', make_single_table('Color', colour)) |
|
end |
|
|
|
local function NerdFont(text) |
|
local wezterm = require 'wezterm' |
|
local ok, nf = pcall(function() |
|
return wezterm.nerdfonts[text] |
|
end) |
|
if ok then |
|
return Text(nf) |
|
else |
|
wezterm.log_info(wezterm.to_string(nf)) |
|
end |
|
|
|
return nil |
|
end |
|
|
|
local function MakeFormat(f) |
|
f = f:sub(3, -2) |
|
local ret = {} |
|
for ff in f:split ',' do |
|
if ff == 'none' then |
|
table.insert(ret, 'ResetAttributes') |
|
elseif ff:match '^fg=' then |
|
table.insert(ret, ForegroundColour(ff:sub(4))) |
|
elseif ff:match '^bg=' then |
|
table.insert(ret, BackgroundColour(ff:sub(4))) |
|
elseif ff:match '^nf=' then |
|
table.insert(ret, NerdFont(ff:sub(4))) |
|
elseif ff == 'bright' or ff == 'bold' then |
|
table.insert(ret, Intensity 'Bold') |
|
elseif ff == 'dim' then |
|
table.insert(ret, Intensity 'Half') |
|
elseif ff == 'italics' then |
|
table.insert(ret, Italic(true)) |
|
elseif ff == 'underscore' then |
|
table.insert(ret, Underline 'Single') |
|
elseif ff == 'double-underscore' then |
|
table.insert(ret, Underline 'Double') |
|
elseif ff == 'curly-underscore' then |
|
table.insert(ret, Underline 'Curly') |
|
elseif ff == 'dotted-underscore' then |
|
table.insert(ret, Underline 'Dotted') |
|
elseif ff == 'dashed-underscore' then |
|
table.insert(ret, Underline 'Dashed') |
|
end |
|
end |
|
return ret |
|
end |
|
|
|
M = {} |
|
|
|
function M.status_to_wezterm_format(text) |
|
local rx = '(#%b[])' |
|
|
|
local tokens = {} |
|
|
|
-- Extract all formatting notations into tokens table as plain text |
|
local pass1 = string.gsub(text, rx, function(f) |
|
table.insert(tokens, f) |
|
return '`' |
|
end) |
|
|
|
-- Capture all text, which remains in pass1 result, as Text objects |
|
local i = 1 |
|
for ticks, txt in string.gmatch(pass1, '(`*)([^`]+)') do |
|
i = i + ticks:len() |
|
if txt:len() > 0 then |
|
table.insert(tokens, i, Text(txt)) |
|
i = i + 1 |
|
end |
|
end |
|
|
|
-- Finalise wezterm.format input |
|
local ret = {} |
|
for _, v in pairs(tokens) do |
|
if type(v) == 'table' then |
|
table.insert(ret, v) |
|
goto continue |
|
end |
|
|
|
local formats = MakeFormat(v) |
|
for _, f in ipairs(formats) do |
|
table.insert(ret, f) |
|
end |
|
|
|
::continue:: |
|
end |
|
|
|
return ret |
|
end |
|
|
|
return M |
I have added nerdfonts support.