🫙 tiny scratch terminal for Neovim
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: init

robin 2e676f6b

+224
+21
lua/jamjar/init.lua
··· 1 + local jamjar = {} 2 + 3 + ---@type table<string, jamjar.Terminal> 4 + jamjar._terminals = {} 5 + 6 + jamjar.make = function(name, opts) 7 + return function() 8 + local term = jamjar._terminals[name] 9 + if not term then 10 + term = require("jamjar.terminal"):new(opts) 11 + vim.schedule(function() 12 + jamjar._terminals[name] = term 13 + end) 14 + 15 + term:init() 16 + end 17 + term:toggle() 18 + end 19 + end 20 + 21 + return jamjar
+126
lua/jamjar/terminal.lua
··· 1 + ---@return vim.api.keyset.win_config 2 + local function get_winopts() 3 + local horiz = vim.opt.fillchars:get().horiz or "─" 4 + 5 + local height = math.ceil(vim.o.lines * 0.3) 6 + local offset = vim.o.cmdheight + 1 7 + 8 + return { 9 + relative = "editor", 10 + col = 0, 11 + row = vim.o.lines - height - offset - 1, 12 + height = height, 13 + width = vim.o.columns, 14 + style = "minimal", 15 + border = { horiz, horiz, horiz, "", "", "", "", "" }, 16 + } 17 + end 18 + 19 + ---@class jamjar.Terminal.proto 20 + ---@field cmd? string 21 + ---@field cwd? string 22 + ---@field winoptsfn? fun(): vim.api.keyset.win_config 23 + ---@field on_open? fun(self: jamjar.Terminal) 24 + 25 + ---@class jamjar.Terminal : jamjar.Terminal.proto 26 + ---@field cmd string 27 + ---@field buf integer 28 + ---@field win? integer 29 + local Terminal = {} 30 + Terminal.__index = Terminal 31 + 32 + local function get_defaultopts() 33 + return { 34 + cmd = vim.o.shell, 35 + on_open = function(self) 36 + vim.api.nvim_set_option_value( 37 + "winhighlight", 38 + "Normal:Normal,NormalNC:NormalNC,FloatBorder:WinSeparator", 39 + { win = self.win } 40 + ) 41 + end, 42 + } 43 + end 44 + 45 + ---@param opts? jamjar.Terminal.proto 46 + function Terminal:new(opts) 47 + return setmetatable(vim.tbl_extend("force", get_defaultopts(), opts or {}), self) 48 + end 49 + 50 + function Terminal:init() 51 + self.buf = vim.api.nvim_create_buf(true, true) 52 + vim.api.nvim_set_option_value("buftype", "nofile", { buf = self.buf }) 53 + 54 + vim._with({ buf = self.buf }, function() 55 + vim.fn.jobstart(self.cmd, { 56 + term = true, 57 + cwd = self.cwd, 58 + on_exit = function() 59 + self:delete() 60 + end, 61 + }) 62 + end) 63 + end 64 + 65 + function Terminal:toggle() 66 + if not self.win then 67 + self:open() 68 + return 69 + end 70 + 71 + local wcfg = vim.api.nvim_win_get_config(self.win) 72 + local hidden = wcfg.hide 73 + 74 + if hidden then 75 + self:open() 76 + else 77 + self:close() 78 + end 79 + end 80 + 81 + function Terminal:open() 82 + if not self.win then 83 + if not self.buf then 84 + self:init() 85 + end 86 + self.win = vim.api.nvim_open_win(self.buf, true, self.winoptsfn and self.winoptsfn() or get_winopts()) 87 + vim.api.nvim_set_option_value("signcolumn", "no", { win = self.win }) 88 + vim.api.nvim_set_option_value("foldcolumn", "0", { win = self.win }) 89 + self.on_open(self) 90 + else 91 + vim.api.nvim_win_set_config(self.win, { 92 + hide = false, 93 + }) 94 + vim.api.nvim_set_current_win(self.win) 95 + end 96 + end 97 + 98 + function Terminal:close() 99 + if self.win then 100 + vim.api.nvim_win_set_config(self.win, { 101 + hide = true, 102 + }) 103 + if vim.api.nvim_get_current_win() == self.win then 104 + vim.cmd.wincmd("p") 105 + end 106 + end 107 + end 108 + 109 + function Terminal:quit() 110 + if self.win then 111 + vim.api.nvim_win_close(self.win, true) 112 + self.win = nil 113 + end 114 + end 115 + 116 + function Terminal:delete() 117 + self:quit() 118 + 119 + if self.buf and vim.api.nvim_buf_is_valid(self.buf) then 120 + vim.bo[self.buf].buflisted = false 121 + vim.api.nvim_buf_delete(self.buf, { unload = true }) 122 + self.buf = nil 123 + end 124 + end 125 + 126 + return Terminal
+9
plugin/jamjar.lua
··· 1 + if vim.g.loaded_jamjar then 2 + return 3 + end 4 + 5 + vim.g.loaded_jamjar = true 6 + 7 + local jamjar = require("jamjar") 8 + 9 + vim.keymap.set("n", "<Plug>(jamjar-toggle-scratch)", jamjar.make("scratch"))
+4
selene.toml
··· 1 + std = "vim" 2 + 3 + [rules] 4 + mixed_table = "allow"
+9
stylua.toml
··· 1 + indent_type = "Spaces" 2 + indent_width = 2 3 + column_width = 120 4 + quote_style = "AutoPreferDouble" 5 + call_parentheses = "Always" 6 + line_endings = "Unix" 7 + 8 + [sort_requires] 9 + enabled = true
+55
vim.toml
··· 1 + [selene] 2 + base = "lua51" 3 + name = "vim" 4 + 5 + [vim] 6 + any = true 7 + 8 + [[describe.args]] 9 + type = "string" 10 + [[describe.args]] 11 + type = "function" 12 + 13 + [[it.args]] 14 + type = "string" 15 + [[it.args]] 16 + type = "function" 17 + 18 + [[before_each.args]] 19 + type = "function" 20 + [[after_each.args]] 21 + type = "function" 22 + 23 + [assert.is_not] 24 + any = true 25 + 26 + [assert.matches] 27 + any = true 28 + 29 + [assert.has_error] 30 + any = true 31 + 32 + [[assert.equals.args]] 33 + type = "any" 34 + [[assert.equals.args]] 35 + type = "any" 36 + [[assert.equals.args]] 37 + type = "any" 38 + required = false 39 + 40 + [[assert.same.args]] 41 + type = "any" 42 + [[assert.same.args]] 43 + type = "any" 44 + 45 + [[assert.truthy.args]] 46 + type = "any" 47 + 48 + [[assert.falsy.args]] 49 + type = "any" 50 + 51 + [[assert.spy.args]] 52 + type = "any" 53 + 54 + [[assert.stub.args]] 55 + type = "any"