lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Jan 24, 2008 11:00 AM, DarkGod <darkgod@t-o-m-e.net> wrote:

> So here are a few new libs for you to enjoy

Nice work, although I had some trouble reading the docs ;-)

Anyway, here is a little demo for the FAM module. Mostly I
wrote this just to check things out but maybe someone else
might find it useful...

-------------------------------
#!/usr/bin/env lua

require "fam"

if arg[1]=="-h" or arg[1]=="--help"
then
  print("\nWatches a directory for changes.\n\nUsage:")
  print(arg[0].."  [directory]  [interval]\n")
  os.exit(0)
end

function fail(msg) print("Fatal: "..msg); os.exit(1) end

local dir = arg[1] or os.getenv("HOME") -- What to watch
dir = dir:match("^/.*") or fail("Path must be absolute.")

local wait = arg[2] or 5 -- Seconds between updates
wait = tonumber(wait) or fail("Interval must be a number.")

local fc = fam.open() or fail("FAM library init failed.")

local fr = fam.monitorDirectory(fc, dir)
  or fail("Failed to monitor `"..dir.."'\nDoes it exist?")

print(fam._VERSION)
print("Monitoring "..dir)
print("At "..wait.." second intervals...")

repeat
  while fam.waitEvent(fc,wait) do
    while fam.pendingEvent(fc) > 0 do
      local fe = fam.nextEvent(fc)
      if fe then
        if fe.code~="Exists" and fe.code~="EndExist" then
          print(fe.code .." "..fe.filename.." in "..fe.path)
        end
      end
    end
  end
until false

-- Actually, we never get here, but if we did...
fam.monitorCancel(fc,fr)
fam.close(fc)

-------------------------------

Did I get it right?

 - Jeff