Skip to content

Commit

Permalink
red: dir colorize
Browse files Browse the repository at this point in the history
  • Loading branch information
hugeping committed Jun 23, 2024
1 parent 2e048e7 commit 9f269ca
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion data/apps/red.lua
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,12 @@ function frame:file(f)

b = win:new(fn)
b.menu = self:menu().buf:gettext() -- clone menu
b.conf = presets.get(fn) or {}
if dir then
b:set ""
b:readdir(fn)
b:cur(1)
elseif not fn:startswith '+' then
b.conf = presets.get(fn) or {}
b:file(fn)
if nr == 0 and b:histfile_get() then
self:push_win(b)
Expand Down
6 changes: 6 additions & 0 deletions data/lib/red/presets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,10 @@ return {
syntax = "diff"
}
},
{"/$", { ts = 8,
spaces_tab = false,
trim_spaces = false,
syntax = "dir"
}
},
}
53 changes: 53 additions & 0 deletions data/lib/red/syntax/dir.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
local scheme = require "red/syntax/scheme"

local function match(ext)
if type(ext) ~= 'table' then
ext = { ext }
end
return function(ctx, txt, i)
local d = 0
local n = {}
while txt[i] ~= '\n' and txt[i] do
table.insert(n, txt[i])
i = i + 1
end
local t = table.concat(n, '')
for _, v in ipairs(ext) do
if (t:lower()):find(v, 1) then
return #n
end
end
end
end

local col = {
col = scheme.default,
{
start = match("/$"),
stop = '\n',
col = scheme.number,
},
{
start = match { "%.pdf$", "%.docx?$", "%.rtf$" },
stop = '\n',
col = scheme.keyword,
},
{
start = match { "%.log$", "%.txt$", "%.md$" },
stop = '\n',
col = scheme.string,
},
{
start = match { "%.jpe?g$", "%.gif$", "%.tiff$", "%.png", "%.xmp",
"%.mp[34]$", "%.avi$", "%.mpeg$", "%.mkv$", "%.webm" },
stop = '\n',
col = scheme.operator,
},
{
start = match { "%.zip$", "%.gz$", "%.rar$", "%.arj", "%.tar$" },
stop = '\n',
col = scheme.lib,
},
}

return col
1 change: 1 addition & 0 deletions data/lib/red/win.lua
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ end
function win:set(text)
self.buf:set(text)
self:cur(self:cur())
self.colorizer = nil
end

function win:gettext(...)
Expand Down

0 comments on commit 9f269ca

Please sign in to comment.