Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I separate `init.lua` to different components in Hammerspoon?

I would like to separate init.lua script used in Hammerspoon to enhance the readability and maintainance.

So it looks like the following:

  • init.lua
  • AppWatcher.lua
  • WiFiWatcher.lua
  • KeyRemap.lua

And then from within init.lua I would read these files and make the watcher activate.

However, it seems that there is no such function defined (maybe I may be missing it, though). Is it possible to separate the logic like that in Hammerspoon?

like image 614
Blaszard Avatar asked Jun 15 '17 06:06

Blaszard


1 Answers

Yes, you can do this using require.

If you put your Lua files in ~/.hammerspoon/, you can then load them using require('modulename'). For example, if you have the following modules:

  • ~/.hammerspoon/AppWatcher.lua
  • ~/.hammerspoon/WiFiWatcher.lua
  • ~/.hammerspoon/KeyRemap.lua

Then you can load them from ~/.hammerspoon/init.lua like this:

local AppWatcher  = require('AppWatcher')
local WiFiWatcher = require('WiFiWatcher')
local KeyRemap    = require('KeyRemap')

You can load any Lua modules, as long as they appear in package.path. To see the directories you can use, take a look at HammerSpoon's package.path setup file. This references the default Lua package.path, which is defined in luaconf.h.

If you want to put your Lua modules in a directory not included in package.path, you can do it by adding them to the LUA_PATH_5_3 or LUA_PATH environment variables.

like image 182
Jack Taylor Avatar answered Oct 03 '22 00:10

Jack Taylor