Advertisement
Tatantyler

HexEdit

May 11th, 2013
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.58 KB | None | 0 0
  1. local guideBar = "|Pos.|00|01|02|03|04|05|06|07|ASCIIRep|"
  2. local sepBar =   "|----|--|--|--|--|--|--|--|--|--------|"
  3. local horizBar = string.rep("-", 39)
  4. local sX, sY = term.getSize()
  5.  
  6. local args = {...}
  7.  
  8. if not term.isColor() then
  9.     print("Sorry, but you must an Advanced Computer is required to use this program.")
  10.     return
  11. end
  12.  
  13. if #args <= 0 then
  14.     print("USAGE: "..fs.getName(shell.getRunningProgram()).." [file]")
  15.     return
  16. end
  17.  
  18. local function writeScreen(bytes, offsetLines, fileName)
  19.     local byteLines = {}
  20.    
  21.     local function writeLine(bLine, offset, y)
  22.         term.setCursorPos(1, y)
  23.         local line = "|"
  24.         local strOffset = string.format("%X", offset)
  25.         for i=#strOffset, 3 do
  26.             strOffset = "0"..strOffset
  27.         end
  28.         line = line..strOffset.."|"
  29.         local asciiRep = ""
  30.         for i=1, 8 do
  31.             if bLine[i] ~= nil then
  32.                 local t = string.format("%X", bLine[i])
  33.                 if #t == 1 then
  34.                     t = "0"..t
  35.                 end
  36.                 line = line..t.."|"
  37.                 if bLine[i] >= 0x20 and bLine[i] <= 0x7E then
  38.                     asciiRep = asciiRep..string.char(bLine[i])
  39.                 else
  40.                     asciiRep = asciiRep.."."
  41.                 end
  42.             else
  43.                 line = line.."  ".."|"
  44.                 asciiRep = asciiRep.." "
  45.             end
  46.         end
  47.         line = line..asciiRep.."|"
  48.         term.clearLine()
  49.         term.write(line)
  50.     end
  51.    
  52.     for i=1, math.ceil(#bytes / 8) do
  53.         byteLines[i] = { --[[ ["offset"] = (i-1)*8 ]] }
  54.         for j=1, 8 do
  55.             byteLines[i][j] = bytes[ ((i-1)*8)+j ]
  56.         end
  57.     end
  58.    
  59.     term.clear()
  60.     term.setCursorPos(1,1)
  61.  
  62.     term.write("Current File: \""..fileName.."\" Size: "..#bytes.." bytes") -- Better to cut off the filename rather than completely wrecking the UI
  63.     print(guideBar)
  64.     print(sepBar)
  65.     for i=4, sY do
  66.         if byteLines[(i-3)+offsetLines] then
  67.             writeLine( byteLines[(i-3)+offsetLines], ((i-4)+offsetLines)*8, i )
  68.         else
  69.             writeLine( {}, ((i-4)+offsetLines)*8, i )
  70.         end
  71.     end
  72.     term.setCursorPos(41, 2)
  73.     term.write("E to exit")
  74.     term.setCursorPos(41, 3)
  75.     term.write("(and save)")
  76.     term.setCursorPos(41, 5)
  77.     term.write("Q to quit")
  78.     term.setCursorPos(41, 6)
  79.     term.write("(no save)")
  80.     term.setCursorPos(41, 8)
  81.     term.write("S to save")
  82.     term.setCursorPos(41, 9)
  83.     term.write("(no quit)")
  84. end
  85.  
  86. local handle = fs.open(args[1], "rb")
  87. local bytes = {}
  88. while true do
  89.     local byte = handle.read()
  90.     if byte then
  91.         table.insert(bytes, byte)
  92.     else
  93.         break
  94.     end
  95.     term.setCursorPos(1, 1)
  96.     term.clear()
  97.     print("Loading file.")
  98.     print(#bytes.." out of approximately "..fs.getSize(args[1]).." bytes loaded.")
  99.     print(math.ceil((#bytes / fs.getSize(args[1]))*100).."% complete.")
  100.     os.queueEvent("")
  101.     os.pullEvent()
  102. end
  103. handle.close()
  104.  
  105. local function save(b)
  106.     local handle = fs.open(args[1], "wb")
  107.     for i=1, #bytes do
  108.         term.setCursorPos(1, 1)
  109.         term.clearLine()
  110.         term.write("Writing: "..i.." of "..#b.." ("..math.ceil((i/#b)*100).."%)")
  111.         handle.write(b[i])
  112.         os.queueEvent("")
  113.         os.pullEvent()
  114.     end
  115.     handle.close()
  116. end
  117.  
  118. local lineOffset = 0
  119. while true do
  120.     --local byteOffset = lineOffset * 8
  121.     writeScreen(bytes, lineOffset, args[1])
  122.     local event, a1, x, y = os.pullEvent()
  123.     if event == "char" then
  124.         if a1 == "e" or a1 == "E" then
  125.             save(bytes)
  126.             break
  127.         elseif a1 == "q" or a1 == "Q" then
  128.             break
  129.         elseif a1 == "s" or a1 == "S" then
  130.             save(bytes)
  131.         end
  132.     elseif event == "mouse_click" then
  133.         if (not (y >= 1 and y <= 3)) and (x >6 and x < 39) then
  134.             term.setCursorPos(1,1)
  135.             term.write("Current File: \""..args[1].."\" Size: "..#bytes.." bytes !EDITING!")
  136.             -- Get clicked cell:
  137.             if x > 30 then
  138.                 local cell = x - 30
  139.                 term.setCursorPos(((cell-1)*3)+7, y)
  140.                 term.setBackgroundColor(colors.blue)
  141.                 term.write("  ")
  142.                 term.setCursorPos(x, y)
  143.                 term.setBackgroundColor(colors.red)
  144.                 term.write(" ")
  145.                 term.setBackgroundColor(colors.black)
  146.                 term.setCursorPos(x, y)
  147.                 term.setCursorBlink(true)
  148.                 local b = 0
  149.                 while true do
  150.                     local event, c = os.pullEvent()
  151.                     if event == "char" then
  152.                         b = string.byte(c)
  153.                         break
  154.                     elseif event == "key" then
  155.                         if c == keys.backspace or c == keys.delete then
  156.                             b = nil
  157.                             break
  158.                         end
  159.                     end
  160.                 end
  161.                 bytes[ (lineOffset+(y-4))*8+cell ] = b
  162.                 term.setCursorBlink(false)
  163.             else--[[if x > 6 and x < 39 then]]
  164.                 local cell = math.ceil((x-5)/3)
  165.                 term.setCursorPos(((cell-1)*3)+7, y)
  166.                 term.setBackgroundColor(colors.red)
  167.                 term.write("  ")
  168.                 term.setCursorPos(30+cell, y)
  169.                 term.setBackgroundColor(colors.blue)
  170.                 term.write(" ")
  171.                 term.setBackgroundColor(colors.black)
  172.                 term.setCursorPos(((cell-1)*3)+7, y)
  173.                 term.setCursorBlink(true)
  174.                 local st = ""
  175.                 while true do
  176.                     local event, c = os.pullEvent()
  177.                     if event == "char" and tonumber(c, 16) then
  178.                         st = st..c
  179.                     elseif event == "key" and (c == keys.backspace or c == keys.delete) then
  180.                         if #st == 0 then
  181.                             break
  182.                         else
  183.                             st = string.sub(st, 1, #st-1)
  184.                         end
  185.                     end
  186.                     term.setCursorPos(((cell-1)*3)+7, y)
  187.                     term.write(st)
  188.                     if #st >= 2 then
  189.                         break
  190.                     end
  191.                 end
  192.                 bytes[ (lineOffset+(y-4))*8+cell ] = tonumber(st, 16)
  193.                 term.setCursorBlink(false)
  194.             end
  195.             term.setCursorPos(1,1)
  196.             term.write("Current File: \""..args[1].."\" Size: "..#bytes.." bytes")
  197.         end
  198.         -- > 30, < 39
  199.     elseif event == "mouse_scroll" then
  200.         lineOffset = lineOffset+a1
  201.         if lineOffset < 0 then
  202.             lineOffset = 0
  203.         end
  204.     end
  205. end
  206.  
  207. --[[
  208. term.setCursorPos(1,1)
  209. term.write("Current File: \""..args[1].."\" Size: "..#bytes.." bytes !WRITING!")
  210.  
  211. local handle = fs.open(args[1], "wb")
  212. for i=1, #bytes do
  213.     handle.write(bytes[i])
  214.     os.queueEvent("")
  215.     os.pullEvent()
  216. end
  217. handle.close()
  218. ]]
  219.  
  220. term.clear()
  221. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement