From 15ac692c65ec072f7f113b8c2c4f3122f1e37a64 Mon Sep 17 00:00:00 2001 From: Ahmed Zamouche Date: Sun, 26 Nov 2023 21:25:54 +0100 Subject: [PATCH] Personal changes --- .gitignore | 3 + .gitmodules | 4 + init.lua | 2 + lua/config/lazy.lua | 6 +- lua/config/options.lua | 2 + lua/plugins/gist.lua | 7 + lua/plugins/goto-preview.lua | 29 ++++ lua/plugins/lualine.lua | 10 ++ lua/plugins/octo.lua | 14 ++ lua/plugins/supertab.lua | 54 ++++++ lua/plugins/symbols-outline.lua | 8 + lua/plugins/telescope-fzf-native.lua | 12 ++ lua/plugins/toggleterm.lua | 41 +++++ lua/plugins/window-picker.lua | 12 ++ lua/user/csearchpath.lua | 56 +++++++ lua/user/csearchpath.sh | 34 ++++ lua/user/docker.lua | 57 +++++++ lua/user/scratchpad.lua | 6 + lua/user/toggleterm.lua | 236 +++++++++++++++++++++++++++ lua/user/utils.lua | 55 +++++++ thirdparty/json | 1 + 21 files changed, 646 insertions(+), 3 deletions(-) create mode 100644 .gitmodules create mode 100644 lua/plugins/gist.lua create mode 100644 lua/plugins/goto-preview.lua create mode 100644 lua/plugins/lualine.lua create mode 100644 lua/plugins/octo.lua create mode 100644 lua/plugins/supertab.lua create mode 100644 lua/plugins/symbols-outline.lua create mode 100644 lua/plugins/telescope-fzf-native.lua create mode 100644 lua/plugins/toggleterm.lua create mode 100644 lua/plugins/window-picker.lua create mode 100644 lua/user/csearchpath.lua create mode 100644 lua/user/csearchpath.sh create mode 100644 lua/user/docker.lua create mode 100644 lua/user/scratchpad.lua create mode 100644 lua/user/toggleterm.lua create mode 100644 lua/user/utils.lua create mode 160000 thirdparty/json diff --git a/.gitignore b/.gitignore index cc5457a..583b50c 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ debug foo.* *.log data +.luarc.json +lazy-lock.json +lazyvim.json diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..ef59d56 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,4 @@ +[submodule "thirdparty/json"] + path = thirdparty/json + url = https://github.com/rxi/json.lua.git + branch = master diff --git a/init.lua b/init.lua index 2514f9e..df7e8eb 100644 --- a/init.lua +++ b/init.lua @@ -1,2 +1,4 @@ -- bootstrap lazy.nvim, LazyVim and your plugins require("config.lazy") +require("user.toggleterm") +require("user.csearchpath") diff --git a/lua/config/lazy.lua b/lua/config/lazy.lua index 891b190..881987a 100644 --- a/lua/config/lazy.lua +++ b/lua/config/lazy.lua @@ -11,9 +11,9 @@ require("lazy").setup({ -- add LazyVim and import its plugins { "LazyVim/LazyVim", import = "lazyvim.plugins" }, -- import any extras modules here - -- { import = "lazyvim.plugins.extras.lang.typescript" }, - -- { import = "lazyvim.plugins.extras.lang.json" }, - -- { import = "lazyvim.plugins.extras.ui.mini-animate" }, + { import = "lazyvim.plugins.extras.lang.typescript" }, + { import = "lazyvim.plugins.extras.lang.json" }, + { import = "lazyvim.plugins.extras.ui.mini-animate" }, -- import/override with your plugins { import = "plugins" }, }, diff --git a/lua/config/options.lua b/lua/config/options.lua index 3ea1454..2c20a4e 100644 --- a/lua/config/options.lua +++ b/lua/config/options.lua @@ -1,3 +1,5 @@ -- Options are automatically loaded before lazy.nvim startup -- Default options that are always set: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/options.lua -- Add any additional options here + +vim.opt.winbar = "%=%m %f" diff --git a/lua/plugins/gist.lua b/lua/plugins/gist.lua new file mode 100644 index 0000000..56894e6 --- /dev/null +++ b/lua/plugins/gist.lua @@ -0,0 +1,7 @@ +return { + { + "mattn/vim-gist", + event = "BufRead", + dependencies = "mattn/webapi-vim", + }, +} diff --git a/lua/plugins/goto-preview.lua b/lua/plugins/goto-preview.lua new file mode 100644 index 0000000..adfeba9 --- /dev/null +++ b/lua/plugins/goto-preview.lua @@ -0,0 +1,29 @@ +return { + { + "rmagatti/goto-preview", + config = function() + require("goto-preview").setup({ + width = 120, -- Width of the floating windowconfig + height = 25, -- Height of the floating window + default_mappings = true, -- Bind default mappings + debug = false, -- Print debug information + opacity = nil, -- 0-100 opacity level of the floating window where 100 is fully transparent. + post_open_hook = nil, -- A function taking two arguments, a buffer and a window to be ran as a hook. + references = { -- Configure the telescope UI for slowing the references cycling window. + -- telescope = telescope.themes.get_dropdown({ hide_preview = false }) + }, + -- These two configs can also be passed down to the goto-preview definition and implementation calls for one off "peak" functionality. + focus_on_open = true, -- Focus the floating window when opening it. + dismiss_on_move = false, -- Dismiss the floating window when moving the cursor. + force_close = true, -- passed into vim.api.nvim_win_close's second argument. See :h nvim_win_close + bufhidden = "wipe", -- the bufhidden option to set on the floating window. See :h bufhidden + + -- You can use "default_mappings = true" setup option + -- Or explicitly set keybindings + -- vim.cmd("nnoremap gpd lua require('goto-preview').goto_preview_definition()") + -- vim.cmd("nnoremap gpi lua require('goto-preview').goto_preview_implementation()") + -- vim.cmd("nnoremap gP lua require('goto-preview').close_all_win()") + }) + end, + }, +} diff --git a/lua/plugins/lualine.lua b/lua/plugins/lualine.lua new file mode 100644 index 0000000..b45b85a --- /dev/null +++ b/lua/plugins/lualine.lua @@ -0,0 +1,10 @@ +return { + -- the opts function can also be used to change the default opts: + { + "nvim-lualine/lualine.nvim", + event = "VeryLazy", + opts = function(_, opts) + table.insert(opts.sections.lualine_x, "😄") + end, + }, +} diff --git a/lua/plugins/octo.lua b/lua/plugins/octo.lua new file mode 100644 index 0000000..6adb9d3 --- /dev/null +++ b/lua/plugins/octo.lua @@ -0,0 +1,14 @@ +return { + { + "pwntester/octo.nvim", + cmd = "Octo", + dependencies = { + "nvim-lua/plenary.nvim", + "nvim-telescope/telescope.nvim", + "nvim-tree/nvim-web-devicons", + }, + config = function() + require("octo").setup() + end, + }, +} diff --git a/lua/plugins/supertab.lua b/lua/plugins/supertab.lua new file mode 100644 index 0000000..3672f85 --- /dev/null +++ b/lua/plugins/supertab.lua @@ -0,0 +1,54 @@ +return { + -- Use for completion and snippets (supertab) + -- first: disable default and behavior in LuaSnip + { + "L3MON4D3/LuaSnip", + keys = function() + return {} + end, + }, + -- then: setup supertab in cmp + { + "hrsh7th/nvim-cmp", + dependencies = { + "hrsh7th/cmp-emoji", + }, + ---@param opts cmp.ConfigSchema + opts = function(_, opts) + local has_words_before = function() + table.insert(opts.sources, { name = "emoji" }) + unpack = unpack or table.unpack + local line, col = unpack(vim.api.nvim_win_get_cursor(0)) + return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil + end + + local luasnip = require("luasnip") + local cmp = require("cmp") + + opts.mapping = vim.tbl_extend("force", opts.mapping, { + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + -- You could replace the expand_or_jumpable() calls with expand_or_locally_jumpable() + -- this way you will only jump inside the snippet region + elseif luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() + elseif has_words_before() then + cmp.complete() + else + fallback() + end + end, { "i", "s" }), + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item() + elseif luasnip.jumpable(-1) then + luasnip.jump(-1) + else + fallback() + end + end, { "i", "s" }), + }) + end, + }, +} diff --git a/lua/plugins/symbols-outline.lua b/lua/plugins/symbols-outline.lua new file mode 100644 index 0000000..dbc4605 --- /dev/null +++ b/lua/plugins/symbols-outline.lua @@ -0,0 +1,8 @@ +return { + { + "simrat39/symbols-outline.nvim", + cmd = "SymbolsOutline", + keys = { { "cs", "SymbolsOutline", desc = "Symbols Outline" } }, + config = true, + }, +} diff --git a/lua/plugins/telescope-fzf-native.lua b/lua/plugins/telescope-fzf-native.lua new file mode 100644 index 0000000..4fc4ac9 --- /dev/null +++ b/lua/plugins/telescope-fzf-native.lua @@ -0,0 +1,12 @@ +return { + { + "telescope.nvim", + dependencies = { + "nvim-telescope/telescope-fzf-native.nvim", + build = "make", + config = function() + require("telescope").load_extension("fzf") + end, + }, + }, +} diff --git a/lua/plugins/toggleterm.lua b/lua/plugins/toggleterm.lua new file mode 100644 index 0000000..d1f7ec9 --- /dev/null +++ b/lua/plugins/toggleterm.lua @@ -0,0 +1,41 @@ +return { + { + "akinsho/toggleterm.nvim", + version = "*", + config = true, + keys = { + -- horizontal bash + { "th", "lua TermToggleBash('horizontal')", desc = "H-Bash" }, + { + "", + mode = { "n", "v", "t" }, + " lua TermToggleBash('horizontal')", + desc = "H-Bash", + }, + -- float bash + { "tf", "lua TermToggleBash('float')", desc = "Bash" }, + { + "", + mode = { "n", "v", "t" }, + " lua TermToggleBash('float')", + desc = "Bash", + }, + -- containers + { "tc", "lua TermToggleContainer('float')", desc = "Container" }, + { "", mode = { "n", "x", "o", "t" }, "lua TermToggleContainer('float')", desc = "Container" }, + -- ssh + { "ts", "lua TermToggleSsh('float')", desc = "ssh" }, + { "", mode = { "n", "x", "o", "t" }, "lua TermToggleSsh('float')", desc = "ssh" }, + -- python + { "tp", "lua TermTogglePython('float')", desc = "Python" }, + { "", mode = { "n", "x", "o", "t" }, "lua TermTogglePython('float')", desc = "Python" }, + -- node + { "tn", "lua TermToggleNode('float')", desc = "Node" }, + { "", mode = { "n", "x", "o", "t" }, "lua TermToggleNode('float')", desc = "Node" }, + -- lua + { "tl", "lua TermToggleLua('float')", desc = "Lua" }, + { "", mode = { "n", "x", "o", "t" }, "lua TermToggleLua('float')", desc = "Lua" }, + -- + }, + }, +} diff --git a/lua/plugins/window-picker.lua b/lua/plugins/window-picker.lua new file mode 100644 index 0000000..1e1100a --- /dev/null +++ b/lua/plugins/window-picker.lua @@ -0,0 +1,12 @@ +return { + + { + "s1n7ax/nvim-window-picker", + name = "window-picker", + event = "VeryLazy", + version = "2.*", + config = function() + require("window-picker").setup() + end, + }, +} diff --git a/lua/user/csearchpath.lua b/lua/user/csearchpath.lua new file mode 100644 index 0000000..72944d0 --- /dev/null +++ b/lua/user/csearchpath.lua @@ -0,0 +1,56 @@ +-- csearchpath.lua +local function script_path() + local str = debug.getinfo(2, "S").source:sub(2) + return str:match("(.*/)") +end + +function CSearchPaths(args) + args = args or {} + args.cxx = args.cxx or os.getenv("CXX") or "/usr/bin/g++" + args.cppflags = args.cppflags or os.getenv("CPPFLAGS") or "" + args.paths = args.paths or {} + + local cmd = "bash -c 'export CXX=\"" + .. string.gsub(args.cxx, '"', '\\"') + .. '";' + .. 'export CPPFLAGS="' + .. string.gsub(args.cppflags, '"', '\\"') + .. '";' + .. ". " + .. script_path() + .. "csearchpath.sh" + .. ";" + .. "csearchpaths'" + + local proc = io.popen(cmd) + if proc == nil then + return + end + + local s = proc:read("*all") + proc:close() + + if s == nil then + return + end + + -- Add Current working directory + vim.opt.path = vim.fn.getcwd() .. "/**" + + -- Add user search paths + for _, path in pairs(args.paths) do + vim.opt.path = vim.opt.path + path + end + + -- Add toolchain search path + for path in string.gmatch(s, "[^\r\n]+") do + if string.sub(path, 1, 1) ~= "#" then + vim.opt.path = vim.opt.path + path + end + end + + -- Add system wide includes + vim.opt.path = vim.opt.path + ".,/usr/include,," +end + +CSearchPaths() diff --git a/lua/user/csearchpath.sh b/lua/user/csearchpath.sh new file mode 100644 index 0000000..99da09f --- /dev/null +++ b/lua/user/csearchpath.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash + +#export CPPFLAGS="" +#export CFLAGS=-std=c99 +#export CXXFLAGS=-Weffc++ + +function cdefines() { + local regexp="^#include\s+(<|\")\.\.\.(>|\")\s+search\s+starts\s+here:$" + + while read -r line; do + echo "$line" + done <<<"$(${CXX} "${CPPFLAGS}" -xc++ -E -dM - &1)" +} + +function csearchpaths() { + local do_print=0 + local regexp="^#include\s+(<|\")\.\.\.(>|\")\s+search\s+starts\s+here:$" + + while read -r line; do + if [[ "${line}" =~ $regexp ]]; then + echo "${line}" + do_print=1 + continue + elif [ "End of search list." == "${line}" ]; then + do_print=0 + else + : + fi + if [ "${do_print}" == "1" ]; then + echo "${line}" + fi + + done <<<"$(${CXX} "${CPPFLAGS}" -xc++ -E -v - &1)" +} diff --git a/lua/user/docker.lua b/lua/user/docker.lua new file mode 100644 index 0000000..32d6354 --- /dev/null +++ b/lua/user/docker.lua @@ -0,0 +1,57 @@ +json = require("thirdparty.json.json") +utils = require("lua.user.utils") + +local M = {} + +function M.container(args) + args = args or { cmd = "list" } + + function list() + local proc = io.popen("docker ps -as --no-trunc --format json", "r") + if proc == nil then + return false + end + + lst = {} + for line in proc:lines() do + t = json.decode(line) + lst[t.Image] = t + end + return true, lst + end + + function start(cid) + local suc, _, _ = os.execute("docker start " .. cid .. " > /dev/null 2>&1") + return suc ~= nil and suc + end + + function stop(cid) + local suc, _, _ = os.execute("docker stop " .. cid .. " > /dev/null 2>&1") + return suc ~= nil and suc + end + + function restart(cid) + stop(cid) + return start(cid) + end + + function attach(cid, callback) + return start(cid) and callback(cid) + end + + if args.cmd == "list" then + return list() + elseif args.cmd == "start" then + return start(args.cid) + elseif args.cmd == "stop" then + return stop(args.cid) + elseif args.cmd == "restart" then + return restart(args.cid) + elseif args.cmd == "attach" then + return attach(args.cid, args.callback) + else + return list() + end +end + +return M diff --git a/lua/user/scratchpad.lua b/lua/user/scratchpad.lua new file mode 100644 index 0000000..2a89eda --- /dev/null +++ b/lua/user/scratchpad.lua @@ -0,0 +1,6 @@ +local utils = require("user.utils") + +vim.ui.select({ "apple", "banana", "mango" }, { + prompt = "Title", + telescope = require("telescope.themes").get_cursor(), +}, function(selected) end) diff --git a/lua/user/toggleterm.lua b/lua/user/toggleterm.lua new file mode 100644 index 0000000..c028520 --- /dev/null +++ b/lua/user/toggleterm.lua @@ -0,0 +1,236 @@ +-- toggleterm-config.lua +local utils = require("user.utils") +local docker = require("user.docker") + +local status_ok, toggleterm = pcall(require, "toggleterm") +if not status_ok then + return +end + +toggleterm.setup({ + size = 20, + open_mapping = [[]], + hide_numbers = true, + shade_filetypes = {}, + shade_terminals = true, + shading_factor = 2, + start_in_insert = true, + insert_mappings = true, + persist_size = true, + direction = "float", + close_on_exit = true, + shell = vim.o.shell, + float_opts = { + border = "curved", + winblend = 0, + highlights = { + border = "Normal", + background = "Normal", + }, + }, +}) + +---@diagnostic disable-next-line: duplicate-set-field +function _G.set_terminal_keymaps() + local opts = { noremap = true } + vim.api.nvim_buf_set_keymap(0, "t", "", [[]], opts) + vim.api.nvim_buf_set_keymap(0, "t", "jk", [[]], opts) + vim.api.nvim_buf_set_keymap(0, "t", "", [[h]], opts) + vim.api.nvim_buf_set_keymap(0, "t", "", [[j]], opts) + vim.api.nvim_buf_set_keymap(0, "t", "", [[k]], opts) + vim.api.nvim_buf_set_keymap(0, "t", "", [[l]], opts) +end + +vim.cmd("autocmd! TermOpen term://* lua set_terminal_keymaps()") + +local Terminal = require("toggleterm.terminal").Terminal + +local ids = 0 +local function next_id() + id = ids + ids = ids + 1 + return id +end + +local function get_ssh_hosts(path) + local hosts = {} + path = path or os.getenv("HOME") .. "/.ssh/config" + + if type(path) ~= "string" then + print("expected arg path of type 'string' actual '" .. type(path)) + return hosts + end + + if not utils.file_exists(path) then + print("file '" .. path .. "' do not exist!") + return hosts + end + + local file = io.open(path, "r") + if file == nil then + print("could not open file '" .. path .. "'!") + return hosts + end + + local cur_host = "" + for line in file:lines() do + for key, value in string.gmatch(line, "%s*(%w+)%s+(.*)") do + if key == "Host" then + cur_host = string.lower(value) + hosts[cur_host] = {} + hosts[cur_host][string.lower(key)] = cur_host + else + hosts[cur_host][string.lower(key)] = value + end + end + end + + for k, _ in pairs(hosts) do + hosts[k].port = hosts[k].port or 22 + hosts[k].hostname = hosts[k].hostname or os.getenv("HOSTNAME") + hosts[k].user = hosts[k].user or os.getenv("USERNAME") + hosts[k].term = nil + end + hosts.current = nil + return hosts +end + +local function get_containers() + local suc, hosts = docker.container({ cmd = "list" }) + if not suc or hosts == nil then + return {} + end + + for k, _ in pairs(hosts) do + hosts[k].term = nil + end + hosts.current = nil + return hosts +end + +local bash = { + float = { + term = nil, + size = -1, + }, + vertical = { + term = nil, + size = 80, + }, + horizontal = { + term = nil, + size = 20, + }, +} + +function TermToggleBash(direction) + direction = direction or "float" + if bash[direction].term == nil then + bash[direction].term = Terminal:new({ + id = next_id(), + display_name = "bash:" .. direction, + cmd = "bash", + direction = direction, + hidden = true, + }) + end + bash[direction].term:toggle(bash[direction].size, direction) +end + +local lazygit = nil +function TermToggleLazyGit() + if lazygit == nil then + lazygit = Terminal:new({ id = next_id(), display_name = "lazygit", cmd = "lazygit", hidden = true }) + end + lazygit:toggle() +end + +local lua = nil +function TermToggleLua() + if lua == nil then + lua = Terminal:new({ id = next_id(), display_name = "Lua", cmd = "lua", hidden = true }) + end + lua:toggle() +end + +local node = nil +function TermToggleNode() + if node == nil then + node = Terminal:new({ id = next_id(), display_name = "Node", cmd = "node", hidden = true }) + end + node:toggle() +end + +local python = nil +function TermTogglePython() + if python == nil then + python = Terminal:new({ id = next_id(), display_name = "Python", cmd = "python3", hidden = true }) + end + python:toggle() +end + +local htop = nil +function _HTOP_TOGGLE() + if htop == nil then + htop = Terminal:new({ id = next_id(), display_name = "htop", cmd = "htop", hidden = true }) + end + htop:toggle() +end + +local ssh_hosts = { items = get_ssh_hosts(), selected = nil } +function TermSelectSsh() + vim.ui.select(utils.get_keys(ssh_hosts.items), { + prompt = "Select a host:", + }, function(choice) + host = ssh_hosts.items[choice] + if host ~= nil then + if host.term == nil then + host.term = Terminal:new({ + id = next_id(), + display_name = "ssh://" .. host.user .. "@" .. host.hostname .. ":" .. host.port, + cmd = "ssh " .. host.user .. "@" .. host.hostname .. " -p" .. host.port, + hidden = true, + }) + end + host.term:open() + ssh_hosts.selected = host + end + end) +end + +function TermToggleSsh() + if ssh_hosts.selected ~= nil then + ssh_hosts.selected.term:toggle() + else + TermSelectSsh() + end +end + +local containers = { items = get_containers(), selected = nil } +function TermSelectContainer() + vim.ui.select(utils.get_keys(containers.items), { + prompt = "Select a container:", + }, function(choice) + container = containers.items[choice] + if container ~= nil then + if container.term == nil then + container.term = Terminal:new({ + id = next_id(), + display_name = "container://" .. container.Image, + cmd = "docker start " .. container.ID .. " && docker attach " .. container.ID, + hidden = true, + }) + end + container.term:open() + containers.selected = container + end + end) +end + +function TermToggleContainer() + if containers.selected ~= nil then + containers.selected.term:toggle() + else + TermSelectContainer() + end +end diff --git a/lua/user/utils.lua b/lua/user/utils.lua new file mode 100644 index 0000000..bd23bc3 --- /dev/null +++ b/lua/user/utils.lua @@ -0,0 +1,55 @@ +local M = {} + +function M.debug(...) + print(...) +end + +function M.dump(t) + if type(t) == "table" then + local str = "{ " + for key, value in pairs(t) do + if type(key) ~= "number" then + key = '"' .. key .. '"' + end + str = str .. "[" .. key .. "] = " .. M.dump(value) .. "," + end + return str .. "} " + else + return tostring(t) + end +end + +function M.file_exists(path) + local file = io.open(path, "r") + if file ~= nil then + io.close(file) + return true + else + return false + end +end + +function M.script_path() + local str = debug.getinfo(2, "S").source:sub(2) + return str:match("(.*/)") +end + +function M.get_keys(t) + local keys = {} + for key, _ in pairs(t) do + table.insert(keys, key) + end + return keys +end + +function M.get_values(t, key) + local values = {} + for k, v in pairs(t) do + if key == k then + table.insert(values, v) + end + end + return values +end + +return M diff --git a/thirdparty/json b/thirdparty/json new file mode 160000 index 0000000..dbf4b2d --- /dev/null +++ b/thirdparty/json @@ -0,0 +1 @@ +Subproject commit dbf4b2dd2eb7c23be2773c89eb059dadd6436f94