zserge / luash

Tiny lua module to write shell scripts with lua (inspired by Python's sh module)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Trying to use this with Conky, but having some issues

MostHated opened this issue · comments

Hey there,
I am sure I am doing something incorrectly, but I am new to LUA, so I am not quite sure what I am doing wrong.

Here is essentially what I am trying to do:

Code block using sh
function parts.cputemp(colorBg)
  -- local command = "zsh -c \'/home/mosthated/_dev/languages/sh/temps.zsh \"Tccd1\"'"
  -- local temp = _h.exec("zsh -c \'/home/mosthated/_dev/languages/sh/temps.zsh \"Tccd1\"'")
  -- local temp = sh.command(command)

  -- Trying to run this command to return a single numeric value (30.1, 35.6, etc)
  local temp = sh.command('zsh','-c', '/home/mosthated/_dev/languages/sh/temps.zsh', 'Tccd1')

  local cputemp = [[\
  -- Then I wanted to reuse that same value throughout the rest of the script block
${if_match ]].. temp .. [[<45}\
]] .. _h.value(temp .. '°C', ci.good) .. [[
${else}${if_match ]].. temp ..[[<55}\
]] .. _h.value(temp .. '°C', ci.degraded) .. [[
${else}${if_match  ]]..  temp ..[[>=55}\
]] .. _h.value(temp .. '°C', ci.bad) .. [[
${endif}${endif}${endif}\
]]
  return _h.common('', 'CPU', cputemp, colorBg)
end
Unfortunately, when I attempt to use that code, I get the following message: `attempt to concatenate a function value (local 'temp')` After looking over the instructions again, I attempted to try this:
${if_match ]].. temp() .. [[<45}\
]] .. _h.value(temp() .. '°C', ci.good) .. [[

That then gives me the message of : attempt to call a table value

I also attempted to do tostring(temp()) but I believe that gave a message of: Bad arguments: 'function: 0x55c917c53460' and '45'

Might anyone have any insight on what I might need to change in order to properly get back the numeric value?

Thanks!
-MH

@MostHated Thanks to JBlaschke#1 @Frityet added the ability to concatenate the output from shell functions. You can give this fork a try (it's available as sh on luarocks)

While I definitely appreciate the effort, that was some time ago. As such, I was able to accomplish what I was after eventually in a different manner at the time. Since then I have mostly migrated from Conky to Eww, which uses Rust. I still do use Conky for a few things that I have just been too lazy to remake, and if I ever make any modifications to them, I will be sure to look into this again.

Thanks,
-MH

@JBlaschke I know this is the wrong place to ask, but the fork doesn't have an Issues tab.

I've noticed that for some reason sh doesn't handle wildcards, nor does it handle certain variables. For instance, the following:

local img = find('"$HOME"/.config/hypr/wallpaper/{*.png,*.jpg}') : shuf() : head('-1')

Returns with the following error:

/bin/lua: /home/USER/.luarocks/share/lua/5.4/sh.lua:320: find: ‘"$HOME"/.config/hypr/wallpaper/{*.png,*.jpg}’: No such file or directory

stack traceback:
	[C]: in function 'error'
	/home/USER/.luarocks/share/lua/5.4/sh.lua:320: in global 'find'
	./wallpaper.lua:35: in main chunk
	[C]: in ?```

@JBlaschke I know this is the wrong place to ask, but the fork doesn't have an Issues tab.

I've noticed that for some reason sh doesn't handle wildcards, nor does it handle certain variables. For instance, the following:

local img = find('"$HOME"/.config/hypr/wallpaper/{*.png,*.jpg}') : shuf() : head('-1')

Returns with the following error:

/bin/lua: /home/USER/.luarocks/share/lua/5.4/sh.lua:320: find: ‘"$HOME"/.config/hypr/wallpaper/{*.png,*.jpg}’: No such file or directory

stack traceback:
	[C]: in function 'error'
	/home/USER/.luarocks/share/lua/5.4/sh.lua:320: in global 'find'
	./wallpaper.lua:35: in main chunk
	[C]: in ?```

This is because any command is executed using popen, like creating a new process instead of just invoking the shell, of which * is a shell expression. its still possible to do though

Huh! I never noticed that there wasn't an issues tab. I'll have to fix that.

@StandingPadAnimations what @Frityet said is exactly right. You can do this though: https://github.com/JBlaschke/luash/blob/101f3006cea281ff3af575f851aedf73c1dcb66e/example.lua#L42 and send the shell expression to bash (or whatever your favorite shell is).

I have been thinking of several features to release. Having a shell execution mode just made it on there.