-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathon_output_notify.lua
More file actions
147 lines (131 loc) · 4.68 KB
/
Copy pathon_output_notify.lua
File metadata and controls
147 lines (131 loc) · 4.68 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
local util = require("overseer.util")
local function has_nvim_notify()
return not not pcall(require, "notify")
end
local function get_notify_config(setting, default)
local notify_setting = vim.F.npcall(function()
return require("notify")._config()[setting]()
end)
return vim.F.if_nil(notify_setting, default)
end
---@type overseer.ComponentFileDefinition
return {
desc = "Use nvim-notify to show notification with task output summary for long-running tasks",
long_desc = vim.trim([[
Works like on_complete_notify but, for long-running commands, also shows real-time output summary.
Requires nvim-notify to modify the last notification window when new output arrives instead of creating new notification.
]]),
params = {
max_lines = {
desc = "Number of lines of output to show",
type = "integer",
default = 1,
validate = function(v)
return v > 0
end,
},
max_width = {
desc = "Maximum output width",
type = "integer",
optional = true,
default = get_notify_config("max_width", 49),
validate = function(v)
return v > 0
end,
},
delay_ms = {
desc = "Time in milliseconds to wait before displaying the notification during task runtime",
type = "number",
default = 2000,
validate = function(v)
return v >= 0
end,
},
trim = {
desc = "Remove whitespace from both sides of each line",
type = "boolean",
default = true,
},
output_on_complete = {
desc = "Show the last lines of task output and status on completion (instead of only the status)",
long_desc = vim.trim([[
When output_on_complete==true: shows status + last output lines during task runtime and after completion.
When output_on_complete==false: shows status + last output lines during task runtime and only status after completion.
]]),
type = "boolean",
default = false,
},
},
constructor = function(params)
return {
lines = {},
notification_id = nil,
last_status = nil,
start_time = nil,
defer_update_lines = util.debounce(function(self, task, bufnr, num_lines)
if vim.api.nvim_buf_is_valid(bufnr) then
self.lines = util.get_last_output_lines(bufnr, num_lines)
self:update_notification(task)
end
end, { delay = 10, reset_timer_on_call = true }),
update_notification = function(self, task)
-- Don't notify on output without nvim-notify installed, as this would create
-- a lot of separate notifications instead of replacing the same one.
if task:is_running() and not has_nvim_notify() then
vim.notify_once(
"overseer.component.on_output_notify requires nvim-notify",
vim.log.levels.WARN
)
return
end
local header = string.format("%s %s", task.status, task.name)
local max_width = math.max(params.max_width or 0, #header)
local lines = { header }
if task:is_running() or params.output_on_complete then
for _, line in ipairs(self.lines) do
if params.trim then
line = vim.trim(line)
end
if #line > max_width then
line = line:sub(1, max_width - 1) .. "…"
end
table.insert(lines, line)
end
end
local msg = table.concat(lines, "\n")
local level = util.status_to_log_level(task.status)
local ret = vim.notify(msg, level, {
replace = self.notification_id,
hide_from_history = self.notification_id and self.last_status == task.status,
timeout = not task:is_running() and get_notify_config("default_timeout", 5000) or false,
})
self.notification_id = ret and ret.id
self.last_status = task.status
end,
on_reset = function(self)
self.lines = {}
self.notification_id = nil
self.last_status = nil
self.start_time = nil
end,
on_start = function(self)
self.start_time = vim.uv.now()
end,
on_output = function(self, task, _data)
local elapsed = vim.uv.now() - self.start_time
if elapsed < params.delay_ms then
return
end
local bufnr = task:get_bufnr()
self.lines = util.get_last_output_lines(bufnr, params.max_lines)
self:update_notification(task)
-- Update again after delay because the terminal buffer takes a few millis to be updated
-- after output is received
self.defer_update_lines(self, task, bufnr, params.max_lines)
end,
on_complete = function(self, task, _status)
self:update_notification(task)
end,
}
end,
}