theme-previewer.lua 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env lua
  2. -- 主题预览器 - 简化版
  3. -- 用法: nvim -u theme-previewer.lua
  4. -- 临时配置目录
  5. local tmp_dir = "/tmp/nvim-theme-preview"
  6. os.execute("mkdir -p " .. tmp_dir)
  7. -- 写入基础配置
  8. local init_lua = tmp_dir .. "/init.lua"
  9. local file = io.open(init_lua, "w")
  10. if not file then
  11. print("无法创建临时配置")
  12. os.exit(1)
  13. end
  14. file:write([[
  15. -- 最小化主题预览配置
  16. vim.opt.termguicolors = true
  17. vim.opt.background = "dark"
  18. -- 主题列表
  19. local themes = {
  20. dracula = function()
  21. package.path = package.path .. ";" .. vim.fn.stdpath("config") .. "/lua/?.lua"
  22. require('plugins.dracula')
  23. end,
  24. onedark = function()
  25. vim.cmd [[colorscheme onedark]]
  26. end,
  27. tokyonight = function()
  28. require('tokyonight').setup({style = 'night'})
  29. vim.cmd.colorscheme('tokyonight')
  30. end,
  31. catppuccin = function()
  32. require('catppuccin').setup({flavour = 'mocha'})
  33. vim.cmd.colorscheme('catppuccin')
  34. end,
  35. gruvbox = function()
  36. require('gruvbox').setup({contrast = 'hard'})
  37. vim.cmd.colorscheme('gruvbox')
  38. end,
  39. }
  40. -- 获取主题参数
  41. local theme = arg[1] or 'dracula'
  42. -- 应用主题
  43. if themes[theme] then
  44. themes[theme]()
  45. else
  46. print("未知主题: " .. theme)
  47. print("可用主题: " .. table.concat(vim.tbl_keys(themes), ", "))
  48. end
  49. -- 预览内容
  50. vim.api.nvim_buf_set_lines(0, 0, -1, false, {
  51. "🎨 " .. theme .. " 主题预览",
  52. "",
  53. "-- Lua 代码示例",
  54. "local function hello_world()",
  55. " print('Hello, World!') -- 注释",
  56. " local dracula = '🧛'",
  57. " return dracula",
  58. "end",
  59. "",
  60. "-- JavaScript 代码示例",
  61. "function hello() {",
  62. " console.log('Hello, World!');",
  63. " const theme = '" .. theme .. "';",
  64. " return theme;",
  65. "}",
  66. "",
  67. "快捷键: q 退出预览",
  68. })
  69. -- 设置快捷键
  70. vim.keymap.set('n', 'q', ':qa!<CR>', {noremap = true, silent = true})
  71. vim.keymap.set('n', '<Esc>', ':qa!<CR>', {noremap = true, silent = true})
  72. print("🎨 预览 " .. theme .. " 主题")
  73. print("按 q 退出预览")
  74. ]])
  75. file:close()
  76. -- 启动 nvim
  77. local cmd = string.format("nvim -u %s", init_lua)
  78. os.execute(cmd)
  79. -- 清理
  80. os.execute("rm -rf " .. tmp_dir)