Advertisement
svdragster

search program

May 8th, 2013
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.17 KB | None | 0 0
  1. -- Search program by svdragster
  2.  
  3. local split = function(str, pat) local t = {} local fpat = "(.-)"..pat local last_end = 1 local s, e, cap = str:find(fpat, 1) while s do if s ~= 1 or cap ~= "" then table.insert(t,cap) end last_end = e+1 s, e, cap = str:find(fpat, last_end) end if last_end <= #str then cap = str:sub(last_end) table.insert(t, cap) end return t end
  4.  
  5. debug = false
  6. found = false
  7. searchFileName = "nada"
  8. searchFileType = "sve"
  9.  
  10. function getProgramType(path)
  11.   programDir = split(path, "/")
  12.   res = split(programDir[#programDir], "%.")
  13.   programPath = path
  14.   programName = res[1]
  15.   programType = res[#res]
  16.   if debug then
  17.     term.setTextColour( colors.blue )
  18.     print(programPath)
  19.     print(programName)
  20.     print(programType)
  21.     term.setTextColour( colors.white )
  22.   end
  23. end
  24.  
  25. function check(path)
  26.   if debug then
  27.     print("check")
  28.   end
  29.     getProgramType(path)
  30.   if debug then
  31.     term.setTextColour( colors.green )
  32.     print(programName)
  33.     term.setTextColour( colors.orange )
  34.     print(programType)
  35.     term.setTextColour( colors.white )
  36.   end
  37.     if programName == searchFileName then
  38.         if programType == searchFileType then
  39.           found = true
  40.           return
  41.       end
  42.     end
  43. end
  44.  
  45. function readDir(dir)
  46.  if debug then
  47.    term.setTextColour( colors.red )
  48.    print("Dir: "..dir)
  49.    term.setTextColour( colors.white )
  50.  end
  51.  local dateien = fs.list(dir)
  52.  for i = 1, #dateien do
  53.    if debug then
  54.      term.setTextColour( colors.red )
  55.      print("Dir: "..dir)
  56.      term.setTextColour( colors.white )
  57.      print("readDir")
  58.    end
  59.    if found then
  60.      return
  61.    end
  62.   path = fs.combine(dir, dateien[i])
  63.   if fs.isDir(path) then
  64.     if (dateien[i] ~= "rom") then
  65.       readDir(path)
  66.     end
  67.   end
  68.   check(path)
  69.  end
  70. end
  71.  
  72. function lookFor(str)
  73.   local res = split(str, "%.")
  74.   searchFileName = res[1]
  75.   searchFileType = res[#res]
  76.   readDir("/")
  77.   if found then
  78.     return true
  79.   else
  80.     return false
  81.   end
  82. end
  83.  
  84. ------------------
  85. --MAIN------------
  86. ------------------
  87.  
  88. local tArgs = { ... }
  89.  
  90. if #tArgs > 0 then
  91.   if lookFor(tArgs[1]) then
  92.     print("Path: /"..path)
  93.   else
  94.     print("File not found")
  95.   end
  96.   return
  97. end
  98.  
  99. if term.isColor() then
  100.   advanced = true
  101. end
  102.  
  103. if advanced then
  104.   term.setTextColour( colors.yellow )
  105. end
  106.  
  107. local done = false
  108.  
  109. while not done do
  110.   num = math.random(1, 3)
  111.  
  112.   if num == 1 then
  113.     print("What file are you looking for?")
  114.   elseif num == 2 then
  115.     print("What file are you searching for?")
  116.   elseif num == 3 then
  117.     print("Did you loose a file? I will find it.")
  118.   end
  119.   print("Type \"help\" for help.")
  120.  
  121.   if advanced then
  122.     term.setTextColour( colors.orange )
  123.   end
  124.   input = read()
  125.   if advanced then
  126.     term.setTextColour( colors.yellow )
  127.   end
  128.   if input == "help" then
  129.     if advanced then
  130.       term.setTextColour( colors.lightGray )
  131.     end
  132.     print("<Required> [Optional]")
  133.     print("Usage: <filename>[.filetype]")
  134.     print("or in shell: search <filename>")
  135.     if advanced then
  136.       term.setTextColour( colors.yellow )
  137.     end
  138.   else
  139.     done = true
  140.   end
  141. end
  142.  
  143. if lookFor(input) then
  144.   print("Path: /"..path)
  145. else
  146.   print("File not found")
  147. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement