Advertisement
andyipod1437

Untitled

Jun 10th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. --musicfs: musical storage filesystem for tapes--
  2.  
  3. -- copypasta
  4. function split(str, pat)
  5. local t = {} -- NOTE: use {n = 0} in Lua-5.0
  6. local fpat = "(.-)" .. pat
  7. local last_end = 1
  8. local s, e, cap = str:find (fpat, 1)
  9. while s do
  10. if s ~= 1 or cap ~= "" then
  11. table.insert(t,cap)
  12. end
  13. last_end = e+1
  14. s, e, cap = str:find(fpat, last_end)
  15. end
  16. if last_end <= #str then
  17. cap = str:sub(last_end)
  18. table.insert(t, cap)
  19. end
  20. return t
  21. end
  22.  
  23. local musicfs = dofile("/usr/lib/libmusicfs.lua")
  24. local component = require "component"
  25. local term = require "term"
  26.  
  27. local openfs
  28.  
  29. local function getTapeDrive(addr)
  30. return addr and component.proxy(component.get(addr)) or component.tape_drive
  31. end
  32.  
  33. local commands = {}
  34.  
  35. function commands.format(addr)
  36. musicfs.format(getTapeDrive(addr))
  37. end
  38.  
  39. function commands.open(addr)
  40. openfs = musicfs.open(getTapeDrive(addr))
  41. end
  42.  
  43. function commands.play(song)
  44. if song == nil then openfs.playSong() return end --unpause logic
  45. song = tonumber(song) or song
  46. if type(song) == "string" then
  47. --find a string that contains "song"
  48. for i, s in openfs.listSongs() do
  49. if s.name:find(song) then
  50. song = i
  51. break
  52. end
  53. end
  54. end
  55. openfs.playSong(song)
  56. end
  57.  
  58. function commands.stop()
  59. openfs.stopSong()
  60. end
  61.  
  62. function commands.pause()
  63. openfs.pauseSong()
  64. end
  65.  
  66. function commands.getStatus()
  67. if not openfs then
  68. print("Status: No FS opened")
  69. return
  70. end
  71. if not openfs.isPlaying() then
  72. print("Status: Not playing")
  73. else
  74. local index = openfs.isPlaying()
  75. local song = openfs.getSong(index)
  76. local curp, finp = openfs.getCurrentPosition()
  77. print("Status: Playing "..song.name.." ("..index..") "..curp.."/"..finp)
  78. end
  79. end
  80.  
  81. function commands.add(title,speed,file)
  82. speed = tonumber(speed)
  83. openfs.addSong(title,speed,(require "filesystem").open(file,"r"))
  84. end
  85.  
  86. function commands.remove(song)
  87. if song == nil then openfs.playSong() return end --unpause logic
  88. song = tonumber(song) or song
  89. if type(song) == "string" then
  90. --find a string that contains "song"
  91. for i, s in openfs.listSongs() do
  92. if s.name:find(song) then
  93. song = i
  94. break
  95. end
  96. end
  97. end
  98. openfs.removeSong(song)
  99. end
  100.  
  101. function commands.exit()
  102. error()
  103. end
  104.  
  105. (require "event").timer(0.5,function()
  106. if openfs then
  107. openfs.update()
  108. end
  109. end,math.huge)
  110.  
  111. print("MusicFS CLI")
  112. while true do
  113. io.write("> ")
  114. local incmd = term.read()
  115. if not incmd then break end
  116. incmd = incmd:sub(1,-2)
  117. local args = split(incmd," ")
  118. local cmd = table.remove(args,1)
  119. if commands[cmd] then
  120. commands[cmd](table.unpack(args))
  121. elseif cmd then
  122. print("Unknown command: "..cmd)
  123. end
  124. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement