ScoutMaester

Git Test

Sep 16th, 2023
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.68 KB | None | 0 0
  1. --===GIT===
  2. --=========
  3. --Developed by LNETeam from LNET Technologies
  4.  
  5. tArgs = {...}
  6.  
  7. if not http then
  8. print( "Git requires http API" )
  9. print( "Set enableAPI_http to 1 in mod_ComputerCraft.cfg" )
  10. return
  11. end
  12.  
  13. if #tArgs == 0 then
  14. print('Usage: git [get/view] [git owner] [repository] [branch] [path] {file-name}')
  15. return false
  16. end
  17.  
  18. option = tArgs[1]
  19. author = tArgs[2]
  20. proj = tArgs[3]
  21. branch = tArgs[4]
  22. paths = tArgs[5]
  23. saveName = tArgs[6]
  24.  
  25. function edit(path)
  26.  
  27. tArgs[1] = path
  28.  
  29. -- Error checking
  30. local sPath = shell.resolve( tArgs[1] )
  31.  
  32. local x,y = 1,1
  33. local w,h = term.getSize()
  34. local scrollX, scrollY = 0,0
  35.  
  36. local tLines = {}
  37. local bRunning = true
  38.  
  39. -- Colours
  40. local highlightColour, keywordColour, commentColour, textColour, bgColour
  41. if term.isColour() then
  42. bgColour = colours.black
  43. textColour = colours.white
  44. highlightColour = colours.yellow
  45. keywordColour = colours.yellow
  46. commentColour = colours.lime
  47. stringColour = colours.red
  48. else
  49. bgColour = colours.black
  50. textColour = colours.white
  51. highlightColour = colours.white
  52. keywordColour = colours.white
  53. commentColour = colours.white
  54. stringColour = colours.white
  55. end
  56.  
  57. -- Menus
  58. local bMenu = false
  59. local nMenuItem = 1
  60. local tMenuItems = {"Exit", "Print"}
  61. local sStatus = "Press Ctrl to access menu"
  62.  
  63. local function load(_sPath)
  64. tLines = {}
  65. if fs.exists( _sPath ) then
  66. local file = io.open( _sPath, "r" )
  67. local sLine = file:read()
  68. while sLine do
  69. table.insert( tLines, sLine )
  70. sLine = file:read()
  71. end
  72. file:close()
  73. end
  74.  
  75. if #tLines == 0 then
  76. table.insert( tLines, "" )
  77. end
  78. end
  79.  
  80. local function save( _sPath )
  81. -- Create intervening folder
  82. local sDir = sPath:sub(1, sPath:len() - fs.getName(sPath):len() )
  83. if not fs.exists( sDir ) then
  84. fs.makeDir( sDir )
  85. end
  86.  
  87. -- Save
  88. local file = nil
  89. local function innerSave()
  90. file = fs.open( _sPath, "w" )
  91. if file then
  92. for n, sLine in ipairs( tLines ) do
  93. file.write( sLine .. "\n" )
  94. end
  95. else
  96. error( "Failed to open ".._sPath )
  97. end
  98. end
  99.  
  100. local ok = pcall( innerSave )
  101. if file then
  102. file.close()
  103. end
  104. return ok
  105. end
  106.  
  107. local tKeywords = {
  108. ["and"] = true,
  109. ["break"] = true,
  110. ["do"] = true,
  111. ["else"] = true,
  112. ["elseif"] = true,
  113. ["end"] = true,
  114. ["false"] = true,
  115. ["for"] = true,
  116. ["function"] = true,
  117. ["if"] = true,
  118. ["in"] = true,
  119. ["local"] = true,
  120. ["nil"] = true,
  121. ["not"] = true,
  122. ["or"] = true,
  123. ["repeat"] = true,
  124. ["return"] = true,
  125. ["then"] = true,
  126. ["true"] = true,
  127. ["until"]= true,
  128. ["while"] = true,
  129. }
  130.  
  131. local function tryWrite( sLine, regex, colour )
  132. local match = string.match( sLine, regex )
  133. if match then
  134. if type(colour) == "number" then
  135. term.setTextColour( colour )
  136. else
  137. term.setTextColour( colour(match) )
  138. end
  139. term.write( match )
  140. term.setTextColour( textColour )
  141. return string.sub( sLine, string.len(match) + 1 )
  142. end
  143. return nil
  144. end
  145.  
  146. local function writeHighlighted( sLine )
  147. while string.len(sLine) > 0 do
  148. sLine =
  149. tryWrite( sLine, "^%-%-%[%[.-%]%]", commentColour ) or
  150. tryWrite( sLine, "^%-%-.*", commentColour ) or
  151. tryWrite( sLine, "^\".-[^\\]\"", stringColour ) or
  152. tryWrite( sLine, "^\'.-[^\\]\'", stringColour ) or
  153. tryWrite( sLine, "^%[%[.-%]%]", stringColour ) or
  154. tryWrite( sLine, "^[%w_]+", function( match )
  155. if tKeywords[ match ] then
  156. return keywordColour
  157. end
  158. return textColour
  159. end ) or
  160. tryWrite( sLine, "^[^%w_]", textColour )
  161. end
  162. end
  163.  
  164. local function redrawText()
  165. for y=1,h-1 do
  166. term.setCursorPos( 1 - scrollX, y )
  167. term.clearLine()
  168.  
  169. local sLine = tLines[ y + scrollY ]
  170. if sLine ~= nil then
  171. writeHighlighted( sLine )
  172. end
  173. end
  174. term.setCursorPos( x - scrollX, y - scrollY )
  175. end
  176.  
  177. local function redrawLine(_nY)
  178. local sLine = tLines[_nY]
  179. term.setCursorPos( 1 - scrollX, _nY - scrollY )
  180. term.clearLine()
  181. writeHighlighted( sLine )
  182. term.setCursorPos( x - scrollX, _nY - scrollY )
  183. end
  184.  
  185. local function setLeftStatus()
  186. end
  187.  
  188. local function redrawMenu()
  189. term.setCursorPos( 1, h )
  190. term.clearLine()
  191.  
  192. local sLeft, sRight
  193. local nLeftColour, nLeftHighlight1, nLeftHighlight2
  194. if bMenu then
  195. local sMenu = ""
  196. for n,sItem in ipairs( tMenuItems ) do
  197. if n == nMenuItem then
  198. nLeftHighlight1 = sMenu:len() + 1
  199. nLeftHighlight2 = sMenu:len() + sItem:len() + 2
  200. end
  201. sMenu = sMenu.." "..sItem.." "
  202. end
  203. sLeft = sMenu
  204. nLeftColour = textColour
  205. else
  206. sLeft = sStatus
  207. nLeftColour = highlightColour
  208. end
  209.  
  210. -- Left goes last so that it can overwrite the line numbers.
  211. sRight = "Ln "..y
  212. term.setTextColour( highlightColour )
  213. term.setCursorPos( w-sRight:len() + 1, h )
  214. term.write(sRight)
  215.  
  216. sRight = tostring(y)
  217. term.setTextColour( textColour )
  218. term.setCursorPos( w-sRight:len() + 1, h )
  219. term.write(sRight)
  220.  
  221. if sLeft then
  222. term.setCursorPos( 1, h )
  223. term.setTextColour( nLeftColour )
  224. term.write(sLeft)
  225. if nLeftHighlight1 then
  226. term.setTextColour( highlightColour )
  227. term.setCursorPos( nLeftHighlight1, h )
  228. term.write( "[" )
  229. term.setCursorPos( nLeftHighlight2, h )
  230. term.write( "]" )
  231. end
  232. term.setTextColour( textColour )
  233. end
  234.  
  235. -- Cursor highlights selection
  236. term.setCursorPos( x - scrollX, y - scrollY )
  237. end
  238.  
  239. local tMenuFuncs = {
  240. Save=function()
  241. if bReadOnly then
  242. sStatus = "Access denied"
  243. else
  244. local ok, err = save( sPath )
  245. if ok then
  246. sStatus="Saved to "..sPath
  247. else
  248. sStatus="Error saving to "..sPath
  249. end
  250. end
  251. redrawMenu()
  252. end,
  253. Print=function()
  254. local sPrinterSide = nil
  255. for n,sSide in ipairs(rs.getSides()) do
  256. if peripheral.isPresent(sSide) and peripheral.getType(sSide) == "printer" then
  257. sPrinterSide = sSide
  258. break
  259. end
  260. end
  261.  
  262. if not sPrinterSide then
  263. sStatus = "No printer attached"
  264. return
  265. end
  266.  
  267. local nPage = 0
  268. local sName = fs.getName( sPath )
  269. local printer = peripheral.wrap(sPrinterSide)
  270. if printer.getInkLevel() < 1 then
  271. sStatus = "Printer out of ink"
  272. return
  273. elseif printer.getPaperLevel() < 1 then
  274. sStatus = "Printer out of paper"
  275. return
  276. end
  277.  
  278. local terminal = {
  279. getCursorPos = printer.getCursorPos,
  280. setCursorPos = printer.setCursorPos,
  281. getSize = printer.getPageSize,
  282. write = printer.write,
  283. }
  284. terminal.scroll = function()
  285. if nPage == 1 then
  286. printer.setPageTitle( sName.." (page "..nPage..")" )
  287. end
  288.  
  289. while not printer.newPage() do
  290. if printer.getInkLevel() < 1 then
  291. sStatus = "Printer out of ink, please refill"
  292. elseif printer.getPaperLevel() < 1 then
  293. sStatus = "Printer out of paper, please refill"
  294. else
  295. sStatus = "Printer output tray full, please empty"
  296. end
  297.  
  298. term.restore()
  299. redrawMenu()
  300. term.redirect( terminal )
  301.  
  302. local timer = os.startTimer(0.5)
  303. sleep(0.5)
  304. end
  305.  
  306. nPage = nPage + 1
  307. if nPage == 1 then
  308. printer.setPageTitle( sName )
  309. else
  310. printer.setPageTitle( sName.." (page "..nPage..")" )
  311. end
  312. end
  313.  
  314. bMenu = false
  315. term.redirect( terminal )
  316. local ok, error = pcall( function()
  317. term.scroll()
  318. for n, sLine in ipairs( tLines ) do
  319. print( sLine )
  320. end
  321. end )
  322. term.restore()
  323. if not ok then
  324. print( error )
  325. end
  326.  
  327. while not printer.endPage() do
  328. sStatus = "Printer output tray full, please empty"
  329. redrawMenu()
  330. sleep( 0.5 )
  331. end
  332. bMenu = true
  333.  
  334. if nPage > 1 then
  335. sStatus = "Printed "..nPage.." Pages"
  336. else
  337. sStatus = "Printed 1 Page"
  338. end
  339. redrawMenu()
  340. end,
  341. Exit=function()
  342. bRunning = false
  343. end
  344. }
  345.  
  346. local function doMenuItem( _n )
  347. tMenuFuncs[tMenuItems[_n]]()
  348. if bMenu then
  349. bMenu = false
  350. term.setCursorBlink( true )
  351. end
  352. redrawMenu()
  353. end
  354.  
  355. local function setCursor( x, y )
  356. local screenX = x - scrollX
  357. local screenY = y - scrollY
  358.  
  359. local bRedraw = false
  360. if screenX < 1 then
  361. scrollX = x - 1
  362. screenX = 1
  363. bRedraw = true
  364. elseif screenX > w then
  365. scrollX = x - w
  366. screenX = w
  367. bRedraw = true
  368. end
  369.  
  370. if screenY < 1 then
  371. scrollY = y - 1
  372. screenY = 1
  373. bRedraw = true
  374. elseif screenY > h-1 then
  375. scrollY = y - (h-1)
  376. screenY = h-1
  377. bRedraw = true
  378. end
  379.  
  380. if bRedraw then
  381. redrawText()
  382. end
  383. term.setCursorPos( screenX, screenY )
  384.  
  385. -- Statusbar now pertains to menu, it would probably be safe to redraw the menu on every key event.
  386. redrawMenu()
  387. end
  388.  
  389. -- Actual program functionality begins
  390. load(sPath)
  391.  
  392. term.setBackgroundColour( bgColour )
  393. term.clear()
  394. term.setCursorPos(x,y)
  395. term.setCursorBlink( true )
  396.  
  397. redrawText()
  398. redrawMenu()
  399.  
  400. -- Handle input
  401. while bRunning do
  402. local sEvent, param, param2, param3 = os.pullEvent()
  403. if sEvent == "key" then
  404. if param == keys.up then
  405. -- Up
  406. if not bMenu then
  407. if y > 1 then
  408. -- Move cursor up
  409. y = y - 1
  410. x = math.min( x, string.len( tLines[y] ) + 1 )
  411. setCursor( x, y )
  412. end
  413. end
  414. elseif param == keys.down then
  415. -- Down
  416. if not bMenu then
  417. -- Move cursor down
  418. if y < #tLines then
  419. y = y + 1
  420. x = math.min( x, string.len( tLines[y] ) + 1 )
  421. setCursor( x, y )
  422. end
  423. end
  424. elseif param == keys.tab then
  425. -- Tab
  426. if not bMenu then
  427. local sLine = tLines[y]
  428.  
  429. -- Indent line
  430. -- IN CASE OF INSERT TAB IN PLACE:
  431. -- tLines[y] = string.sub(sLine,1,x-1) .. " " .. string.sub(sLine,x)
  432. tLines[y]=" "..tLines[y]
  433. x = x + 2
  434. setCursor( x, y )
  435. redrawLine(y)
  436. end
  437. elseif param == keys.pageUp then
  438. -- Page Up
  439. if not bMenu then
  440. -- Move up a page
  441. local sx,sy=term.getSize()
  442. y=y-sy-1
  443. if y<1 then y=1 end
  444. x = math.min( x, string.len( tLines[y] ) + 1 )
  445. setCursor( x, y )
  446. end
  447. elseif param == keys.pageDown then
  448. -- Page Down
  449. if not bMenu then
  450. -- Move down a page
  451. local sx,sy=term.getSize()
  452. if y<#tLines-sy-1 then
  453. y = y+sy-1
  454. else
  455. y = #tLines
  456. end
  457. x = math.min( x, string.len( tLines[y] ) + 1 )
  458. setCursor( x, y )
  459. end
  460. elseif param == keys.home then
  461. -- Home
  462. if not bMenu then
  463. -- Move cursor to the beginning
  464. x=1
  465. setCursor(x,y)
  466. end
  467. elseif param == keys["end"] then
  468. -- End
  469. if not bMenu then
  470. -- Move cursor to the end
  471. x = string.len( tLines[y] ) + 1
  472. setCursor(x,y)
  473. end
  474. elseif param == keys.left then
  475. -- Left
  476. if not bMenu then
  477. if x > 1 then
  478. -- Move cursor left
  479. x = x - 1
  480. elseif x==1 and y>1 then
  481. x = string.len( tLines[y-1] ) + 1
  482. y = y - 1
  483. end
  484. setCursor( x, y )
  485. else
  486. -- Move menu left
  487. nMenuItem = nMenuItem - 1
  488. if nMenuItem < 1 then
  489. nMenuItem = #tMenuItems
  490. end
  491. redrawMenu()
  492. end
  493. elseif param == keys.right then
  494. -- Right
  495. if not bMenu then
  496. if x < string.len( tLines[y] ) + 1 then
  497. -- Move cursor right
  498. x = x + 1
  499. elseif x==string.len( tLines[y] ) + 1 and y<#tLines then
  500. x = 1
  501. y = y + 1
  502. end
  503. setCursor( x, y )
  504. else
  505. -- Move menu right
  506. nMenuItem = nMenuItem + 1
  507. if nMenuItem > #tMenuItems then
  508. nMenuItem = 1
  509. end
  510. redrawMenu()
  511. end
  512. elseif param == keys.delete then
  513. -- Delete
  514. if not bMenu then
  515. if x < string.len( tLines[y] ) + 1 then
  516. local sLine = tLines[y]
  517. tLines[y] = string.sub(sLine,1,x-1) .. string.sub(sLine,x+1)
  518. redrawLine(y)
  519. elseif y<#tLines then
  520. tLines[y] = tLines[y] .. tLines[y+1]
  521. table.remove( tLines, y+1 )
  522. redrawText()
  523. redrawMenu()
  524. end
  525. end
  526. elseif param == keys.backspace then
  527. -- Backspace
  528. if not bMenu then
  529. if x > 1 then
  530. -- Remove character
  531. local sLine = tLines[y]
  532. tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x)
  533. redrawLine(y)
  534.  
  535. x = x - 1
  536. setCursor( x, y )
  537. elseif y > 1 then
  538. -- Remove newline
  539. local sPrevLen = string.len( tLines[y-1] )
  540. tLines[y-1] = tLines[y-1] .. tLines[y]
  541. table.remove( tLines, y )
  542. redrawText()
  543.  
  544. x = sPrevLen + 1
  545. y = y - 1
  546. setCursor( x, y )
  547. end
  548. end
  549. elseif param == keys.enter then
  550. -- Enter
  551. if not bMenu then
  552. -- Newline
  553. local sLine = tLines[y]
  554. local _,spaces=string.find(sLine,"^[ ]+")
  555. if not spaces then
  556. spaces=0
  557. end
  558. tLines[y] = string.sub(sLine,1,x-1)
  559. table.insert( tLines, y+1, string.rep(' ',spaces)..string.sub(sLine,x) )
  560. redrawText()
  561.  
  562. x = spaces+1
  563. y = y + 1
  564. setCursor( x, y )
  565. else
  566. -- Menu selection
  567. doMenuItem( nMenuItem )
  568. end
  569. elseif param == keys.leftCtrl or param == keys.rightCtrl then
  570. -- Menu toggle
  571. bMenu = not bMenu
  572. if bMenu then
  573. term.setCursorBlink( false )
  574. nMenuItem = 1
  575. else
  576. term.setCursorBlink( true )
  577. end
  578. redrawMenu()
  579. end
  580.  
  581. elseif sEvent == "char" then
  582. if not bMenu then
  583. -- Input text
  584. local sLine = tLines[y]
  585. tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
  586. redrawLine(y)
  587.  
  588. x = x + string.len( param )
  589. setCursor( x, y )
  590. else
  591. -- Select menu items
  592. for n,sMenuItem in ipairs( tMenuItems ) do
  593. if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then
  594. doMenuItem( n )
  595. break
  596. end
  597. end
  598. end
  599.  
  600. elseif sEvent == "mouse_click" then
  601. if not bMenu then
  602. if param == 1 then
  603. -- Left click
  604. local cx,cy = param2, param3
  605. if cy < h then
  606. y = math.min( math.max( scrollY + cy, 1 ), #tLines )
  607. x = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[y] ) + 1 )
  608. setCursor( x, y )
  609. end
  610. end
  611. end
  612.  
  613. elseif sEvent == "mouse_scroll" then
  614. if not bMenu then
  615. if param == -1 then
  616. -- Scroll up
  617. if scrollY > 0 then
  618. -- Move cursor up
  619. scrollY = scrollY - 1
  620. redrawText()
  621. end
  622.  
  623. elseif param == 1 then
  624. -- Scroll down
  625. local nMaxScroll = #tLines - (h-1)
  626. if scrollY < nMaxScroll then
  627. -- Move cursor down
  628. scrollY = scrollY + 1
  629. redrawText()
  630. end
  631.  
  632. end
  633. end
  634. end
  635. end
  636.  
  637. -- Cleanup
  638. term.clear()
  639. term.setCursorBlink( false )
  640. term.setCursorPos( 1, 1 )
  641. end
  642.  
  643. function requestObject(url,sN,mode)
  644. if not url then error('Incorrect statement!') end
  645. if not sN and mode == 'get' then error('Check mode!') end
  646. if mode == 'get' then
  647. write('Fetching: '..url..'... ')
  648. http.request(url)
  649. local requesting = true
  650. while requesting do
  651. local event, url, sourceText = os.pullEvent()
  652. if event == "http_success" then
  653. local respondedText = sourceText.readAll()
  654. temp = io.open(sN,'w')
  655. temp:write(respondedText)
  656. temp:close()
  657. print('Successfully saved!')
  658. requesting = false
  659. return true
  660. elseif event == "http_failure" then
  661. print("Fetch failed! Please check values or non-existent project!")
  662. requesting = false
  663. return false
  664. end
  665. end
  666. elseif mode == 'view' then
  667. write('Fetching: '..url..'... ')
  668. http.request(url)
  669. local requesting = true
  670. while requesting do
  671. local event, url, sourceText = os.pullEvent()
  672. if event == "http_success" then
  673. local respondedText = sourceText.readAll()
  674. print('Successfully fetched!\n')
  675. temp = io.open('temp','w')
  676. temp:write(respondedText)
  677. temp:close()
  678. edit('temp')
  679. fs.delete('temp')
  680. requesting = false
  681. return true
  682. elseif event == "http_failure" then
  683. print("Fetch failed!")
  684. requesting = false
  685. return false
  686. end
  687. end
  688. end
  689. end
  690.  
  691. function compileURL(auth,pro,bran,pat)
  692. baseURL = 'https://raw.github.com/'..auth..'/'..pro..'/'..bran..'/'..pat
  693. return baseURL
  694. end
  695.  
  696. if option == 'get' then
  697. if not option or not author or not proj or not branch or not paths or not saveName then print('Usage: git [get/view] [git owner] [repository] [branch] [path] {file-name}') return false end
  698. statusCode = requestObject(compileURL(author,proj,branch,paths),saveName,option)
  699. elseif option =='view' then
  700. if not option or not author or not proj or not branch or not paths then print('Usage: git [get/view] [git owner] [repository] [branch] [path] {file-name}') return false end
  701. statusCode = requestObject(compileURL(author,proj,branch,paths),nil,option)
  702. end
Add Comment
Please, Sign In to add comment