-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDiffScreen.lua
More file actions
222 lines (197 loc) · 6.47 KB
/
DiffScreen.lua
File metadata and controls
222 lines (197 loc) · 6.47 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
-- DiffScreen.lua
DiffScreen = class(AppleScreen)
function DiffScreen:init(filename,localConts,gitConts,prevScreen)
self.filename = filename
local schema = {
title = filename,
backButton = {
text = "Files",
callback = function() screen = prevScreen end,
},
elems = {}
}
AppleScreen.init(self,schema)
-- calculate the diffs
if not localConts then localConts = "" end
if not gitConts then gitConts = "" end
local localFile = DiffScreen.splitLines(localConts)
local gitFile = DiffScreen.splitLines(gitConts)
local diff = Differ.diff(localFile,gitFile)
--for idx,elem in ipairs(diff) do
-- print(idx,elem)
--end
-- topY = 10 for example means that we start showing only from
-- 10 on down
self.tY = 0
self.textBlocks = {}
self.lineBlocks = {} -- line numbers
local lastGitIdx=0
local row = 0
for localIdx,gitIdx in ipairs(diff) do
row = row + 1
if gitIdx == 0 then
-- this line has been added
table.insert(self.textBlocks,{text=localFile[localIdx],col=color(0,255,0),
type="Added"})
table.insert(self.lineBlocks,{text=localIdx..".",col=color(255,255,255)})
else
if gitIdx - lastGitIdx > 1 then
-- these lines have been removed
for idx = lastGitIdx + 1,gitIdx-1 do
table.insert(self.textBlocks,{text=gitFile[idx],col=color(255,0,0),
type="Removed"})
table.insert(self.lineBlocks,{text="",col=color(255,255,255)})
end
lastGitIdx = gitIdx
end
-- this line stays the same
table.insert(self.textBlocks,{text=localFile[localIdx],col=color(255,255,255)})
table.insert(self.lineBlocks,{text=localIdx..".",col=color(255,255,255)})
lastGitIdx = gitIdx
end
end
for i = lastGitIdx + 1,#gitFile do
table.insert(self.textBlocks,{text=gitFile[i],col=color(255,0,0),type="Removed"})
table.insert(self.lineBlocks,{text="",col=color(255,255,255)})
end
-- create the next and prev buttons
self.next = TextButton("",WIDTH-33,HEIGHT-42,25,35,{type="arrowRight",
topColor=color(127,127,127),
bottomColor=color(127,127,127),
pressedTopColor=color(255,255,255),
pressedBottomColor=color(255,255,255)
})
self.next.onEnded = function(b,t)
TextButton.onEnded(b,t)
self:nextDiff()
end
self:add(self.next)
self.prev = TextButton("",WIDTH-70,HEIGHT-42,25,35,{type="arrowLeft",
topColor=color(127,127,127),
bottomColor=color(127,127,127),
pressedTopColor=color(255,255,255),
pressedBottomColor=color(255,255,255)
})
self.prev.onEnded = function(b,t)
TextButton.onEnded(b,t)
self:prevDiff()
end
self:add(self.prev)
end
function DiffScreen.splitLines(str)
local idx = str:find("\n")
local ans = {}
while idx do
local prefix = str:sub(1,idx-1)
if prefix:len() == 0 then prefix = " " end
table.insert(ans,prefix)
str = str:sub(idx+1)
idx = str:find("\n")
end
if str:len() == 0 then str = " " end
table.insert(ans,str)
return ans
end
function DiffScreen:nextDiff()
local si = self.pointedLine
if not si then si = 0 end
-- first find a non change, then find a change
local nonFound = false
for row = si+1,#self.textBlocks do
if self.textBlocks[row].type == "Added" or self.textBlocks[row].type == "Removed" then
if nonFound then
self:setPointedLine(row)
break
end
else
nonFound = true
end
end
end
function DiffScreen:prevDiff()
local si = self.pointedLine
if not si then si = 0 end
-- first find a non change, then find a change
local nonFound = false
for row = si-1,1,-1 do
if self.textBlocks[row].type == "Added" or self.textBlocks[row].type == "Removed" then
if nonFound then
-- now go back until the start of the diff
local f = false -- if we don't find the start of the diff, then
-- set to line 1
for r = row,1,-1 do
if self.textBlocks[r].type ~= "Added" and
self.textBlocks[r].type ~= "Removed" then
self:setPointedLine(r+1)
f = true
break
end
end
if not f then self:setPointedLine(1) end
break
end
else
nonFound = true
end
end
end
function DiffScreen:setPointedLine(newPointer)
self.pointedLine = newPointer
-- move the screen around
pushStyle()
self:setTextProperties()
local startH = HEIGHT-60
local currentH = startH + self.tY
for idx,block in ipairs(self.textBlocks) do
local w,h = textSize(block.text)
currentH = currentH - h
if idx == newPointer then
if currentH < 0 or currentH > startH - 30 then
self.tY = startH + self.tY - currentH - 30
end
break
end
end
popStyle()
end
function DiffScreen:draw()
AppleScreen.draw(self)
pushStyle()
-- draw the background
noStroke()
fill(0,0,0)
rect(0,0,WIDTH,HEIGHT-50)
-- draw the diffs
self:setTextProperties()
local startH = HEIGHT-60
local currentH = startH + self.tY
for idx,block in ipairs(self.textBlocks) do
local w,h = textSize(block.text)
currentH = currentH - h
if currentH <= startH - h and currentH > -h then
fill(block.col)
text(block.text,70,currentH)
fill(self.lineBlocks[idx].col)
local linText = self.lineBlocks[idx].text
if idx == self.pointedLine then
fill(0, 13, 255, 255)
linText = linText .. "->"
end
text(linText,5,currentH)
end
end
popStyle()
self.next:draw()
end
function DiffScreen:setTextProperties()
font("ArialMT")
textMode(CORNER)
fontSize(15)
end
function DiffScreen:touched(touch)
AppleScreen.touched(self,touch)
if touch.state == MOVING then
self.tY = self.tY + touch.deltaY
self.tY = math.max(self.tY,0)
end
end