Advertisement
slipers

Minecraft. OpenComputers. Cedit (/bin/hl.lua).

Sep 1st, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.58 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local keyboard = require("keyboard")
  4. local shell = require("shell")
  5. local term = require("term")
  6. local unicode = require("unicode")
  7. local hl = require("highlighter")
  8.  
  9. if not term.isAvailable() then
  10.   return false
  11. end
  12.  
  13. local function waitkey()
  14.   while true do
  15.     local event, address, char, code = event.pull("key_down")
  16.     if component.isPrimary(address) then
  17.       if code == keyboard.keys.q then
  18.         return "exit"
  19.       elseif code == keyboard.keys.space or code == keyboard.keys.pageDown then
  20.         return "page"
  21.       elseif code == keyboard.keys.enter or code == keyboard.keys.down then
  22.         return "line"
  23.       end
  24.     end
  25.   end
  26. end
  27.  
  28. ----------------------------------------------------------------------------
  29. hl.reload()
  30. local args
  31. args, options = shell.parse(...)
  32. term.setCursorBlink(false)
  33. if #args == 0 then
  34.   local w, h = component.gpu.getResolution()
  35.   local num = 0
  36.   repeat
  37.     local read = io.read("*L")
  38.     if read then
  39.       if string.find( read, "\n$") then
  40.         read = string.sub( read, 1, -2)
  41.       end
  42.       if string.len(read) > 0 then
  43.         num = num + hl.line( read, options.w)
  44.       else
  45.         num = num + 1
  46.         term.write("\n")
  47.       end      
  48.       if options.m and num >= h - 1 then
  49.         term.write(":")
  50.         term.setCursorBlink(true)      
  51.         local k = waitkey()
  52.         term.clearLine()
  53.         term.setCursorBlink(false)
  54.         if k == "exit" then
  55.           break
  56.         elseif k == "page" then
  57.           num = 0
  58.         elseif k == "line" then
  59.           num = num - 1
  60.         end
  61.       end
  62.     end
  63.   until not read
  64. else
  65.   for i = 1, #args do
  66.     local file, reason = io.open(shell.resolve(args[i]))
  67.     if not file then
  68.       io.stderr:write(reason)
  69.       return
  70.     end
  71.     local w, h = component.gpu.getResolution()
  72.     local num = 0    
  73.     repeat
  74.       local line = file:read("*L")
  75.       if line then
  76.         if string.find( line, "\n$") then
  77.           line = string.sub( line, 1, -2)
  78.         end
  79.         if string.len(line) > 0 then
  80.           num = num + hl.line( line, options.w)
  81.         else
  82.           num = num + 1
  83.           term.write("\n")
  84.         end
  85.       end
  86.       if options.m and num >= h - 1 then
  87.         term.write(":")
  88.         term.setCursorBlink(true)
  89.         local k = waitkey()
  90.         term.clearLine()
  91.         term.setCursorBlink(false)
  92.         if k == "exit" then
  93.           break
  94.         elseif k == "page" then
  95.           num = 0
  96.         elseif k == "line" then
  97.           num = num - 1
  98.         end
  99.       end
  100.     until not line
  101.     file:close()
  102.   end
  103. end
  104. term.setCursorBlink(true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement