Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(platforms): add generation for platform files #161

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions lua/nordic/platforms/foot.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
local U = require('nordic.utils')

local M = {}

--- @param colors ColorScheme
function M.generate(colors)
local footColors = U.removeHash(colors)

local foot = U.template(
[[
[colors]
foreground=${white1}
background=${bg}

regular0=${gray2}
regular1=${red.base}
regular2=${green.base}
regular3=${yellow.base}
regular4=${blue1}
regular5=${magenta.base}
regular6=${cyan.base}
regular7=${white1}
bright0=${gray3}
bright1=${red.bright}
bright2=${green.bright}
bright3=${yellow.bright}
bright4=${blue2}
bright5=${magenta.bright}
bright6=${cyan.bright}
bright7=${white2}
]],
footColors
)

return foot
end

return M
38 changes: 38 additions & 0 deletions lua/nordic/platforms/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-- Adapted from [tokyonight](https://github.com/folke/tokyonight.nvim)
--
-- Tokyonight is licensed under [Apache License 2.0](https://raw.githubusercontent.com/folke/tokyonight.nvim/refs/heads/main/LICENSE)
--
-- Changes:
-- - Adjusted the format of M.platforms
-- - Changed variable names (mostly from extra -> platform)
-- - Simplified code to work better for just one color scheme "flavor"
-- - Move `write` function to `utils.lua`
local U = require('nordic.utils')

local M = {}

--- @type table<string, {ext?: string, url:string, subdir?: string}>
-- stylua: ignore
M.platforms = {
foot = { ext = "ini", url = "https://codeberg.org/dnkl/foot" },
}

function M.setup()
local C = require('nordic.colors')

---@type string[]
local platform_names = vim.tbl_keys(M.platforms)
table.sort(platform_names)

for _, platform in ipairs(platform_names) do
local info = M.platforms[platform]
local plugin = require('nordic.platforms.' .. platform)
local fname = platform .. (info.subdir and '/' .. info.subdir .. '/' or '') .. '/nordic' .. '.' .. info.ext
fname = string.gsub(fname, '%.$', '') -- remove trailing dot when no extension

print('[write] ' .. fname)
U.write('platforms/' .. fname, plugin.generate(C))
end
end

return M
9 changes: 9 additions & 0 deletions lua/nordic/tests/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,12 @@ assert_eq(
assert_eq({ U.hex_to_rgb('#191D24') }, { 25, 29, 36 }, 'U.hex_to_rgb("#191D24") should return 25, 29, 36')
assert_eq(U.rgb_to_hex(25, 29, 36), '#191D24', 'U.rgb_to_hex(25, 29, 36) should return "#191D24"')
assert_eq(U.blend('#FFFFFF', '#000000', 0.5), '#808080', 'U.blend("#FFFFFF", ""#000000", 0.5) should return "#808080"')

assert_eq(
U.removeHash({ red = '#ff0000', green = '#00ff00', blue = { base = '#0000ff', other = '0000fb' } }),
{ red = 'ff0000', green = '00ff00', blue = { base = '0000ff', other = '0000fb' } }
)

-- Template.

assert_eq(U.template('${name} is ${age}', { name = 'Bob', age = 23 }), 'Bob is 23')
45 changes: 45 additions & 0 deletions lua/nordic/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,49 @@ function M.assert_eq(left, right, message)
end
end

---Simple string interpolation.
---
---Example template: "${name} is ${age}"
---
--- This function is taken from tokyonight.
--- For more information see `platforms/init.lua`
---@param str string template string
---@param table table key value pairs to replace in the string
function M.template(str, table)
return (
str:gsub('($%b{})', function(w)
-- TODO: unpack is depreciated
return vim.tbl_get(table, unpack(vim.split(w:sub(3, -2), '.', { plain = true }))) or w
end)
)
end

-- Remove the hash (#) from the beginning of all color values in a table
---@param colors table
function M.removeHash(colors)
local output_colors = {}
for k, v in pairs(colors) do
if type(v) == 'string' then
output_colors[k] = v:gsub('^#', '')
elseif type(v) == 'table' then
output_colors[k] = M.removeHash(v)
end
end

return output_colors
end

---Write a file and its contents to disk
---
--- This function is taken from tokyonight.
--- For more information see `platforms/init.lua`
---@param path string
---@param contents string
function M.write(path, contents)
vim.fn.mkdir(vim.fn.fnamemodify(path, ':h'), 'p')
local fd = assert(io.open(path, 'w+'))
fd:write(contents)
fd:close()
end

return M
File renamed without changes.
23 changes: 23 additions & 0 deletions platforms-old/foot/nordic.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Nordic Colorscheme for foot.
# Based on https://github.com/AlexvZyl/nordic.nvim
# Author: @filahf

[colors]
foreground=D8DEE9
background=242933
regular0=3B4252
regular1=BF616A
regular2=A3BE8C
regular3=EBCB8B
regular4=81A1C1
regular5=B48EAD
regular6=8FBCBB
regular7=D8DEE9
bright0=3B4252
bright1=D06F79
bright2=B1D196
bright3=F0D399
bright4=88C0D0
bright5=C895BF
bright6=93CCDC
bright7=E5E9F0
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions platforms.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env sh
5-pebbles marked this conversation as resolved.
Show resolved Hide resolved

nvim --headless -u NONE -c "lua package.path = package.path .. ';./lua/?/init.lua;./lua/?.lua'; require('nordic.platforms').setup() ; vim.api.nvim_command('quit')"
17 changes: 7 additions & 10 deletions platforms/foot/nordic.ini
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# Nordic Colorscheme for foot.
# Based on https://github.com/AlexvZyl/nordic.nvim
# Author: @filahf

[colors]
foreground=D8DEE9
background=242933

regular0=3B4252
regular1=BF616A
regular2=A3BE8C
Expand All @@ -13,11 +10,11 @@ regular4=81A1C1
regular5=B48EAD
regular6=8FBCBB
regular7=D8DEE9
bright0=3B4252
bright1=D06F79
bright2=B1D196
bright3=F0D399
bright0=434C5E
bright1=C5727A
bright2=B1C89D
bright3=EFD49F
bright4=88C0D0
bright5=C895BF
bright6=93CCDC
bright5=BE9DB8
bright6=9FC6C5
bright7=E5E9F0
Loading