geany / geany-plugins

The combined Geany Plugins collection
http://plugins.geany.org/
594 stars 266 forks source link

Pass command to internal terminal #1373

Closed Johnmcenroyy closed 1 month ago

Johnmcenroyy commented 1 month ago

Hello

Can command be passed to internal terminal from LUA plugin or C plugin ? Something like geany.launch ("internal terminal","mc")

Thanks

elextr commented 1 month ago

If you mean the built-in terminal tab then AFAIK access to it is not available to plugins.

Johnmcenroyy commented 1 month ago

I have found a way to send command to internal terminal from plugins. I have written little LUA script to do it. This script works as a demonstration of "man" command inside Geany's internal terminal, so you select any command you want to lookup documentation and then choose this LUA script in Geany's menu - you will see documentation in Geany's internal terminal.

#! /usr/bin/env lua
-- echo ls cd

sel=geany.selection()
-- geany.launch ('xfce4-terminal', '--hold', '-e', 'man '..sel)

-- get geany bash pid
local handle = io.popen([[bash -c 'pid=$(pgrep geany) && pstree -p $pid | grep -oP "(?<=bash\().*?(?=\))" | head -n 1']])
local geany_bash_pid = handle:read("*a")
handle:close()
geany.status("Geany bash pid is: "..geany_bash_pid)

-- get pts pid which = geany_bash_pid
for i=0,10 do 

 local handle = io.popen([[bash -c 'ps -t pts/]]..i..[[ | sed -n "2p" | grep -oP "(?<= ).*?(?=pts/)" | sed "s/ //g"']])
 local pts_pid = handle:read("*a")
 handle:close()
 geany.status("PTS pid is: "..pts_pid)

 if string.find(pts_pid, geany_bash_pid) then
  geany_bash_pts = i
  geany.status("FOUND IT !: "..geany_bash_pts)
  break
 end
end

-- send geany selection with man command to geany's internal terminal by using open-source utility ttyecho
-- ttyecho stackoverflow info: https://stackoverflow.com/questions/24725051/execute-a-command-in-another-terminal-via-dev-pts
-- ttyecho original article: http://www.humbug.in/2010/utility-to-send-commands-or-data-to-other-terminals-ttypts/
-- ttyecho original article (web.archive): https://web.archive.org/web/20240324125143/http://www.humbug.in/2010/utility-to-send-commands-or-data-to-other-terminals-ttypts/
-- ttyecho original sources: https://github.com/osospeed/ttyecho
-- ttyecho fork sources: https://github.com/andreabenini/ttyEcho
-- ttyecho executable need to be modified after building from sources for execution without root (attention! - check sources!)
-- modification: sudo chown root:root ttyecho && sudo chmod u+s ttyecho
local handle = io.popen([[bash -c '~/.config/geany/plugins/helpers/ttyecho -n /dev/pts/]]..geany_bash_pts..[[ man\ ]]..sel..[[']])
handle:close()