Git Product home page Git Product logo

Comments (13)

Mikeno-Philippe avatar Mikeno-Philippe commented on June 8, 2024 1

from hammerspoon.

Mikeno-Philippe avatar Mikeno-Philippe commented on June 8, 2024

I found how to solve this issue using rawset

function declare (name, initval)
	rawset(_G, name, initval or false)
end

counter, offset = 0, 1
declare("counter", 0)
declare("offset", 1)

from hammerspoon.

Rhys-T avatar Rhys-T commented on June 8, 2024

It works fine on my Hammerspoon. Do you have any code in your config (or loaded Spoons) that would be setting a metatable on the global environment?

What does this code print for you when pasted into the console?

local mt = getmetatable(_G)
if mt then
	print(hs.inspect(mt))
	for k,v in pairs(mt) do
		if type(v) == 'function' then
			local info = debug.getinfo(v)
			if what ~= 'C' then
				print(hs.inspect(info))
			end
		end
	end
else
	print('no metatable on _G')
end

from hammerspoon.

Mikeno-Philippe avatar Mikeno-Philippe commented on June 8, 2024

from hammerspoon.

Rhys-T avatar Rhys-T commented on June 8, 2024

Weird… When you ran into the issue, was it in the console or the config file? If it was the config, try putting the code I sent there instead of in the console and see if that prints anything different.

Actually, just in case _G isn't what's getting used for some reason, can you try using this version instead?

local function checkMT(name, tbl)
	local mt = getmetatable(tbl)
	if mt then
		print(hs.inspect(mt))
		for k,v in pairs(mt) do
			if type(v) == 'function' then
				local info = debug.getinfo(v)
				if what ~= 'C' then
					print(hs.inspect(info))
				end
			end
		end
	else
		print('no metatable on '..name)
	end
end
checkMT('_G', _G)
if rawequal(_ENV, _G) then
	print '_ENV is _G'
else
	print '_ENV is not _G'
	checkMT('_ENV', _ENV)
end

from hammerspoon.

Mikeno-Philippe avatar Mikeno-Philippe commented on June 8, 2024

from hammerspoon.

Rhys-T avatar Rhys-T commented on June 8, 2024

What about when you run it from within your reset and/or inc functions? Also, is there anywhere you're declaring counter and/or offset as locals (or function argument names)?

from hammerspoon.

Mikeno-Philippe avatar Mikeno-Philippe commented on June 8, 2024

from hammerspoon.

Rhys-T avatar Rhys-T commented on June 8, 2024

I don't know of anything else that could interfere with setting global variables. As far as I know, Hammerspoon should be using an unmodified Lua interpreter.

The only other thing I can think of to try right now is moving your entire init.lua file aside and making a new one with just the code from your initial post. If that works correctly, we can try to narrow down exactly where it starts breaking. If it still fails, I'm not sure what to do.

from hammerspoon.

Mikeno-Philippe avatar Mikeno-Philippe commented on June 8, 2024

from hammerspoon.

Mikeno-Philippe avatar Mikeno-Philippe commented on June 8, 2024

from hammerspoon.

Rhys-T avatar Rhys-T commented on June 8, 2024

I see a couple of issues in that code that might explain why the counter isn't updating properly:

  • I don't see a definition for that setOffset function anywhere, and even if it exists, any change it makes to offset will get overwritten by the offset = part of the menu item's fn - changing it to whatever setOffset's return value was.
  • This version of reset doesn't seem to actually set counter at all - it just updates the menu's title and shows the alert. Also, it's setting offset to reset's return value, which is nil - meaning that any Increase/Decrease commands after that point will throw an attempt to perform arithmetic on a nil value error and not actually change the counter.

These next two aren't directly related to the counter problem, but:

  • $ and ^ aren't their own keys, at least on most keyboard layouts that I know of, so they don't have keycodes defined in hs.keycodes.map and can't be passed directly to hs.hotkey.bind like that. Assuming that they're Shift-4 and Shift-6 like on my keyboard, you'll need to do something like:
hyper = {"cmd","alt","ctrl"}
hyperShift = {"cmd","alt","ctrl","shift"}

hs.hotkey.bind(hyperShift, "4", function()
	updateCounter(offset)
end)

hs.hotkey.bind(hyperShift, "6", function()
	updateCounter(-offset)
end)
  • "Set offset (" .. offset .. ")": This title will get evaluated once when your config first runs, and won't update when the offset is changed. You can either include something like:
	menuMenu[4].title = "Set offset (" .. offset .. ")"
	menu:setMenu(menuMenu)

in your setOffset function, or change menuMenu to a function so it gets re-evaluated each time the menu opens:

function menuMenu()
	return {
		{ title = "Increase", fn = function() updateCounter(offset) end },
		{ title = "Decrease", fn = function() updateCounter(-offset) end },
		{ title = "-" },
		{ title = "Set offset (" .. offset .. ")" , fn = function() offset = setOffset(offset) end },
		{ title = "Reset counter", fn = function() offset = reset() end }
	}
end

from hammerspoon.

Rhys-T avatar Rhys-T commented on June 8, 2024

Glad to hear it's working for you.

Concerning both keys (« $ » and « ^ »), they are direct own keys on a French keyboard!

Gotcha. For some reason I wasn't sure if Hammerspoon knew that, though, but I see now that it does properly update hs.keycodes.map to include $ and ^ when you're on that layout.

from hammerspoon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.