@@ -86,38 +86,40 @@ local function get_cwd()
86
86
end
87
87
88
88
--- generate ps1.
89
- -- `name` is for `get_version()`
90
89
-- `sections` is an array whose element is like `{ "white", "blue", get_cwd }`
91
90
-- 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
97
91
--- @param char string
98
- --- @param format string
92
+ --- @param sections { (1 : integer , 2 : integar , 3 : string | function () : string ) | string } []
99
93
--- @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 " ❯ "
102
96
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 ()},
104
99
{" white" , " blue" , get_cwd }, {" black" , " white" , get_time }
105
100
}
106
- sep = sep or " "
107
- char = char or " ❯ "
108
- format = format or " %s "
101
+ local sep = " "
102
+ local format = " %s "
109
103
return function ()
110
104
local ps1 = " "
111
105
local last_bg = " "
112
106
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
118
122
end
119
- ps1 = ps1 .. " %{" .. fg .. " " .. bg .. " bg}" .. text
120
- last_bg = bg
121
123
end
122
124
ps1 = ps1 .. " %{reset " .. last_bg .. " }"
123
125
return ansicolors (ps1 ) .. " \n " .. char
0 commit comments