theme-switcher.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/env lua
  2. -- Neovim 主题切换脚本
  3. -- 用法: nvim -l theme-switcher.lua <主题名> [样式]
  4. local themes = {
  5. dracula = {
  6. plugin = 'Mofiqul/dracula.nvim',
  7. setup = function(style)
  8. local opts = {
  9. theme = style or 'dracula', -- dracula, dracula-soft, day
  10. transparent_bg = false,
  11. italic_comment = true,
  12. show_end_of_buffer = true,
  13. }
  14. require('dracula').setup(opts)
  15. end
  16. },
  17. catppuccin = {
  18. plugin = 'catppuccin/nvim',
  19. setup = function(style)
  20. local flavours = { 'mocha', 'macchiato', 'frappe', 'latte' }
  21. local flavour = style and flavours[tonumber(style)] or 'mocha'
  22. require('catppuccin').setup({ flavour = flavour })
  23. end
  24. },
  25. tokyonight = {
  26. plugin = 'folke/tokyonight.nvim',
  27. setup = function(style)
  28. local opts = {
  29. style = style or 'night', -- night, storm, day, moon
  30. transparent = false,
  31. terminal_colors = true,
  32. }
  33. require('tokyonight').setup(opts)
  34. end
  35. },
  36. gruvbox = {
  37. plugin = 'ellisonleao/gruvbox.nvim',
  38. setup = function(style)
  39. local opts = {
  40. contrast = style or 'hard', -- soft, medium, hard
  41. transparent_mode = false,
  42. }
  43. require('gruvbox').setup(opts)
  44. end
  45. },
  46. onedark = {
  47. plugin = 'navarasu/onedark.nvim',
  48. setup = function(style)
  49. local opts = {
  50. style = style or 'dark', -- dark, darker, cool, deep, warm, warmer
  51. transparent = false,
  52. }
  53. require('onedark').setup(opts)
  54. end
  55. }
  56. }
  57. -- 解析命令行参数
  58. local theme_name = arg[1]
  59. local style = arg[2]
  60. if not theme_name then
  61. print("\n🎨 Neovim 主题切换器")
  62. print("用法: nvim -l theme-switcher.lua <主题> [样式]")
  63. print("\n可用主题:")
  64. for name, info in pairs(themes) do
  65. print(string.format(" %-12s %s", name, info.plugin))
  66. end
  67. print("\n示例:")
  68. print(" nvim -l theme-switcher.lua dracula dracula-soft")
  69. print(" nvim -l theme-switcher.lua catppuccin 1 (1=mocha,2=macchiato,3=frappe,4=latte)")
  70. print(" nvim -l theme-switcher.lua tokyonight storm")
  71. os.exit(0)
  72. end
  73. -- 检查主题是否存在
  74. if not themes[theme_name] then
  75. print("❌ 未知主题: " .. theme_name)
  76. print("可用主题: " .. table.concat(vim.tbl_keys(themes), ", "))
  77. os.exit(1)
  78. end
  79. -- 生成临时配置文件
  80. local config_content = string.format([[
  81. -- 临时主题配置 - %s
  82. vim.cmd [[set runtimepath+=~/.config/nvim]]
  83. -- 安装主题插件
  84. local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
  85. if not vim.loop.fs_stat(lazypath) then
  86. vim.fn.system({
  87. "git", "clone", "--filter=blob:none",
  88. "https://github.com/folke/lazy.nvim.git",
  89. "--branch=stable", lazypath,
  90. })
  91. end
  92. vim.opt.rtp:prepend(lazypath)
  93. require("lazy").setup({
  94. { "%s", priority = 1000 },
  95. })
  96. -- 设置主题
  97. %s
  98. vim.cmd.colorscheme("%s")
  99. -- 打开一个新文件查看效果
  100. vim.cmd [[enew]]
  101. vim.api.nvim_buf_set_lines(0, 0, -1, false, {
  102. "🎨 主题预览: %s",
  103. "",
  104. "function hello() {",
  105. " console.log('Hello, World!');",
  106. " // This is a comment",
  107. " const dracula = '🧛';",
  108. " return dracula;",
  109. "}",
  110. "",
  111. "local themes = {",
  112. " dracula = 'dark',",
  113. " catppuccin = 'soft',",
  114. "}",
  115. })
  116. -- 设置快捷键
  117. vim.keymap.set('n', 'q', ':qa!<CR>', { noremap = true, silent = true })
  118. vim.keymap.set('n', '<Esc>', ':qa!<CR>', { noremap = true, silent = true })
  119. print("\n🎨 主题预览: %s (%s)")
  120. print("按 q 或 Esc 退出预览")
  121. ]],
  122. theme_name,
  123. themes[theme_name].plugin,
  124. themes[theme_name].setup(style),
  125. theme_name,
  126. theme_name,
  127. theme_name,
  128. style or "default"
  129. )
  130. -- 写入临时配置
  131. local tmp_config = "/tmp/nvim-theme-preview.lua"
  132. local file = io.open(tmp_config, "w")
  133. if file then
  134. file:write(config_content)
  135. file:close()
  136. -- 启动 neovim 预览
  137. local cmd = string.format("nvim -u %s", tmp_config)
  138. print(string.format("\n🎨 启动 %s 主题预览 (%s)...", theme_name, style or "default"))
  139. os.execute(cmd)
  140. -- 清理临时文件
  141. os.remove(tmp_config)
  142. else
  143. print("❌ 无法创建临时配置文件")
  144. os.exit(1)
  145. end