Advertisement
Guest User

Untitled

a guest
Mar 29th, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. local os=require("os")
  2. local component=require("component")
  3. local shell=require("shell")
  4. local filesystem=require("filesystem")
  5. local math=require("math")
  6. local term=require("term")
  7.  
  8. local tape=component.getPrimary("tape_drive")
  9. local args,flags=shell.parse(...)
  10.  
  11. --functions--
  12.  
  13. local function round(num,idp)
  14. local mult=10^(idp or 0)
  15. return math.floor(num*mult+.5)/mult
  16. end
  17.  
  18. local function lerror(text)
  19. error(text)
  20. end
  21.  
  22. local function writeToTape(filepath)
  23. local filesize=filesystem.size(filepath)
  24. local file=io.open(filepath,"fb")
  25. local writed=0
  26. local lastpercent=""
  27. local percent=""
  28. local text=nil
  29. local x,y=term.getCursor()
  30. local offset=#(filepath..": ")+1
  31.  
  32. term.write(filepath..": ")
  33.  
  34. text=file:read()
  35.  
  36. if not flags["M"] then
  37. tape.write("[START]")
  38. --tape.write("[FILENAME "..filepath.." ]")
  39. tape.write("[FILESIZE "..tostring(filesize).." ]")
  40. end
  41.  
  42. while text~=nil do
  43. x,y=term.getCursor()
  44. os.sleep(0)
  45. tape.stop()
  46. tape.write(text)
  47.  
  48. writed=writed+#text
  49. if flags["b"] then
  50. term.setCursor(offset,y)
  51. term.write(tostring(writed).."/"..tostring(filesize))
  52. else
  53. term.setCursor(offset,y)
  54. term.write(" ")
  55. percent=tostring(round(writed/filesize*100,1))
  56. term.setCursor(offset+4-#percent,y)
  57. term.write(percent)
  58. term.setCursor(offset+4,y)
  59. term.write("%")
  60. end
  61.  
  62. text=file:read(10240)
  63. if text~=nil then
  64. text=text.."\n"
  65. end
  66. end
  67.  
  68. if not flags["M"] then
  69. tape.write("[END]")
  70. end
  71. term.setCursor(offset,y)
  72. if flags["b"] then
  73. print(tostring(writed).."/"..tostring(filesize))
  74. else
  75. print(" 100%")
  76. end
  77. end
  78.  
  79. function canonPath(path)
  80. if string.sub(args[1],1,1)=="/" then
  81. return filesystem.canonical(path)
  82. else
  83. return filesystem.canonical(os.getenv("PWD").."/"..path)
  84. end
  85. end
  86.  
  87. ----
  88.  
  89. if args[1]==nil then
  90. file=os.getenv("PWD")
  91. else
  92. for i=1,#args do
  93. if not filesystem.exists(canonPath(args[i])) and not filesystem.exists(canonPath(args[i])..".dfpwm") then
  94. lerror("No such file or directory: "..args[i])
  95. end
  96. end
  97. end
  98.  
  99. if filesystem.isDirectory(args[1] or file) then
  100. for filename in filesystem.list(file) do
  101. if string.find(filename,".dfpwm$") and not filesystem.isDirectory(filename) and not exitl then
  102. writeToTape(file..filename)
  103. end
  104. end
  105. print("Done!")
  106. else
  107. local filept=""
  108. for i=1,#args do
  109. filept=canonPath(args[i])
  110. if not filesystem.exists(filept) and filesystem.exists(filept..".dfpwm") and not filesystem.isDirectory(filept..".dfpwm") then
  111. filept=filept..".dfpwm"
  112. end
  113. writeToTape(filept)
  114. end
  115. print("Done!")
  116. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement