View difference between Paste ID: GMcQ19qZ and YUwFUvxj
SHOW: | | - or go back to the newest paste.
1
2
local frame = 1;
3
4
local tArgs = { ... }
5
if #tArgs == 0 then
6
	print( "Usage: powerpoint <folder path>" )
7
	return
8
end
9
10
local sPath = shell.resolve( tArgs[1] )
11
local bReadOnly = fs.isReadOnly( sPath )
12
if fs.exists( sPath ) and fs.isDir( sPath ) == false then
13
	print( "Cannot edit a frame" )
14
	return
15
elseif fs.exists( sPath ) == false then
16
	fs.makeDir( sPath )
17
end
18
19
local w,h = term.getSize()
20
local x,y = 1,1
21
local scrollX, scrollY = 0,0
22
23
local bMenu = false
24
local nMenuItem = 1
25
26
local sStatus = ""
27
local tLines = {}
28
29
local function load()
30
	tLines = {}
31
	if fs.exists( sPath.. "/Frame" ..frame ) then
32-
		local file = io.open( sPath, "r" )
32+
		local file = io.open( sPath.. "/Frame" ..frame, "r" )
33
		local sLine = file:read()
34
		while sLine do
35
			table.insert( tLines, sLine )
36
			sLine = file:read()
37
		end
38
		file:close()
39
	else
40
		table.insert( tLines, "" )
41
	end
42
end
43
44
local function save()
45
	local file = io.open( sPath.. "/Frame" ..frame, "w" )
46
	if file then
47
		for n, sLine in ipairs( tLines ) do
48
			file:write( sLine .. "\n" )
49
		end
50
		file:close()
51
		return true
52
	end
53
	return false
54
end
55
56
local function redrawText()
57
	for y=1,h-1 do
58
		term.setCursorPos( 1 - scrollX, y )
59
		term.clearLine()
60
61
		local sLine = tLines[ y + scrollY ]
62
		if sLine ~= nil then
63
			term.write( sLine )
64
		end
65
	end
66
	term.setCursorPos( x - scrollX, y - scrollY )
67
end
68
69
local function redrawLine()
70
	local sLine = tLines[y]
71
	term.setCursorPos( 1 - scrollX, y - scrollY )
72
	term.clearLine()
73
	term.write( sLine )
74
	term.setCursorPos( x - scrollX, y - scrollY )
75
end
76
77
local tMenuItems = { "Save", "Exit", "Next", "Back" }
78
79
local function redrawMenu()
80
    term.setCursorPos( 1, h )
81
	term.clearLine()
82
83
	if not bMenu then
84
		term.write( sStatus )
85
	else
86
		for n,sItem in ipairs( tMenuItems ) do
87
			if n == nMenuItem then
88
				term.write( "["..sItem.."]" )
89
			else
90
				term.write( " "..sItem.." " )
91
			end
92
		end
93
	end
94
	
95
    term.setCursorPos( x - scrollX, y - scrollY )
96
end
97
98
local function setStatus( _sText )
99
	sStatus = _sText
100
	redrawMenu()
101
end
102
103
local function setCursor( x, y )
104
	local screenX = x - scrollX
105
	local screenY = y - scrollY
106
	
107
	local bRedraw = false
108
	if screenX < 1 then
109
		scrollX = x - 1
110
		screenX = 1
111
		bRedraw = true
112
	elseif screenX > w then
113
		scrollX = x - w
114
		screenX = w
115
		bRedraw = true
116
	end
117
	
118
	if screenY < 1 then
119
		scrollY = y - 1
120
		screenY = 1
121
		bRedraw = true
122
	elseif screenY > h-1 then
123
		scrollY = y - (h-1)
124
		screenY = h-1
125
		bRedraw = true
126
	end
127
	
128
	if bRedraw then
129
		redrawText()
130
	end
131
	term.setCursorPos( screenX, screenY )
132
end
133
134
load()
135
136
term.clear()
137
term.setCursorPos(x,y)
138
term.setCursorBlink( true )
139
140
redrawText()
141
setStatus( "Press Ctrl to access menu (Frame " ..frame.. ")" )
142
143
local bRunning = true
144
local tMenuFunctions = {
145
	["Exit"] = function()
146
		bRunning = false
147
	end,
148
	["Save"] = function()
149
		if save() then
150
			setStatus( "Saved to "..sPath.." (Frame " ..frame..")" )
151
		else
152
			setStatus( "Access denied" )
153
		end
154
	end,
155
	["Next"] = function()
156
		frame = frame +1
157
		load()
158
		setStatus( "Press Ctrl to access menu (Frame " ..frame.. ")" )
159
	end,
160
	["Back"] = function()
161
		if not frame == 1 then
162
			frame = frame-1
163
			load()
164
			setStatus( "Press Ctrl to access menu (Frame " ..frame.. ")" )
165
		end
166
	end,
167
}
168
169
local function doMenuItem( _n )
170
	local fnMenu = tMenuFunctions[ tMenuItems[_n] ]
