🫙 tiny scratch terminal for Neovim
0
fork

Configure Feed

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

fix: ensure win and buf are valid

add:
- `Terminal:isbufvalid()`
- `Terminal:ensurebuf()`
- `Terminal:iswinvalid()`

robin b3057967 2e676f6b

+36 -17
+36 -17
lua/jamjar/terminal.lua
··· 47 47 return setmetatable(vim.tbl_extend("force", get_defaultopts(), opts or {}), self) 48 48 end 49 49 50 + function Terminal:isbufvalid() 51 + return self.buf and vim.api.nvim_buf_is_valid(self.buf) 52 + end 53 + 54 + function Terminal:ensurebuf() 55 + if not self:isbufvalid() then 56 + self:init() 57 + end 58 + end 59 + 50 60 function Terminal:init() 51 61 self.buf = vim.api.nvim_create_buf(true, true) 52 62 vim.api.nvim_set_option_value("buftype", "nofile", { buf = self.buf }) ··· 60 70 end, 61 71 }) 62 72 end) 73 + end 74 + 75 + function Terminal:iswinvalid() 76 + return self.win and vim.api.nvim_win_is_valid(self.win) 63 77 end 64 78 65 79 function Terminal:toggle() 66 - if not self.win then 80 + self:ensurebuf() 81 + 82 + if not self:iswinvalid() then 67 83 self:open() 68 84 return 69 85 end ··· 79 95 end 80 96 81 97 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()) 98 + if not self:iswinvalid() then 99 + self:ensurebuf() 100 + self.win = vim.api.nvim_open_win(self.buf, true, vim.F.if_nil(self.winoptsfn, get_winopts)()) 87 101 vim.api.nvim_set_option_value("signcolumn", "no", { win = self.win }) 88 102 vim.api.nvim_set_option_value("foldcolumn", "0", { win = self.win }) 89 103 self.on_open(self) ··· 96 110 end 97 111 98 112 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 113 + if not self:iswinvalid() then 114 + return 115 + end 116 + 117 + vim.api.nvim_win_set_config(self.win, { 118 + hide = true, 119 + }) 120 + 121 + if vim.api.nvim_get_current_win() == self.win then 122 + vim.cmd.wincmd("p") 106 123 end 107 124 end 108 125 109 126 function Terminal:quit() 110 - if self.win then 111 - vim.api.nvim_win_close(self.win, true) 112 - self.win = nil 127 + if not self:iswinvalid() then 128 + return 113 129 end 130 + 131 + vim.api.nvim_win_close(self.win, true) 132 + self.win = nil 114 133 end 115 134 116 135 function Terminal:delete() 117 136 self:quit() 118 137 119 - if self.buf and vim.api.nvim_buf_is_valid(self.buf) then 138 + if self:isbufvalid() then 120 139 vim.bo[self.buf].buflisted = false 121 140 vim.api.nvim_buf_delete(self.buf, { unload = true }) 122 141 self.buf = nil