Skip to content

Commit

Permalink
#393 Application scripting (introduce a scripting runspace with tty)
Browse files Browse the repository at this point in the history
  • Loading branch information
o-sdn-o committed Jun 2, 2023
1 parent ff5e0e6 commit 8d04786
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 85 deletions.
17 changes: 1 addition & 16 deletions src/netxs/desktopio/console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3713,7 +3713,7 @@ namespace netxs::ui
class host
: public base
{
protected:
public:
using tick = datetime::quartz<events::reactor<>, hint>;
using list = std::vector<rect>;
using gptr = sptr<gate>;
Expand All @@ -3738,7 +3738,6 @@ namespace netxs::ui
if (client) client->rebuild_scene(*this, damaged);
}

public:
host(sptr<pipe> server, xmls config, pro::focus::mode m = pro::focus::mode::hub)
: focus{*this, m, faux },
quartz{ bell::router<tier::general>(), e2::timer::tick.id },
Expand All @@ -3748,20 +3747,6 @@ namespace netxs::ui
using namespace std::chrono;
auto& canal = *server;

config.pushd("/config/scripting/");
if (config.take("enabled", faux))
{
auto lang = config.take("engine", ""s);
config.cd(lang);
auto path = config.take("cwd", ""s);
auto exec = config.take("cmd", ""s);
auto main = config.take("main", ""s);
engine.start(path, exec);
engine.write(main + "\n");
//todo run integration script
}
config.popd();

auto& g = skin::globals();
g.brighter = config.take("brighter" , cell{});//120);
g.kb_focus = config.take("kb_focus" , cell{});//60
Expand Down
28 changes: 28 additions & 0 deletions src/netxs/desktopio/scripting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ namespace netxs::scripting
{
using namespace ui;

namespace attr
{
static constexpr auto cwd = "cwd";
static constexpr auto cmd = "cmd";
static constexpr auto run = "run";
static constexpr auto tty = "tty";
static constexpr auto rse = "engine"; // Runtime Scripting Engine.
}
namespace path
{
static constexpr auto scripting = "/config/scripting/";
}

template<class Host>
class repl
{
Expand Down Expand Up @@ -127,6 +140,21 @@ namespace netxs::scripting
stream{owner },
active{ true }
{
auto& config = owner.config;
if (config.take(path::scripting, faux))
{
config.pushd(path::scripting);
auto rse = config.take(attr::rse, ""s);
config.cd(rse);
auto cwd = config.take(attr::cwd, ""s);
auto cmd = config.take(attr::cmd, ""s);
auto run = config.take(attr::run, ""s);
auto tty = config.take(attr::tty, faux);
start(cwd, cmd);
//todo run integration script
if (run.size()) write(run + "\n");
config.popd();
}

}
};
Expand Down
4 changes: 2 additions & 2 deletions src/netxs/desktopio/system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3026,7 +3026,7 @@ namespace netxs::os
stdinput = std::thread([&] { read_socket_thread(); });
stdwrite = std::thread([&] { send_socket_thread(); });

if (termlink) log(prompt::dtvt, "Console created for pid ", proc_pid);
if (termlink) log(prompt::dtvt, "DirectVT console created for process ", proc_pid);
writesyn.notify_one(); // Flush temp buffer.

return proc_pid;
Expand Down Expand Up @@ -3399,7 +3399,7 @@ namespace netxs::os
stdinput = std::thread([&] { read_socket_thread(); });
stdwrite = std::thread([&] { send_socket_thread(); });

if (termlink) log(prompt::task, "Console created for pid ", proc_pid);
if (termlink) log(prompt::task, "Standard I/O has been redirected for process ", proc_pid);

return proc_pid;
}
Expand Down
104 changes: 55 additions & 49 deletions src/vtm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,35 @@ namespace netxs::app::vtm
sptr applet{};
};