171
	if fnMenu then
172
		fnMenu()
173
	end
174
175
	bMenu = false
176
	term.setCursorBlink( true )
177
	redrawMenu()
178
end
179
180
while bRunning do
181
  local sEvent, param = os.pullEvent()
182
  if sEvent == "key" then
183
    if param == 200 then
184
		-- Up
185
		if not bMenu then
186
			-- Move cursor up
187
			if y > 1 then
188
				y = y - 1
189
				x = math.min( x, string.len( tLines[y] ) + 1 )
190
				setCursor( x, y )
191
			end
192
		end
193
		
194
	elseif param == 208 then
195
		-- Down
196
		if not bMenu then
197
			-- Move cursor down
198
			if y < #tLines then
199
				y = y + 1
200
				x = math.min( x, string.len( tLines[y] ) + 1 )
201
				setCursor( x, y )
202
			end
203
		end
204
	
205
	elseif param == 203 then
206
		-- Left
207
		if not bMenu then
208
			-- Move cursor left
209
			if x > 1 then
210
				x = x - 1
211
				setCursor( x, y )
212
			end
213
		else
214
			-- Move menu left
215
			nMenuItem = nMenuItem - 1
216
			if nMenuItem < 1 then
217
				nMenuItem = #tMenuItems
218
			end
219
			redrawMenu()
220
		end
221
			
222
		
223
	elseif param == 205 then
224
		-- Right
225
		if not bMenu then
226
			-- Move cursor right
227
   		 	if x < string.len( tLines[y] ) + 1 then
228
				x = x + 1
229
				setCursor( x, y )
230
		  	end
231
		else
232
			-- Move menu right
233
			nMenuItem = nMenuItem + 1
234
		  	if nMenuItem > #tMenuItems then
235
				nMenuItem = 1
236
		  	end
237
		  	redrawMenu()
238
		end
239
		
240
	elseif param == 14 then
241
		-- Backspace
242
		if not bMenu then
243
   		 	if x > 1 then
244
				-- Remove character
245
				local sLine = tLines[y]
246
				tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x)
247
				redrawLine()
248
		
249
				x = x - 1
250
				setCursor( x, y )
251
			
252
			elseif y > 1 then
253
				-- Remove newline
254
				local sPrevLen = string.len( tLines[y-1] )
255
				tLines[y-1] = tLines[y-1] .. tLines[y]
256
				table.remove( tLines, y )
257
				redrawText()
258
			
259
				x = sPrevLen + 1
260
				y = y - 1
261
				setCursor( x, y )
262
		  	end
263
		end
264
		
265
	elseif param == 28 then
266
		-- Enter
267
		if not bMenu then
268
			-- Newline
269
			local sLine = tLines[y]
270
			tLines[y] = string.sub(sLine,1,x-1)
271
			table.insert( tLines, y+1, string.sub(sLine,x) )
272
			redrawText()
273
		
274
			x = 1
275
			y = y + 1
276
			setCursor( x, y )		
277
			
278
		else
279
			-- Menu selection
280
			doMenuItem( nMenuItem )
281
		end
282
		
283
	elseif param == 29 then
284
		-- Menu toggle
285
		bMenu = not bMenu
286
		if bMenu then
287
			term.setCursorBlink( false )
288
			nMenuItem = 1
289
		else
290
			term.setCursorBlink( true )
291
		end
292
		redrawMenu()
293
	
294
	end
295
		
296
  elseif sEvent == "char" then
297
	if not bMenu then
298
    	-- Input text
299
		local sLine = tLines[y]
300
		tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
301
		redrawLine()
302
	
303
		x = x + string.len( param )
304
		setCursor( x, y )
305
	
306
	else
307
		-- Select menu items
308
		for n,sMenuItem in ipairs( tMenuItems ) do
309
			if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then
310
				doMenuItem( n )
311
				break
312
			end
313
		end
314
	end
315
  end
316
end
317
318
term.clear()
319
term.setCursorBlink( false )
320
term.setCursorPos( 1, 1 )