diff --git a/lua/helpers.lua b/lua/helpers.lua index 09b5fb5..62f4614 100644 --- a/lua/helpers.lua +++ b/lua/helpers.lua @@ -16,5 +16,39 @@ end helpers.edit_cf('h', '/lua/helpers.lua') +---@param opts? { cmd?: string } +helpers.open_term = function(opts) + opts = opts or { cmd = '' } + + local buf = vim.api.nvim_create_buf(false, true) + vim.api.nvim_set_option_value('bufhidden', 'wipe', { buf = buf }) + vim.api.nvim_set_option_value('modifiable', false, { buf = buf }) + + local height = math.ceil(vim.o.lines * 0.9) + local width = math.ceil(vim.o.columns * 0.9) + local win = vim.api.nvim_open_win(buf, true, { + style = 'minimal', + relative = 'editor', + width = width, + height = height, + row = math.ceil((vim.o.lines - height) / 2), + col = math.ceil((vim.o.columns - width) / 2), + border = 'single', + }) + + vim.api.nvim_set_current_win(win) + + vim.fn.jobstart(opts.cmd, { + term = true, + on_exit = function(_, _, _) + if vim.api.nvim_win_is_valid(win) then + vim.api.nvim_win_close(win, true) + end + end, + }) + + vim.cmd.startinsert() +end + return helpers -- nnoremap ev :tabedit $MYVIMRC diff --git a/lua/keymap.lua b/lua/keymap.lua index ec87663..3555471 100644 --- a/lua/keymap.lua +++ b/lua/keymap.lua @@ -1,6 +1,8 @@ -- Basic Keymaps -- See `:help vim.keymap.set()` +local helpers = require 'helpers' + -- Swap : and ; around vim.keymap.set({ 'n', 'v' }, ':', ';') vim.keymap.set({ 'n', 'v' }, ';', ':') @@ -93,24 +95,12 @@ vim.keymap.set('n', '{', 'mzF[`a``%i`z', { desc = 'Ind vim.keymap.set('n', '}', 'mzF[`a``%i`zvi[:s/,\\s*/,\\r/gvi[=`z:nohlsearch', { desc = 'Indent an array some other way?' }) -- Git mappings -vim.keymap.set('n', 'gaf', 'Git add %', { desc = 'Git add current file' }) -vim.keymap.set('n', 'gaa', 'Git add --all', { desc = 'Git add all unstaged changes' }) -vim.keymap.set('n', 'gap', 'Git add --patch --all', { desc = 'Git add all unstaged changes interactively' }) vim.keymap.set('n', 'gb', 'Git blame', { desc = 'Git blame' }) -vim.keymap.set('n', 'gcm', 'Git commit -v', { desc = 'Git commit' }) -vim.keymap.set('n', 'gp', 'Git push', { desc = 'Git push' }) -vim.keymap.set('n', 'gup', 'Git pull --rebase', { desc = 'Git pull --rebase' }) -vim.keymap.set('n', 'gsb', 'Git status', { desc = 'Git status' }) vim.keymap.set('n', 'gd', 'Git diff', { desc = 'Git diff' }) -vim.keymap.set('n', 'gf', 'Git fetch', { desc = 'Git fetch' }) vim.keymap.set('n', 'gdc', 'Git diff --cached', { desc = 'Git diff' }) -vim.keymap.set('n', 'gl', 'Git log', { desc = 'Git log' }) -vim.keymap.set('n', 'gun', 'Git reset -- %', { desc = 'Git unstage file' }) -vim.keymap.set('n', 'gco', 'Git checkout -- %', { desc = 'Git checkout' }) -vim.keymap.set('n', 'G', 'Git', { desc = 'Git' }) -vim.keymap.set('n', 'gsta', 'Git stash push', { desc = 'Git stash' }) -vim.keymap.set('n', 'gstA', 'Git stash apply', { desc = 'Git stash apply' }) -vim.keymap.set('n', 'gstp', 'Git stash pop', { desc = 'Git stash pop' }) +vim.keymap.set('n', 'G', function() + helpers.open_term { cmd = 'lazygit' } +end, { desc = 'Git' }) -- Add keymaps for diff mode vim.api.nvim_create_autocmd('BufEnter', { diff --git a/lua/projects.lua b/lua/projects.lua index fd0c09b..12e7a08 100644 --- a/lua/projects.lua +++ b/lua/projects.lua @@ -3,7 +3,8 @@ local M = {} -- Helper to create mappings -local map = require('helpers').map +local helpers = require 'helpers' +local map = helpers.map -- Get the last folder name from a path local function project_name_from_cwd(cwd) @@ -20,6 +21,9 @@ local PROJECTS = { ':cexpr system("cd server && vendor/bin/phpstan analyse --no-progress --error-format=raw --memory-limit=2G -vv") ', { desc = 'Run lint' } ) + map('db', function() + helpers.open_term { cmd = 'lazysql mysql://root@localhost:3306/runcats' } + end, { desc = 'Open database manager' }) map('s ', '!cd server && ', { desc = 'Run command in server directory' }, 'c') map('c ', '!cd client && ', { desc = 'Run command in client directory' }, 'c') map('sl ', '!cd server && sail ', { desc = 'Run sail command' }, 'c')