-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
prompt-style.lua
163 lines (154 loc) · 4.66 KB
/
prompt-style.lua
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
---prompt style.
local os = require "os"
local string = require "string"
local table = require "table"
local lfs = require "lfs"
local ansicolors = require "ansicolors"
local prompt = require "prompt"
---@diagnostic disable: deprecated
if table.unpack == nil then table.unpack = unpack end
---wakatime
---@param cmd string
---@return string
local function wakatime(cmd)
cmd = cmd or "wakatime-cli --write --plugin=repl-lua-wakatime " ..
"--entity-type=app --entity=lua --alternate-language=lua --project=%s &"
local s, _ = string.find(cmd, "%s")
if s ~= nil then
local project = io.popen("git rev-parse --show-toplevel 2> /dev/null"):read()
if project == nil then
project = lfs.currentdir()
end
project = string.match(project, "[^/]+$")
cmd = string.format(cmd, project)
end
io.popen(cmd)
return ""
end
---get distribution
---@return string
local function get_distribution()
local line = io.popen("lsb_release -i 2>/dev/null"):read()
if line == nil then return "linux" end
return string.lower(string.gsub(line, ".*:%s*", ""))
end
---get os
---@return string
local function get_os()
if os.getenv("PREFIX") == "/data/data/com.termux/files/usr" then
return "android"
end
local binary_format = string.match(package.cpath, '([^.]+)[;|$]')
if binary_format == "so" then
return get_distribution()
elseif binary_format == "dll" then
return "windows"
elseif binary_format == "dylib" then
return "macos"
end
return "unknown"
end
---get icon
---@return string
local function get_icon()
local icons = {
unknown = "?",
android = "",
arch = "",
centos = "",
debian = "",
docker = "",
gentoo = "",
linux = "",
macos = "",
ubuntu = "",
nixos = "",
windows = ""
}
return icons[get_os()]
end
---get version
---@param name string
---@param logo string
---@param format string
---@return string
local function get_version(name, logo, format)
name = name or prompt.name
logo = logo or ""
format = format or " %s "
name = string.format(format, name)
local version = string.gsub(_VERSION, ".*%s+", "")
return logo .. name .. version
end
---get time
---@param format string
---@return string
local function get_time(format)
format = format or "%H:%M:%S"
local time = os.date(format, os.time())
if type(time) == "string" then return time end
return ""
end
---get cwd
---@return string
local function get_cwd()
local cwd = string.gsub(lfs.currentdir(), os.getenv("HOME") or "", "~")
cwd = string.gsub(cwd, "[^/]+$", "%%{bright}%0")
return cwd
end
---generate ps1.
-- `sections` is an array whose element is like `{ "white", "blue", get_cwd }`
-- and its order determine the order of prompt sections.
---@param char string
---@param sections {(1: integer, 2: integar, 3: string | function(): string) | string}[]
---@return function(): string
local function generate_ps1(char, sections)
char = char or "❯ "
sections = sections or {
---@diagnostic disable: missing-parameter
{ "", "", wakatime },
{ "black", "yellow", get_icon() }, { "blue", "black", get_version() },
{ "white", "blue", get_cwd }, { "black", "white", get_time }
}
local sep = ""
local format = " %s "
return function()
local ps1 = ""
local last_bg = ""
for _, v in ipairs(sections) do
if type(v) == "string" then
if string.match(v, "%s") then
format = v
else
sep = v
end
else
local fg, bg, text = table.unpack(v)
if type(text) == "function" then text = text() end
if text ~= "" then
text = string.format(format, text)
if last_bg == "" then
ps1 = ps1 .. "%{" .. fg .. " " .. bg .. "bg}" .. text
else
ps1 = ps1 .. "%{" .. last_bg .. " " .. bg .. "bg}" .. sep ..
"%{" .. fg .. "}" .. text
end
last_bg = bg
end
end
end
ps1 = ps1 .. "%{reset " .. last_bg .. "}" .. sep
return ansicolors(ps1) .. "\n" .. char
end
end
---@export
return {
wakatime = wakatime,
get_distribution = get_distribution,
get_os = get_os,
get_icon = get_icon,
get_version = get_version,
get_time = get_time,
get_cwd = get_cwd,
generate_ps1 = generate_ps1
}