Skip to content

Commit 3b1289e

Browse files
committed
Avoid using shell commands
Instead of using shell commands via io.popen() and os.execute(), we now just open a handle to the standard output TTY, and write to it directly.
1 parent df916e4 commit 3b1289e

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

plugin/bg.lua

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
1-
local handle = io.popen("tty")
2-
local tty = handle:read("*a")
3-
handle:close()
4-
5-
if tty:find("not a tty") then
1+
local tty = vim.uv.new_tty(1, false)
2+
if tty == nil then
3+
-- Standard out is not a terminal
64
return
75
end
86

97
local update_count = 0
108

119
local reset = function()
1210
if os.getenv("TMUX") then
13-
os.execute('printf "\\ePtmux;\\e\\033]111\\007\\e\\\\"')
11+
tty:write("\x1bPtmux;\x1b\x1b]111\x07\x1b\\")
1412
elseif os.getenv("TERM") == "xterm-kitty" then
15-
for i=1, update_count do
16-
os.execute('printf "\\033]30101\\007" > ' .. tty)
13+
for _ = 1, update_count do
14+
tty:write("\x1b]30101\x07")
1715
end
1816
else
19-
os.execute('printf "\\033]111\\007" > ' .. tty)
17+
tty:write("\x1b]111\x07")
2018
end
2119
end
2220

2321
local update = function()
24-
local normal = vim.api.nvim_get_hl_by_name("Normal", true)
25-
local bg = normal["background"]
26-
local fg = normal["foreground"]
22+
local normal = vim.api.nvim_get_hl(0, { name = "Normal", link = false, create = false })
23+
local bg = normal.bg
24+
local fg = normal.fg
2725
if bg == nil then
2826
return reset()
2927
end
@@ -32,15 +30,15 @@ local update = function()
3230
local fghex = string.format("#%06x", fg)
3331

3432
if os.getenv("TERM") == "xterm-kitty" then
35-
os.execute('printf "\\033]30001\\007" > ' .. tty)
33+
tty:write("\x1b]30001\x07")
3634
end
3735

3836
if os.getenv("TMUX") then
39-
os.execute('printf "\\ePtmux;\\e\\033]11;' .. bghex .. '\\007\\e\\\\"')
40-
os.execute('printf "\\ePtmux;\\e\\033]12;' .. fghex .. '\\007\\e\\\\"')
37+
tty:write("\x1bPtmux;\x1b\x1b]11;" .. bghex .. "\x07\x1b\\")
38+
tty:write("\x1bPtmux;\x1b\x1b]12;" .. fghex .. "\x07\x1b\\")
4139
else
42-
os.execute('printf "\\033]11;' .. bghex .. '\\007" > ' .. tty)
43-
os.execute('printf "\\033]12;' .. fghex .. '\\007" > ' .. tty)
40+
tty:write("\x1b]11;" .. bghex .. "\x07")
41+
tty:write("\x1b]12;" .. fghex .. "\x07")
4442
end
4543

4644
update_count = update_count + 1

0 commit comments

Comments
 (0)