Skip to content

Commit 2e03a3b

Browse files
committed
✨ Simplify generate_ps1()
1 parent c607f5c commit 2e03a3b

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

prompt-style.lua

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,38 +86,40 @@ local function get_cwd()
8686
end
8787

8888
---generate ps1.
89-
-- `name` is for `get_version()`
9089
-- `sections` is an array whose element is like `{ "white", "blue", get_cwd }`
9190
-- and its order determine the order of prompt sections.
92-
-- `sep` is separator. `char` is the last character like `> `
93-
-- `format` determine the space and by default is ' %s '.
94-
---@param name string
95-
---@param sections {1: integer, 2: integar, 3: string | function(): string}[]
96-
---@param sep string
9791
---@param char string
98-
---@param format string
92+
---@param sections {(1: integer, 2: integar, 3: string | function(): string) | string}[]
9993
---@return function(): string
100-
local function generate_ps1(name, sections, sep, char, format)
101-
name = name or prompt.name
94+
local function generate_ps1(char, sections)
95+
char = char or ""
10296
sections = sections or {
103-
{"black", "yellow", get_icon()}, {"blue", "black", get_version(name)},
97+
---@diagnostic disable: missing-parameter
98+
{"black", "yellow", get_icon()}, {"blue", "black", get_version()},
10499
{"white", "blue", get_cwd}, {"black", "white", get_time}
105100
}
106-
sep = sep or ""
107-
char = char or ""
108-
format = format or " %s "
101+
local sep = ""
102+
local format = " %s "
109103
return function()
110104
local ps1 = ""
111105
local last_bg = ""
112106
for _, v in ipairs(sections) do
113-
local fg, bg, text = table.unpack(v)
114-
if type(text) == "function" then text = text() end
115-
text = string.format(format, text)
116-
if last_bg ~= "" then
117-
ps1 = ps1 .. "%{" .. last_bg .. " " .. bg .. "bg}" .. sep
107+
if type(v) == "string" then
108+
if string.match(v, "%s") then
109+
format = v
110+
else
111+
sep = v
112+
end
113+
else
114+
local fg, bg, text = table.unpack(v)
115+
if type(text) == "function" then text = text() end
116+
text = string.format(format, text)
117+
if last_bg ~= "" then
118+
ps1 = ps1 .. "%{" .. last_bg .. " " .. bg .. "bg}" .. sep
119+
end
120+
ps1 = ps1 .. "%{" .. fg .. " " .. bg .. "bg}" .. text
121+
last_bg = bg
118122
end
119-
ps1 = ps1 .. "%{" .. fg .. " " .. bg .. "bg}" .. text
120-
last_bg = bg
121123
end
122124
ps1 = ps1 .. "%{reset " .. last_bg .. "}"
123125
return ansicolors(ps1) .. "\n" .. char

0 commit comments

Comments
 (0)