-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathinit.lua
More file actions
223 lines (198 loc) · 6.02 KB
/
init.lua
File metadata and controls
223 lines (198 loc) · 6.02 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
local Context = require("sidekick.cli.context")
local State = require("sidekick.cli.state")
local Util = require("sidekick.util")
local M = {}
---@class sidekick.Prompt
---@field msg string
---@class sidekick.cli.Message
---@field msg? string
---@field prompt? string
---@field text? sidekick.Text[]
---@class sidekick.cli.Config
---@field cmd string[] Command to run the CLI tool
---@field env? table<string, string|false> Environment variables to set when running the command
---@field url? string Web URL to open when the tool is not installed
---@field keys? table<string, sidekick.cli.Keymap|false>
---@field is_proc? (fun(self:sidekick.cli.Tool, proc:sidekick.cli.Proc):boolean)|string Regex or function to identity a running process
---@field mux_focus? boolean wether the tool needs to be focused in order to receive input
---@field format? fun(text:sidekick.Text[], str:string):string?
---@field native_scroll? boolean whether the tool handles scrolling natively
---@class sidekick.cli.Show
---@field name? string
---@field focus? boolean
---@field filter? sidekick.cli.Filter
---@field all? boolean
---@class sidekick.cli.Hide
---@field name? string
---@field filter? sidekick.cli.Filter
---@field all? boolean
---@class sidekick.cli.Send: sidekick.cli.Show,sidekick.cli.Message
---@field submit? boolean
--- Keymap options similar to `vim.keymap.set` and `lazy.nvim` mappings
---@class sidekick.cli.Keymap: vim.keymap.set.Opts
---@field [1] string keymap
---@field [2] string|sidekick.cli.Action
---@field mode? string|string[]
---@generic T: {name?:string, filter?:sidekick.cli.Filter}
---@param opts? T|string
---@return T
local function filter_opts(opts)
opts = type(opts) == "string" and { name = opts } or opts or {}
---@cast opts {name?:string, filter?:sidekick.cli.Filter}
opts.filter = opts.filter or {}
opts.filter.name = opts.name or opts.filter.name or nil
return opts
end
--- Select a prompt to send
---@param opts? sidekick.cli.Prompt|{cb:nil}
---@overload fun(cb:fun(msg?:string))
function M.prompt(opts)
opts = opts or {}
opts = type(opts) == "function" and { cb = opts } or opts --[[@as sidekick.cli.Prompt]]
opts.cb = opts.cb or function(_, text)
if text then
M.send({ text = text })
end
end
require("sidekick.cli.ui.prompt").select(opts)
end
--- Start or attach to a CLI tool
---@param opts? sidekick.cli.Select|{cb:nil}|{focus?:boolean}
---@overload fun(cb:fun(state?:sidekick.cli.State))
function M.select(opts)
opts = opts or {}
opts = type(opts) == "function" and { cb = opts } or opts --[[@as sidekick.cli.Select]]
opts.cb = opts.cb
or function(state)
if state then
State.attach(state, { show = true, focus = opts.focus })
end
end
require("sidekick.cli.ui.select").select(opts)
end
---@param opts? sidekick.cli.Show
---@overload fun(name: string)
function M.show(opts)
opts = filter_opts(opts)
State.with(function() end, {
all = opts.all,
attach = true,
filter = opts.filter,
focus = opts.focus,
show = true,
})
end
---@param opts? sidekick.cli.Show
---@overload fun(name: string)
function M.toggle(opts)
opts = filter_opts(opts)
State.with(function(state, attached)
if state.terminal and not attached then
state.terminal:toggle()
end
if opts.focus == false then
return
end
if state.external or (state.terminal and state.terminal:is_open()) then
state.session:focus()
end
end, {
attach = true,
filter = opts.filter,
})
end
--- Toggle focus of the terminal window if it is already open
---@param opts? sidekick.cli.Show
---@overload fun(name: string)
function M.focus(opts)
opts = filter_opts(opts)
State.with(function(state)
if state.session:is_focused() then
state.session:blur()
else
state.session:focus()
end
end, {
attach = true,
filter = opts.filter,
focus = false,
show = true,
})
end
---@param opts? sidekick.cli.Hide
---@overload fun(name: string)
function M.hide(opts)
opts = filter_opts(opts)
State.with(function(state)
return state.terminal and state.terminal:hide()
end, {
all = opts.all,
filter = Util.merge(opts.filter, { terminal = true }),
})
end
---@param opts? sidekick.cli.Hide
---@overload fun(name: string)
function M.close(opts)
opts = filter_opts(opts)
State.with(State.detach, {
all = opts.all,
filter = Util.merge(opts.filter),
})
end
-- Render a message template or prompt
---@param opts? sidekick.cli.Message|string
function M.render(opts)
return Context.get():render(opts or "")
end
--- Send a message or prompt to a CLI
---@param opts? sidekick.cli.Send
---@overload fun(msg:string)
function M.send(opts)
opts = type(opts) == "string" and { msg = opts } or opts
opts = filter_opts(opts)
if not opts.msg and not opts.prompt and Util.visual_mode() then
opts.msg = "{selection}"
end
local msg, text = "", opts.text ---@type string?, sidekick.Text[]?
if not text then
msg, text = M.render(opts)
if msg == "" or not text then
Util.warn("Nothing to send.")
return
elseif msg == "\n" then
msg = "" -- allow sending a new line
text = {}
end
end
State.with(function(state)
Util.exit_visual_mode()
vim.schedule(function()
msg = state.tool:format(text)
state.session:send(msg .. "\n")
if opts.submit then
state.session:submit()
end
end)
end, {
attach = true,
filter = opts.filter,
focus = opts.focus,
show = true,
})
end
---@deprecated use `require("sidekick.cli").prompt()`
function M.select_prompt(...)
Util.deprecate('require("sidekick.cli").select_prompt()', 'require("sidekick.cli").prompt()')
return M.prompt(...)
end
---@deprecated use `require("sidekick.cli").select()`
function M.select_tool(...)
Util.deprecate('require("sidekick.cli").select_tool()', 'require("sidekick.cli").select()')
return M.select(...)
end
---@deprecated use `require("sidekick.cli").send()`
function M.ask(...)
Util.deprecate('require("sidekick.cli").ask()', 'require("sidekick.cli").send()')
return M.send(...)
end
return M