-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinit.lua
More file actions
151 lines (132 loc) · 3.45 KB
/
init.lua
File metadata and controls
151 lines (132 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
local config = require('python.config')
-- local Popup = require("nui.popup")
local split_default_style = {
split = 'below',
win = 0,
width = 40,
height = 10,
focusable = true,
}
local popup_default_style = {
border = "rounded",
relative = "win",
focusable = true,
title = "python.nvim",
anchor = "SE",
zindex = 999,
width = 40,
height = 20,
row = vim.o.lines - 3,
col = vim.o.columns - 2,
style = "minimal"
}
---@class UI
---@field win_opts table<string, any>
---@field win number | nil
---@field buf number | nil
local UI = {
win_opts = {},
win = nil,
buf = nil,
}
function UI:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
function UI:mount()
self.buf = vim.api.nvim_create_buf(false, true)
self.win = vim.api.nvim_open_win(self.buf, false, self.win_opts)
end
function UI:unmount()
vim.api.nvim_win_close(self.win, true)
end
local empty_system_ui = {
---@type UI
ui = nil,
line_count = 0,
}
local M = {
system_ui = vim.deepcopy(empty_system_ui)
}
local function _deactivate_system_call_ui()
if M.system_ui.ui then
M.system_ui.ui:unmount()
end
M.system_ui = vim.deepcopy(empty_system_ui)
end
-- Turn off ui
---@param timeout? integer time in milliseconds to close ui. Defaults to config option
function M.deactivate_system_call_ui(timeout)
if timeout == nil then
timeout = config.ui.ui_close_timeout
end
if timeout > 0 then
vim.defer_fn(function()
_deactivate_system_call_ui()
end, timeout)
else
_deactivate_system_call_ui()
end
end
--- Open a ui window to show the output of the command being called.
function M.activate_system_call_ui()
M.deactivate_system_call_ui(0)
local ui = nil
if config.ui.default_ui_style == "popup" then
local win_opts = vim.tbl_deep_extend('keep', config.ui.popup.win_opts or {}, popup_default_style)
ui = UI:new({ win_opts = win_opts })
end
if config.ui.default_ui_style == "split" then
local win_opts = vim.tbl_deep_extend('keep', config.ui.split.win_opts or {}, split_default_style)
ui = UI:new({ win_opts = win_opts })
end
if ui then
-- mount/open the component
ui:mount()
end
M.system_ui.ui = ui
end
--- Open a ui w"indow to show the output of the command being called.
---@param err string stderr data
---@param data string stdout data
---@param flush boolean clear ui text and replace with full output
---@param callback function callback function with no arguments
function M.show_system_call_progress(err, data, flush, callback)
if not M.system_ui.ui then
return
end
local out = data
if not out then
out = ""
end
if err then
out = out .. err
end
vim.schedule(function()
out = out:gsub('\r', '')
local _, line_count = out:gsub('\n', '\n')
if flush then
M.system_ui.line_count = 0
pcall(vim.api.nvim_buf_set_text, M.system_ui.ui.buf, 0, 0, 0, 0, {})
end
local row = M.system_ui.line_count
local increase = row + line_count
if not M.system_ui.ui.buf then
return
end
-- Don't throw errors if we can't set the text on the next line for something reason
pcall(vim.api.nvim_buf_set_text, M.system_ui.ui.buf, row, 0, row, 0, vim.fn.split(out .. "\n", "\n"))
pcall(vim.api.nvim_win_set_cursor, M.system_ui.ui.win, { row, 0 })
M.system_ui.line_count = increase
if callback then
callback()
end
end)
end
return setmetatable(M, {
__index = function(_, k)
return require("python.ui")[k]
end,
})