forked from AnirudhG07/custom-shell.yazi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
223 lines (193 loc) · 5.98 KB
/
init.lua
File metadata and controls
223 lines (193 loc) · 5.98 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 state_option = ya.sync(function(state, attr)
return state[attr]
end)
local function shell_choice(shell_val)
-- input is in lowercase always
local alt_name_map = {
kornshell = "ksh",
powershell = "pwsh",
nushell = "nu",
}
local shell_map = {
bash = { shell_val = "bash", supporter = "-ic", wait_cmd = "read" },
zsh = { shell_val = "zsh", supporter = "-ic", wait_cmd = "read" },
fish = { shell_val = "fish", supporter = "-c", wait_cmd = "read" },
pwsh = { shell_val = "pwsh", supporter = "-Command", wait_cmd = "Read-Host" },
sh = { shell_val = "sh", supporter = "-c", wait_cmd = "read" },
ksh = { shell_val = "ksh", supporter = "-c", wait_cmd = "read" },
csh = { shell_val = "csh", supporter = "-c", wait_cmd = "$<" },
tcsh = { shell_val = "tcsh", supporter = "-c", wait_cmd = "$<" },
dash = { shell_val = "dash", supporter = "-c", wait_cmd = "read" },
nu = { shell_val = "nu", supporter = "-ic", wait_cmd = "input" },
}
shell_val = alt_name_map[shell_val] or shell_val
local shell_info = shell_map[shell_val]
if shell_info then
return shell_info.shell_val, shell_info.supporter, shell_info.wait_cmd
else
return nil, "-c", "read"
end
end
local function manage_extra_args(args)
-- set default values, --custom-shell.yazi does not use --interactive but --confirm.
local block, confirm, orphan, wait = true, true, false, false
for _, arg in ipairs(args) do
if arg == "-nb" or arg == "--no-block" then
block = false
elseif arg == "-nc" or arg == "--no-confirm" then
confirm = false
elseif arg == "-o" or arg == "--orphan" then
orphan = true
elseif arg == "-w" or arg == "--wait" then
wait = true
end
end
return block, confirm, orphan, wait
end
local function manage_additional_title_text(block, wait)
local txt = ""
if block or wait then
txt = "(" .. (block and wait and "block and wait" or block and "block" or "") .. ")"
end
return txt
end
local function history_add(history_path, cmd_run)
local history_file = history_path
local history = {}
-- Ensure the history file exists
local file = io.open(history_file, "a+")
if file then
file:close()
end
-- Read existing history
for line in io.lines(history_file) do
-- add line if it is not empty
if line:match("%S") then
table.insert(history, line)
end
end
if cmd_run == nil then
return history
end
-- Append the new command to the history if it doesn't already exist
local command_exists = false
for _, cmd in ipairs(history) do
if cmd == cmd_run then
command_exists = true
break
end
end
if not command_exists then
file = io.open(history_file, "a")
if file then
file:write(cmd_run .. "\n")
file:close()
end
end
return history
end
local function history_prev(history_path)
-- get the history commands
local history_cmds = history_add(history_path)
if #history_cmds < 1 then
ya.notify({ title = "Custom-Shell", content = "History is Empty.", timeout = 3 })
return
end
-- preview the commands list in fzf and return selected cmd
for i, cmd in ipairs(history_cmds) do
history_cmds[i] = cmd:gsub("'", "'\\''") -- Escape single quotes
end
local permit = ya.hide()
local cmd = string.format('%s < "%s"', "fzf", history_path)
local handle = io.popen(cmd, "r")
local his_cmd = ""
if handle then
-- strip
his_cmd = string.gsub(handle:read("*all") or "", "^%s*(.-)%s*$", "%1")
handle:close()
end
permit:drop()
return his_cmd
end
local function entry(_, args)
local shell_env = os.getenv("SHELL"):match(".*/(.*)")
local shell_value, cmd, custom_shell_cmd = "", "", ""
local history_path, save_history = state_option("history_path"), state_option("save_history")
if args[1] == "auto" or args[1] == "history" then
shell_value = shell_env:lower()
elseif args[1] == "custom" then
if args[2] == "--wait" or args[2] == "-w" then
shell_value = args[3]
cmd = args[4]
else
shell_value = args[2]
cmd = args[3]
end
elseif args[1] ~= "history" then
shell_value = args[1]:lower()
end
local shell_val, supp, wait_cmd = shell_choice(shell_value:lower())
if args[1] == "history" then
local his_cmd = history_prev(history_path)
if his_cmd == nil then
return
end
local value, event = ya.input({
title = "Custom-Shell History",
position = { "top-center", y = 3, w = 40 },
value = his_cmd,
})
if event == 1 then
-- this may lead to nested zsh -c "zsh -c 'zsh -c ...'"
-- But that's not a problem
cmd = value
end
end
if shell_val == nil then
ya.notify("Unsupported shell: " .. shell_value .. "Choosing Default Shell: " .. shell_env)
shell_val, supp = shell_choice(shell_env)
end
local block, confirm, orphan, wait = manage_extra_args(args)
local additional_title_text = manage_additional_title_text(block, wait)
local input_title = shell_value .. " Shell " .. additional_title_text .. ": "
local event = 1
if args[1] ~= "custom" and args[1] ~= "history" then
cmd, event = ya.input({
title = input_title,
position = { "top-center", y = 3, w = 40 },
})
end
if event == 1 then
local after_cmd = wait and wait_cmd or "exit"
-- for history also, this will be added.
custom_shell_cmd = shell_val .. " " .. supp .. " " .. ya.quote(cmd .. "; " .. after_cmd, true)
ya.manager_emit("shell", {
custom_shell_cmd,
block = block,
confirm = confirm,
orphan = orphan,
})
if save_history then
if args[1] == "history" then
-- to avoid nested "zsh -c 'zsh -c ...'"
history_add(history_path, cmd)
else
history_add(history_path, custom_shell_cmd)
end
end
end
end
return {
setup = function(state, options)
local default_history_path = (
ya.target_family() == "windows"
and os.getenv("APPDATA") .. "\\yazi\\config\\plugins\\custom-shell.yazi\\yazi_cmd_history"
) or (os.getenv("HOME") .. "/.config/yazi/plugins/custom-shell.yazi/yazi_cmd_history")
state.history_path = options.history_path or default_history_path
if state.history_path:lower() == "default" then
state.history_path = default_history_path
end
state.save_history = options.save_history and true
end,
entry = entry,
}