static constexpr auto attr_id = "id";
static constexpr auto attr_alias = "alias";
static constexpr auto attr_hidden = "hidden";
static constexpr auto attr_label = "label";
static constexpr auto attr_notes = "notes";
static constexpr auto attr_title = "title";
static constexpr auto attr_footer = "footer";
static constexpr auto attr_bgc = "bgc";
static constexpr auto attr_fgc = "fgc";
static constexpr auto attr_winsize = "winsize";
static constexpr auto attr_wincoor = "wincoor";
static constexpr auto attr_focused = "focused";
static constexpr auto attr_slimmenu = "slimmenu";
static constexpr auto attr_hotkey = "hotkey";
static constexpr auto attr_type = "type";
static constexpr auto attr_cwd = "cwd";
static constexpr auto attr_param = "param";
static constexpr auto attr_splitter = "splitter";
static constexpr auto attr_config = "config";

static constexpr auto path_item = "/config/menu/item";
static constexpr auto path_autorun = "/config/menu/autorun/item";
static constexpr auto path_viewport = "/config/menu/viewport/coor";
namespace attr
{
static constexpr auto id = "id";
static constexpr auto alias = "alias";
static constexpr auto hidden = "hidden";
static constexpr auto label = "label";
static constexpr auto notes = "notes";
static constexpr auto title = "title";
static constexpr auto footer = "footer";
static constexpr auto bgc = "bgc";
static constexpr auto fgc = "fgc";
static constexpr auto winsize = "winsize";
static constexpr auto wincoor = "wincoor";
static constexpr auto focused = "focused";
static constexpr auto slimmenu = "slimmenu";
static constexpr auto hotkey = "hotkey";
static constexpr auto type = "type";
static constexpr auto cwd = "cwd";
static constexpr auto param = "param";
static constexpr auto splitter = "splitter";
static constexpr auto config = "config";
}
namespace path
{
static constexpr auto item = "/config/menu/item";
static constexpr auto autorun = "/config/menu/autorun/item";
static constexpr auto viewport = "/config/menu/viewport/coor";
}


struct events
{
Expand Down Expand Up @@ -1381,41 +1387,41 @@ namespace netxs::app::vtm
};

