-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor-scroll.lua
77 lines (66 loc) · 1.86 KB
/
color-scroll.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
local LastColorLocation = os.getenv("HOME") .. "/.config/nvim/lastcolorscheme.txt"
local FallbackDefaultColor = "retrobox"
local DefaultColor = nil
local ColorIdx = nil
local ColorString = vim.trim(vim.fn.execute(":echo globpath(&rtp, 'colors/*')"))
local LuaMatch = string.gmatch(ColorString, "([^/]+)%.lua%c")
local VimMatch = string.gmatch(ColorString, "([^/]+)%.vim%c")
local AllColors = {}
for mat in LuaMatch do
table.insert(AllColors, mat)
end
for mat in VimMatch do
table.insert(AllColors, mat)
end
local file, err = io.open(LastColorLocation)
if file then
DefaultColor = file:read()
file:close()
else
print("error opening " .. tostring(LastColorLocation), err)
print("setting color to " .. FallbackDefaultColor)
DefaultColor = FallbackDefaultColor
end
for idx, item in ipairs(AllColors) do
if item == DefaultColor then
ColorIdx = idx
break
end
end
local function SaveColor()
local CurrentColor = vim.g.colors_name
print("saving color " .. CurrentColor)
os.execute("echo " .. CurrentColor .. " > " .. LastColorLocation)
end
local function SetColorByIdx(idx)
local nextColor = AllColors[idx]
vim.cmd("source $VIMRUNTIME/colors/vim.lua")
vim.cmd.colorscheme(nextColor)
print("set color to " .. nextColor)
end
local function ColorForward()
ColorIdx = ColorIdx + 1
if ColorIdx > #AllColors then
ColorIdx = 1
end
SetColorByIdx(ColorIdx)
end
local function ColorBack()
ColorIdx = ColorIdx - 1
if ColorIdx == 0 then
ColorIdx = #AllColors
end
SetColorByIdx(ColorIdx)
end
local function ToggleBackground()
if vim.o.background == "light" then
vim.o.background = "dark"
else
vim.o.background = "light"
end
end
vim.keymap.set('n', "<leader>K", ToggleBackground)
vim.keymap.set('n', "<leader>B", ColorForward)
vim.keymap.set('n', "<leader>C", ColorBack)
vim.api.nvim_create_autocmd({ "VimLeavePre" }, { callback = SaveColor })
vim.cmd.colorscheme(DefaultColor)