galaxyline.lua 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. -- if not package.loaded['galaxyline'] then
  2. -- return
  3. -- end
  4. local Log = require "core.log"
  5. local status_ok, gl = pcall(require, "galaxyline")
  6. if not status_ok then
  7. Log:get_default().error "Failed to load galaxyline"
  8. return
  9. end
  10. -- NOTE: if someone defines colors but doesn't have them then this will break
  11. local palette_status_ok, colors = pcall(require, lvim.colorscheme .. ".palette")
  12. if not palette_status_ok then
  13. colors = lvim.builtin.galaxyline.colors
  14. end
  15. local condition = require "galaxyline.condition"
  16. local gls = gl.section
  17. gl.short_line_list = { "NvimTree", "vista", "dbui", "packer" }
  18. local function get_mode_name()
  19. local names = {
  20. n = "NORMAL",
  21. i = "INSERT",
  22. c = "COMMAND",
  23. v = "VISUAL",
  24. V = "VISUAL LINE",
  25. t = "TERMINAL",
  26. R = "REPLACE",
  27. [""] = "VISUAL BLOCK",
  28. }
  29. return names[vim.fn.mode()]
  30. end
  31. table.insert(gls.left, {
  32. ViMode = {
  33. provider = function()
  34. -- auto change color according the vim mode
  35. local mode_color = {
  36. n = colors.blue,
  37. i = colors.green,
  38. v = colors.purple,
  39. [""] = colors.purple,
  40. V = colors.purple,
  41. c = colors.magenta,
  42. no = colors.blue,
  43. s = colors.orange,
  44. S = colors.orange,
  45. [""] = colors.orange,
  46. ic = colors.yellow,
  47. R = colors.red,
  48. Rv = colors.red,
  49. cv = colors.blue,
  50. ce = colors.blue,
  51. r = colors.cyan,
  52. rm = colors.cyan,
  53. ["r?"] = colors.cyan,
  54. ["!"] = colors.blue,
  55. t = colors.blue,
  56. }
  57. if lvim.builtin.galaxyline.show_mode then
  58. local name = get_mode_name()
  59. -- Fall back to the default behavior is a name is not defined
  60. if name ~= nil then
  61. vim.api.nvim_command("hi GalaxyViMode guibg=" .. mode_color[vim.fn.mode()])
  62. vim.api.nvim_command("hi GalaxyViMode guifg=" .. colors.alt_bg)
  63. return " " .. name .. " "
  64. end
  65. end
  66. vim.api.nvim_command("hi GalaxyViMode guibg=" .. colors.alt_bg)
  67. vim.api.nvim_command("hi GalaxyViMode guifg=" .. mode_color[vim.fn.mode()])
  68. return "▊"
  69. end,
  70. separator_highlight = { "NONE", colors.alt_bg },
  71. highlight = { "NONE", colors.alt_bg },
  72. },
  73. })
  74. -- print(vim.fn.getbufvar(0, 'ts'))
  75. vim.fn.getbufvar(0, "ts")
  76. table.insert(gls.left, {
  77. GitIcon = {
  78. provider = function()
  79. return " "
  80. end,
  81. condition = condition.check_git_workspace,
  82. separator = " ",
  83. separator_highlight = { "NONE", colors.alt_bg },
  84. highlight = { colors.orange, colors.alt_bg },
  85. },
  86. })
  87. table.insert(gls.left, {
  88. GitBranch = {
  89. provider = "GitBranch",
  90. condition = condition.check_git_workspace,
  91. separator = " ",
  92. separator_highlight = { "NONE", colors.alt_bg },
  93. highlight = { colors.grey, colors.alt_bg },
  94. },
  95. })
  96. table.insert(gls.left, {
  97. DiffAdd = {
  98. provider = "DiffAdd",
  99. condition = condition.hide_in_width,
  100. icon = "  ",
  101. highlight = { colors.green, colors.alt_bg },
  102. },
  103. })
  104. table.insert(gls.left, {
  105. DiffModified = {
  106. provider = "DiffModified",
  107. condition = condition.hide_in_width,
  108. icon = " 柳",
  109. highlight = { colors.blue, colors.alt_bg },
  110. },
  111. })
  112. table.insert(gls.left, {
  113. DiffRemove = {
  114. provider = "DiffRemove",
  115. condition = condition.hide_in_width,
  116. icon = "  ",
  117. highlight = { colors.red, colors.alt_bg },
  118. },
  119. })
  120. table.insert(gls.left, {
  121. Filler = {
  122. provider = function()
  123. return " "
  124. end,
  125. highlight = { colors.grey, colors.alt_bg },
  126. },
  127. })
  128. -- get output from shell command
  129. function os.capture(cmd, raw)
  130. local f = assert(io.popen(cmd, "r"))
  131. local s = assert(f:read "*a")
  132. f:close()
  133. if raw then
  134. return s
  135. end
  136. s = string.gsub(s, "^%s+", "")
  137. s = string.gsub(s, "%s+$", "")
  138. s = string.gsub(s, "[\n\r]+", " ")
  139. return s
  140. end
  141. -- cleanup virtual env
  142. local function env_cleanup(venv)
  143. if string.find(venv, "/") then
  144. local final_venv = venv
  145. for w in venv:gmatch "([^/]+)" do
  146. final_venv = w
  147. end
  148. venv = final_venv
  149. end
  150. return venv
  151. end
  152. local PythonEnv = function()
  153. if vim.bo.filetype == "python" then
  154. local venv = os.getenv "CONDA_DEFAULT_ENV"
  155. if venv ~= nil then
  156. return "  (" .. env_cleanup(venv) .. ")"
  157. end
  158. venv = os.getenv "VIRTUAL_ENV"
  159. if venv ~= nil then
  160. return "  (" .. env_cleanup(venv) .. ")"
  161. end
  162. return ""
  163. end
  164. return ""
  165. end
  166. table.insert(gls.left, {
  167. VirtualEnv = {
  168. provider = PythonEnv,
  169. event = "BufEnter",
  170. highlight = { colors.green, colors.alt_bg },
  171. },
  172. })
  173. table.insert(gls.right, {
  174. DiagnosticError = {
  175. provider = "DiagnosticError",
  176. icon = "  ",
  177. highlight = { colors.red, colors.alt_bg },
  178. },
  179. })
  180. table.insert(gls.right, {
  181. DiagnosticWarn = {
  182. provider = "DiagnosticWarn",
  183. icon = "  ",
  184. highlight = { colors.orange, colors.alt_bg },
  185. },
  186. })
  187. table.insert(gls.right, {
  188. DiagnosticInfo = {
  189. provider = "DiagnosticInfo",
  190. icon = "  ",
  191. highlight = { colors.yellow, colors.alt_bg },
  192. },
  193. })
  194. table.insert(gls.right, {
  195. DiagnosticHint = {
  196. provider = "DiagnosticHint",
  197. icon = "  ",
  198. highlight = { colors.blue, colors.alt_bg },
  199. },
  200. })
  201. table.insert(gls.right, {
  202. TreesitterIcon = {
  203. provider = function()
  204. if next(vim.treesitter.highlighter.active) ~= nil then
  205. return " "
  206. end
  207. return ""
  208. end,
  209. separator = " ",
  210. separator_highlight = { "NONE", colors.alt_bg },
  211. highlight = { colors.green, colors.alt_bg },
  212. },
  213. })
  214. local function get_attached_provider_name(msg)
  215. msg = msg or "LSP Inactive"
  216. local buf_clients = vim.lsp.buf_get_clients()
  217. if next(buf_clients) == nil then
  218. return msg
  219. end
  220. local buf_client_names = {}
  221. for _, client in pairs(buf_clients) do
  222. if client.name ~= "null-ls" then
  223. table.insert(buf_client_names, client.name)
  224. end
  225. end
  226. local null_ls = require "lsp.null-ls"
  227. local null_ls_providers = null_ls.list_supported_provider_names(vim.bo.filetype)
  228. vim.list_extend(buf_client_names, null_ls_providers)
  229. return table.concat(buf_client_names, ", ")
  230. end
  231. table.insert(gls.right, {
  232. ShowLspClient = {
  233. provider = get_attached_provider_name,
  234. condition = function()
  235. local tbl = { ["dashboard"] = true, [" "] = true }
  236. if tbl[vim.bo.filetype] then
  237. return false
  238. end
  239. return true
  240. end,
  241. icon = " ",
  242. highlight = { colors.grey, colors.alt_bg },
  243. },
  244. })
  245. table.insert(gls.right, {
  246. LineInfo = {
  247. provider = "LineColumn",
  248. separator = " ",
  249. separator_highlight = { "NONE", colors.alt_bg },
  250. highlight = { colors.grey, colors.alt_bg },
  251. },
  252. })
  253. table.insert(gls.right, {
  254. PerCent = {
  255. provider = "LinePercent",
  256. separator = " ",
  257. separator_highlight = { "NONE", colors.alt_bg },
  258. highlight = { colors.grey, colors.alt_bg },
  259. },
  260. })
  261. table.insert(gls.right, {
  262. Tabstop = {
  263. provider = function()
  264. local label = "Spaces: "
  265. if not vim.api.nvim_buf_get_option(0, "expandtab") then
  266. label = "Tab size: "
  267. end
  268. return label .. vim.api.nvim_buf_get_option(0, "shiftwidth") .. " "
  269. end,
  270. condition = condition.hide_in_width,
  271. separator = " ",
  272. separator_highlight = { "NONE", colors.alt_bg },
  273. highlight = { colors.grey, colors.alt_bg },
  274. },
  275. })
  276. table.insert(gls.right, {
  277. BufferType = {
  278. provider = "FileTypeName",
  279. condition = condition.hide_in_width,
  280. separator = " ",
  281. separator_highlight = { "NONE", colors.alt_bg },
  282. highlight = { colors.grey, colors.alt_bg },
  283. },
  284. })
  285. table.insert(gls.right, {
  286. FileEncode = {
  287. provider = "FileEncode",
  288. condition = condition.hide_in_width,
  289. separator = " ",
  290. separator_highlight = { "NONE", colors.alt_bg },
  291. highlight = { colors.grey, colors.alt_bg },
  292. },
  293. })
  294. table.insert(gls.right, {
  295. Space = {
  296. provider = function()
  297. return " "
  298. end,
  299. separator = " ",
  300. separator_highlight = { "NONE", colors.alt_bg },
  301. highlight = { colors.grey, colors.alt_bg },
  302. },
  303. })
  304. table.insert(gls.short_line_left, {
  305. BufferType = {
  306. provider = "FileTypeName",
  307. separator = " ",
  308. separator_highlight = { "NONE", colors.alt_bg },
  309. highlight = { colors.alt_bg, colors.alt_bg },
  310. },
  311. })
  312. table.insert(gls.short_line_left, {
  313. SFileName = {
  314. provider = "SFileName",
  315. condition = condition.buffer_not_empty,
  316. highlight = { colors.alt_bg, colors.alt_bg },
  317. },
  318. })
  319. --table.insert(gls.short_line_right[1] = {BufferIcon = {provider = 'BufferIcon', highlight = {colors.grey, colors.alt_bg}}})