-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
359 lines (302 loc) · 10.9 KB
/
main.lua
File metadata and controls
359 lines (302 loc) · 10.9 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
VERSION = '0.1.5'
local micro = import('micro')
local shell = import('micro/shell')
local config = import('micro/config')
local strings = import('strings')
local runtime = import("runtime")
local lastGitStatusRunTime = nil
local lastGitStatusStr = ""
local minGitStatusTick = 10
local currentBufCount = 1
local lastGitStatusTick = -minGitStatusTick
local gitStatusTick = 0
local currentGitStatus = {
populatedCounter = 0,
branch = "",
isBranchDone = false,
conflict = "",
isConflictDone = false,
behindAhead = "",
isBehindAheadDone = false,
stash = "",
isStashDone = false,
stagedModifiedUntracked = "",
isStagedModifiedUntrackedDone = false
}
function getOS()
if runtime.GOOS == "windows" then
return "Windows"
else
return "Unix"
end
end
function runCrossPlatformBackgroundCommand(cmd, exitCallback)
local cmdSplits = {}
local currentOS = getOS()
if currentOS == "Unix" then
-- table.insert(cmdSplits, 'sh')
table.insert(cmdSplits, '-c')
table.insert(cmdSplits, cmd)
shell.JobSpawn("sh", cmdSplits, nil, nil, exitCallback)
else
-- table.insert(cmdSplits, 'cmd')
-- table.insert(cmdSplits, '/s')
table.insert(cmdSplits, '/v:on')
table.insert(cmdSplits, '/c')
table.insert(cmdSplits, cmd)
shell.JobSpawn("cmd", cmdSplits, nil, nil, exitCallback)
-- shell.JobSpawn("git rev-parse --abbrev-ref HEAD", nil, nil, branchDone)
end
end
function branchDone(output)
micro.Log("output:", output)
-- if err == nil then
local _, fatalCount = output:gsub('fatal:', 'fatal:')
if fatalCount > 0 then
currentGitStatus.branch = config.GetGlobalOption('gitStatus.iconNoGit')
else
currentGitStatus.branch = ('%s %s'):format( config.GetGlobalOption('gitStatus.iconBranch'),
output:gsub('%s+', ''))
end
currentGitStatus.isBranchDone = true
-- else
-- currentGitStatus.branch = "error: " .. err:Error()
-- end
end
function startBranch()
runCrossPlatformBackgroundCommand("git rev-parse --abbrev-ref HEAD", branchDone)
end
function conflictDone(output)
local _, fatalCount = output:gsub('Not a git repository', 'Not a git repository')
local res = {}
if fatalCount == 0 then
res = strings.Split(strings.TrimSpace(output), '\n')
end
if #res ~= 0 and res[1] ~= '' then
if config.GetGlobalOption('gitStatus.iconConflict') ~=
config.GetGlobalOption('gitStatus.gitStatus.iconConflit') then
if config.GetGlobalOption('gitStatus.iconConflict') ~= '' then
currentGitStatus.conflict =
(' %s:%s'):format(config.GetGlobalOption('gitStatus.iconConflict'), #res)
else
currentGitStatus.conflict =
(' %s:%s'):format(config.GetGlobalOption('gitStatus.iconConflit'), #res)
end
else
currentGitStatus.conflict =
(' %s:%s'):format(config.GetGlobalOption('gitStatus.iconConflict'), #res)
end
else
currentGitStatus.conflict = ''
end
currentGitStatus.isConflictDone = true
end
function startConflict()
runCrossPlatformBackgroundCommand('git diff --name-only --diff-filter=U', conflictDone)
end
function behindOrAheadDone(output)
-- micro.Log("output:", output)
local res = strings.Split(strings.TrimSpace(output), '')
if res ~= nil and #res >= 3 and #res <= 5 then
behindCount = strings.Split(strings.TrimSpace(output), '')[1]
aheadCount = strings.Split(strings.TrimSpace(output), '')[3]
if behindCount ~= nil and behindCount ~= '0' then
currentGitStatus.behindAhead =
(' %s%s'):format(config.GetGlobalOption('gitStatus.iconBehind'), behindCount)
elseif aheadCount ~= nil and aheadCount ~= '0' then
currentGitStatus.behindAhead =
(' %s%s'):format(config.GetGlobalOption('gitStatus.iconAhead'), aheadCount)
else
currentGitStatus.behindAhead = ''
end
else
currentGitStatus.behindAhead = ''
end
currentGitStatus.isBehindAheadDone = true
end
function startBehindOrAhead()
runCrossPlatformBackgroundCommand('git rev-list --left-right --count @{upstream}...HEAD', behindOrAheadDone)
end
function stashDone(output)
local _, count = output:gsub('@', '')
if count ~= nil and count ~= 0 then
currentGitStatus.stash = (' {%s}'):format(count)
else
currentGitStatus.stash = ''
end
currentGitStatus.isStashDone = true
end
function startStash()
runCrossPlatformBackgroundCommand('git stash list', stashDone)
end
function getStagedModifiedUntrackCount(output)
local stagedCount = 0
local modifiedCount = 0
local untrackCount = 0
for line in output:gmatch("[^\r\n]+") do
local _, curAddCount = string.gsub(line, '^A .*$', '')
if curAddCount == nil then curAddCount = 0 end
local _, curStagedCount = string.gsub(line, '^M .*$', '')
if curStagedCount == nil then curStagedCount = 0 end
local _, curModCount = string.gsub(line, '^.M .*$', '')
if curModCount == nil then curModCount = 0 end
local _, curUntrackCount = string.gsub(output, '^?? .*$', '')
if curUntrackCount == nil then curUntrackCount = 0 end
stagedCount = stagedCount + curAddCount + curStagedCount
modifiedCount = modifiedCount + curModCount
untrackCount = untrackCount + curUntrackCount
end
return stagedCount, modifiedCount, untrackCount
end
function stagedModifiedUntrackedDone(output)
local staged, mod, untracked = getStagedModifiedUntrackCount(output)
local returnStr = ''
if staged ~= 0 then
returnStr = returnStr .. (' %s:%s'):format(config.GetGlobalOption('gitStatus.iconStage'), staged)
end
if mod ~= 0 then
returnStr = returnStr .. (' %s:%s'):format(config.GetGlobalOption('gitStatus.iconModified'), mod)
end
if untracked ~= nil and untracked ~= 0 then
if config.GetGlobalOption('gitStatus.iconUnstage') ~= config.GetGlobalOption('gitStatus.iconUntracked') then
if config.GetGlobalOption('gitStatus.iconUnstage') == 'U' then
returnStr = returnStr .. (' %s:%s'):format(config.GetGlobalOption('gitStatus.iconUntracked'), untracked)
else
returnStr = returnStr .. (' %s:%s'):format(config.GetGlobalOption('gitStatus.iconUntracked'), untracked)
end
else
returnStr = returnStr .. (' %s:%s'):format(config.GetGlobalOption('gitStatus.iconUnstage'), untracked)
end
end
currentGitStatus.stagedModifiedUntracked = returnStr
currentGitStatus.isStagedModifiedUntrackedDone = true
end
function startStagedModifiedUntracked()
runCrossPlatformBackgroundCommand('git status --porcelain --branch', stagedModifiedUntrackedDone)
end
function symbol(branch, stageModifiedUntracked)
local symbol = ''
if branch ~= config.GetGlobalOption('gitStatus.iconNoGit') then
if stageModifiedUntracked ~= '' then
symbol = ' ' .. config.GetGlobalOption('gitStatus.iconBranchNoOK')
else
symbol = ' ' .. config.GetGlobalOption('gitStatus.iconBranchOK')
end
end
return symbol
end
function gitStatusToStr()
return currentGitStatus.branch ..
currentGitStatus.conflict ..
currentGitStatus.behindAhead ..
currentGitStatus.stash ..
currentGitStatus.stagedModifiedUntracked ..
symbol( currentGitStatus.branch,
currentGitStatus.stagedModifiedUntracked)
end
function updateCurrentBufferCount()
currentBufCount = 0
local bp = micro.CurPane()
if bp == nil then
currentBufCount = 1
return
end
currentBufCount = #bp:Tab().Panes
if currentBufCount <= 0 then
currentBufCount = 1
end
end
function halftick()
-- micro.InfoBar():Message("halftick(): ", gitStatusTick)
gitStatusTick = gitStatusTick + 1
lastGitStatusTick = gitStatusTick
updateCurrentBufferCount()
end
function fulltick(doLog)
-- if doLog then
-- micro.InfoBar():Message("fulltick(): ", gitStatusTick)
-- end
gitStatusTick = gitStatusTick + 1
lastGitStatusTick = gitStatusTick
lastGitStatusRunTime = os.time()
updateCurrentBufferCount()
end
function updateLastGitStatusStrIfPossible()
if currentGitStatus.isBranchDone and
currentGitStatus.isConflictDone and
currentGitStatus.isBehindAheadDone and
currentGitStatus.isStashDone and
currentGitStatus.isStagedModifiedUntrackedDone then
currentGitStatus.isBranchDone = false
currentGitStatus.isConflictDone = false
currentGitStatus.isBehindAheadDone = false
currentGitStatus.isStashDone = false
currentGitStatus.isStagedModifiedUntrackedDone = false
currentGitStatus.populatedCounter = 0
lastGitStatusStr = gitStatusToStr()
-- micro.InfoBar():Message("Called")
else
-- micro.InfoBar():Message("Called 2")
end
end
function info(buf)
if gitStatusTick - lastGitStatusTick < minGitStatusTick * currentBufCount then
gitStatusTick = gitStatusTick + 1
return lastGitStatusStr
end
if lastGitStatusRunTime ~= nil then
local lastRunTimeDiff = os.difftime(os.time(), lastGitStatusRunTime)
-- micro.InfoBar():Message("local lastRunTimeDiff = os.difftime(os.time(), lastGitStatusRunTime)")
if lastRunTimeDiff < config.GetGlobalOption('gitStatus.commandInterval') then
halftick()
return lastGitStatusStr
end
end
if gitStatusTick == 0 then
-- currentGitStatus.branch = branch()
startBranch()
startConflict()
startBehindOrAhead()
startStash()
startStagedModifiedUntracked()
-- lastGitStatusStr = gitStatusToStr()
-- micro.InfoBar():Message("gitStatusFirstRun")
currentGitStatus.populatedCounter = 3
fulltick(true)
return lastGitStatusStr
end
if currentGitStatus.populatedCounter == 0 then
startBranch()
startBehindOrAhead()
startStash()
fulltick(true)
elseif currentGitStatus.populatedCounter == 1 then
startConflict()
elseif currentGitStatus.populatedCounter == 2 then
startStagedModifiedUntracked()
fulltick(true)
end
if currentGitStatus.populatedCounter < 3 then
currentGitStatus.populatedCounter = currentGitStatus.populatedCounter + 1
else
updateLastGitStatusStrIfPossible()
end
return lastGitStatusStr
end
function init()
config.RegisterCommonOption('gitStatus', 'iconBranch', '')
config.RegisterCommonOption('gitStatus', 'iconNoGit', '?')
config.RegisterCommonOption('gitStatus', 'iconConflit', '')
config.RegisterCommonOption('gitStatus', 'iconConflict', '')
config.RegisterCommonOption('gitStatus', 'iconBehind', '↓')
config.RegisterCommonOption('gitStatus', 'iconAhead', '↑')
config.RegisterCommonOption('gitStatus', 'iconStage', 'S')
config.RegisterCommonOption('gitStatus', 'iconModified', 'M')
config.RegisterCommonOption('gitStatus', 'iconUnstage', 'U')
config.RegisterCommonOption('gitStatus', 'iconUntracked', 'U')
config.RegisterCommonOption('gitStatus', 'iconBranchOK', '✓')
config.RegisterCommonOption('gitStatus', 'iconBranchNoOK', '✗')
config.RegisterCommonOption('gitStatus', 'commandInterval', 3)
micro.SetStatusInfoFn('gitStatus.info')
config.AddRuntimeFile('gitStatus', config.RTHelp, 'help/gitStatus.md')
end