Mercurial > dotfiles
comparison dot_config/nvim/lua/terminal.lua @ 336:7a8b66395d69
Add floating terminal to be toggled via c-z
author | zegervdv <zegervdv@me.com> |
---|---|
date | Thu, 21 Jan 2021 09:17:28 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
335:cd9a31235699 | 336:7a8b66395d69 |
---|---|
1 -- Copied from: https://github.com/kutsan/dotfiles/blob/b2046a6c0bcc754fc381351119c14c374721fd4d/.config/nvim/lua/kutsan/mappings/normal/terminal.lua | |
2 | |
3 local api = vim.api | |
4 local fn = vim.fn | |
5 | |
6 local terminal = { | |
7 buf = nil, | |
8 win = nil, | |
9 pid = nil | |
10 } | |
11 | |
12 terminal.open = function () | |
13 -- Create buffer. | |
14 local buf = nil | |
15 | |
16 if terminal.buf and api.nvim_buf_is_loaded(terminal.buf) then | |
17 buf = terminal.buf | |
18 else | |
19 buf = api.nvim_create_buf(false, true) | |
20 end | |
21 | |
22 -- Create window. | |
23 local width = math.ceil(vim.o.columns * 0.8) | |
24 local height = math.ceil(vim.o.lines * 0.9) | |
25 | |
26 local win = api.nvim_open_win(buf, true, { | |
27 relative = 'editor', | |
28 style = 'minimal', | |
29 width = width, | |
30 height = height, | |
31 col = math.ceil((vim.o.columns - width) / 2), | |
32 row = math.ceil((vim.o.lines - height) / 2 - 1), | |
33 }) | |
34 api.nvim_win_set_option(win, 'winhighlight', 'Normal:CursorLine') | |
35 | |
36 -- Launch terminal. | |
37 if not terminal.buf then | |
38 terminal.pid = fn.termopen(string.format('%s --login', os.getenv('SHELL'))) | |
39 end | |
40 | |
41 vim.cmd('startinsert') | |
42 vim.cmd("autocmd! TermClose <buffer> lua require('terminal').close(true)") | |
43 | |
44 -- Save current handles. | |
45 terminal.win = win | |
46 terminal.buf = buf | |
47 end | |
48 | |
49 terminal.close = function (force) | |
50 if not terminal.win then | |
51 return | |
52 end | |
53 | |
54 if api.nvim_win_is_valid(terminal.win) then | |
55 api.nvim_win_close(terminal.win, false) | |
56 terminal.win = nil | |
57 end | |
58 | |
59 -- Force close upon terminal exit. | |
60 if force then | |
61 if api.nvim_buf_is_loaded(terminal.buf) then | |
62 api.nvim_buf_delete(terminal.buf, { force = true }) | |
63 end | |
64 | |
65 fn.jobstop(terminal.pid) | |
66 | |
67 terminal.buf = nil | |
68 terminal.pid = nil | |
69 end | |
70 end | |
71 | |
72 terminal.toggle = function () | |
73 if not terminal.win then | |
74 terminal.open() | |
75 else | |
76 terminal.close() | |
77 end | |
78 end | |
79 | |
80 return terminal |