auto splitter_count = 0;
for (auto item_ptr : host::config.list(path_item))
for (auto item_ptr : host::config.list(path::item))
{
auto& item = *item_ptr;
auto conf_rec = desk::spec{};
//todo autogen id if absent
conf_rec.splitter = item.take(attr_splitter, faux);
conf_rec.menuid = item.take(attr_id, ""s );
conf_rec.splitter = item.take(attr::splitter, faux);
conf_rec.menuid = item.take(attr::id, ""s );
if (conf_rec.splitter)
{
conf_rec.menuid = "splitter_" + std::to_string(splitter_count++);
}
else if (conf_rec.menuid.empty())
{
log(prompt::hall, "Attribute '", utf::debase(attr_id), "' is missing, skip item");
log(prompt::hall, "Attribute '", utf::debase(attr::id), "' is missing, skip item");
continue;
}
auto label = item.take(attr_label, ""s);
auto label = item.take(attr::label, ""s);
conf_rec.label = label.empty() ? conf_rec.menuid : label;
conf_rec.alias = item.take(attr_alias, ""s);
conf_rec.alias = item.take(attr::alias, ""s);
auto& fallback = conf_rec.alias.empty() ? dflt_spec
: find(conf_rec.alias);
conf_rec.hidden = item.take(attr_hidden, fallback.hidden );
conf_rec.notes = item.take(attr_notes, fallback.notes );
conf_rec.title = item.take(attr_title, fallback.title );
conf_rec.footer = item.take(attr_footer, fallback.footer );
conf_rec.bgc = item.take(attr_bgc, fallback.bgc );
conf_rec.fgc = item.take(attr_fgc, fallback.fgc );
conf_rec.winsize = item.take(attr_winsize, fallback.winsize );
conf_rec.wincoor = item.take(attr_wincoor, fallback.wincoor );
conf_rec.slimmenu = item.take(attr_slimmenu, fallback.slimmenu);
conf_rec.hotkey = item.take(attr_hotkey, fallback.hotkey ); //todo register hotkey
conf_rec.cwd = item.take(attr_cwd, fallback.cwd );
conf_rec.param = item.take(attr_param, fallback.param );
conf_rec.type = item.take(attr_type, fallback.type );
auto patch = item.list(attr_config);
conf_rec.hidden = item.take(attr::hidden, fallback.hidden );
conf_rec.notes = item.take(attr::notes, fallback.notes );
conf_rec.title = item.take(attr::title, fallback.title );
conf_rec.footer = item.take(attr::footer, fallback.footer );
conf_rec.bgc = item.take(attr::bgc, fallback.bgc );
conf_rec.fgc = item.take(attr::fgc, fallback.fgc );
conf_rec.winsize = item.take(attr::winsize, fallback.winsize );
conf_rec.wincoor = item.take(attr::wincoor, fallback.wincoor );
conf_rec.slimmenu = item.take(attr::slimmenu, fallback.slimmenu);
conf_rec.hotkey = item.take(attr::hotkey, fallback.hotkey ); //todo register hotkey
conf_rec.cwd = item.take(attr::cwd, fallback.cwd );
conf_rec.param = item.take(attr::param, fallback.param );
conf_rec.type = item.take(attr::type, fallback.type );
auto patch = item.list(attr::config);
if (patch.size()) conf_rec.patch = patch.front()->snapshot();
if (conf_rec.title.empty()) conf_rec.title = conf_rec.menuid + (conf_rec.param.empty() ? ""s : ": " + conf_rec.param);

Expand Down Expand Up @@ -1636,20 +1642,20 @@ namespace netxs::app::vtm
// hall: Autorun apps from config.
void autorun()
{
vport = host::config.take(path_viewport, dot_00);
vport = host::config.take(path::viewport, dot_00);
auto what = link{};
auto apps = host::config.list(path_autorun);
auto apps = host::config.list(path::autorun);
auto foci = std::vector<sptr<base>>();
foci.reserve(apps.size());
for (auto app_ptr : apps)
{
auto& app = *app_ptr;
if (!app.fake)
{
what.menuid = app.take(attr_id, ""s);
what.square = { app.take(attr_wincoor, dot_00),
app.take(attr_winsize, twod{ 80,25 }) };
auto focused = app.take(attr_focused, faux);
what.menuid = app.take(attr::id, ""s);
what.square = { app.take(attr::wincoor, dot_00),
app.take(attr::winsize, twod{ 80,25 }) };
auto focused = app.take(attr::focused, faux);
what.forced = !!what.square.size;
if (what.menuid.size())
{
Expand Down
25 changes: 7 additions & 18 deletions src/vtm.xml
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
R"==(
<config>
<scripting enabled=true engine="python">
<python cwd="" cmd="python -i" main="print(\"Hello World!\")"/>
<powershell cwd="" cmd="powershell -i" main="Hello World!"/>
<pwsh cwd="" cmd="pwsh -i" main="Hello World!"/>
<lua cwd="" cmd="lua -i" main="print \"Hello World!\""/>
<scripting=on engine="python">
<python cwd="" cmd="python -i" run="print(\"Hello World!\")"/>
<powershell cwd="" cmd="powershell -i -noni" run="\"Hello World!\"" tty=true/>
<pwsh cwd="" cmd="pwsh -i -noni" run="\"Hello World!\"" tty=true/>
<lua cwd="" cmd="lua -i" run="print \"Hello World!\""/>
<js cwd="" cmd="node -i">
<main>
<run>
console.log("Hello, World!");
//public class HelloWorld
//{
// public void printHelloWorld()
// {
// System.out.println("Hello World!");
// }
// public static void main(String[] args)
// {
// printHelloWorld();
// }
//}
</main>
</run>
</js>
</scripting>
<menu selected=Term> <!-- Set selected using menu item id. -->
Expand Down

0 comments on commit 8d04786

Please sign in to comment.