zserge / luash

Tiny lua module to write shell scripts with lua (inspired by Python's sh module)
MIT License
312 stars 29 forks source link

sh: 1: cannot open /tmp/shluainput: No such file #12

Open winterwolf opened 3 years ago

winterwolf commented 3 years ago

I made a simple color picker for Linux. It is written in Lua but uses Python to get the pixel color. Sometimes it displays some strange messages. They don't break the script, but I just don't want to see them. Looks like there are problems with luash temp files ...

getpixelcolor.py

#!/bin/python3

import gi
gi.require_version("Gdk", "3.0")
from gi.repository import Gdk
import sys

def PixelAt(x, y):
  w = Gdk.get_default_root_window()
  pb = Gdk.pixbuf_get_from_window(w, x, y, 1, 1)
  return pb.get_pixels()

print(tuple(PixelAt(int(sys.argv[1]), int(sys.argv[2]))))

colorpicker.lua

#!/bin/luajit

local luash = require "sh"
local socket = require "socket"
local lume = require "lume" -- luarocks install lume

local getlocation = luash.command("xdotool getmouselocation")
local getcolor = luash.command("./getpixelcolor.py")

while true do
  socket.sleep(0.1)
  local location = tostring(getlocation())
  local array = lume.split(location)
  if not array[1] or not array[2] then goto continue end
  local x = lume.split(array[1], ":")[2]
  local y = lume.split(array[2], ":")[2]
  local color = tostring(getcolor(x, y))
  if not color then goto continue end
  print(x, y, color)
  ::continue::
end

output

271     186     (31, 31, 31)
389     189     (205, 136, 46)
542     448     (38, 38, 38)
503     746     (38, 38, 38)
sh: 1: cannot open /tmp/shluainput: No such file
478     743
477     742     (38, 38, 38)
419     728     (38, 38, 38)
414     716     (38, 38, 38)
469     712     (38, 38, 38)
475     710     (38, 38, 38)
sh: 1: cannot open /tmp/shluainput: No such file
474     704
278     534     (215, 163, 100)
46      550     (15, 15, 15)
91      637     (38, 38, 38)
82      591     (203, 215, 163)
JBlaschke commented 1 year ago

@winterwolf I have a PR #15 which contains general improvements, and a rewrite of the command execution part. The upside is that you don't need the temporary file, the downside is that luaposix needs to be installed. At least on all the systems that I work with, lua and luaposix are installed.

JBlaschke commented 1 year ago

By the way -- when you see those errors are you running sh.command in rapid succession? If so, then this might be because of FS latency. If you don't want to use the posix approach, then replacing /tmp/shluainput with os.tmpname() should do the trick.