Skip to content

Commit

Permalink
#393 Application scripting (provide a real console for powershell core)
Browse files Browse the repository at this point in the history
  • Loading branch information
o-sdn-o committed Jun 2, 2023
1 parent 8d04786 commit 1d1626e
Show file tree
Hide file tree
Showing 5 changed files with 480 additions and 297 deletions.
25 changes: 24 additions & 1 deletion src/netxs/desktopio/consrv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,29 @@ struct consrv
return langmap;
}

void set_cp(ui32 c)
{
auto lock = std::lock_guard{ events.locker };
auto& o = outenc;
auto& i = inpenc;
if (o->codepage != c)
{
if (i->codepage == c) o = i; // Reuse existing decoder.
else
{
if (auto p = ptr::shared<decoder>(); p->load(*this, c)) o = p;
}
}
i = o;
for (auto& client : joined) // Reset trailing/hanging bytes.
for (auto& handle : client.tokens)
{
handle.toWIDE.clear();
handle.toANSI.clear();
handle.toUTF8.clear();
}
inpenc->reset();
}
auto attr_to_brush(ui16 attr)
{
auto& colors = uiterm.ctrack.color;
Expand Down Expand Up @@ -2624,7 +2647,7 @@ struct consrv
auto success = direct(packet.target, [&](auto& scrollback)
{
auto& line = celler.content();
count = line.length();
count = static_cast<ui32>(line.length());
if (count > maxsz)
{
count = maxsz;
Expand Down
151 changes: 133 additions & 18 deletions src/netxs/desktopio/scripting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ namespace netxs::scripting
{
using namespace ui;

namespace path
{
static constexpr auto scripting = "/config/scripting/";
}
namespace attr
{
static constexpr auto cwd = "cwd";
Expand All @@ -17,20 +21,117 @@ namespace netxs::scripting
static constexpr auto tty = "tty";
static constexpr auto rse = "engine"; // Runtime Scripting Engine.
}
namespace path

struct faketerm
{
static constexpr auto scripting = "/config/scripting/";
}
using face = ui::face; // Reference for consrv.
struct bufferbase : public ansi::parser
{
twod panel = {1000, 1000};
twod coord = dot_00; // bufferbase: Viewport cursor position; 0-based.
template<class T>
void resize_viewport(T a) { }
template<class T>
void set_autocr(T a) { }
void cup0(twod p) { }
template<bool Copy = faux, class Span, class Shader>
void _data(si32 count, Span const& proto, Shader fuse) { }
void clear_all() { }
void ed(si32) { }
template<class T>
void do_viewport_copy(T mirror) { }
void cr() { }
void lf(si32 n) { }
void move(si32 n) { }
};
struct alt_screen : public bufferbase
{
alt_screen() = default;
alt_screen(faketerm&) { }
};
struct scrollback : public bufferbase
{ };
// repl: Terminal 16/256 color palette tracking functionality.
struct c_tracking
{
using pals = std::remove_const_t<decltype(rgba::color256)>;
using func = std::unordered_map<text, std::function<void(view)>>;
pals color; // c_tracking: 16/256 colors palette.
func procs; // c_tracking: Handlers.
};
// repl: Terminal title tracking functionality.
struct w_tracking
{
std::map<text, text> props;
std::map<text, std::vector<text>> stack;
// w_tracking: Get terminal window property.
auto& get(text const& property)
{
auto& utf8 = props[property];
return utf8;
}
// w_tracking: Set terminal window property.
void set(text const& property, qiew txt) { }
};
// repl: VT-style mouse tracking functionality.
struct m_tracking
{
enum mode
{
none = 0,
bttn = 1 << 0,
drag = 1 << 1,
move = 1 << 2,
over = 1 << 3,
utf8 = 1 << 4,
buttons_press = bttn,
buttons_drags = bttn | drag,
all_movements = bttn | drag | move,
negative_args = bttn | drag | move | over,
};
enum prot
{
x11,
sgr,
w32,
};
void enable(mode m) { }
void disable(mode m) { }
void setmode(prot p) { }
};
struct caret
{
template<class T = si32>
void style(T m) { }
auto style() const { return std::pair{ true, faux }; }
void hide() { }
void show() { }
void toggle() { }
};
alt_screen altbuf;
scrollback normal;
bufferbase* target{&normal};
c_tracking ctrack; // repl: Custom terminal palette tracking object.
m_tracking mtrack; // repl: .
w_tracking wtrack; // repl: .
caret cursor; // repl: Text cursor controller.
bool io_log{ faux }; // repl: Stdio logging.
void sb_min(si32 size_y) { }
void window_resize(twod windowsz) { }
template<class T>
void reset_to_altbuf(T& console) { }
template<class T>
void reset_to_normal(T a) { }
};

template<class Host>
class repl
struct repl : public faketerm
{
using s11n = directvt::binary::s11n;
using pidt = os::pidt;
using task = os::task;

Host& owner;
using vtty = sptr<os::runspace::basetty>;

Host& owner;
// repl: Event handler.
class xlat
: public s11n
Expand Down Expand Up @@ -89,9 +190,9 @@ namespace netxs::scripting
text cmdarg; // repl: Startup command line arguments.
flag active; // repl: Scripting engine lifetime.
pidt procid; // repl: PTY child process id.
task engine; // repl: Scripting engine instance.
vtty engine; // repl: Scripting engine instance.

// repl: Proceed DirectVT input.
// repl: Proceed input.
void ondata(view data)
{
if (active)
Expand All @@ -100,8 +201,14 @@ namespace netxs::scripting
//stream.s11n::sync(data);
}
}
// repl: Proceed input.
template<class T>
void ondata(view data, T target)
{
log(prompt::repl, ansi::hi(utf::debase<faux, faux>(data)));
}
// repl: Shutdown callback handler.
void onexit(si32 code)
void onexit(si32 code, view msg = {})
{
//netxs::events::enqueue(owner.This(), [&, code](auto& boss) mutable
//{
Expand All @@ -110,29 +217,36 @@ namespace netxs::scripting
// //backup.reset(); // Call repl::dtor.
//});
}

public:
// repl: .
template<class P>
void update(P proc)
{
proc();
}
// repl: Write client data.
void write(view data)
{
if (!engine) return;
log(prompt::repl, "exec: ", ansi::hi(utf::debase<faux, faux>(data)));
engine.write(data);
engine->write(data);
}
// repl: Start a new process.
void start(text cwd, text cmd)
{
if (!engine) return;
curdir = cwd;
cmdarg = cmd;
if (!engine)
if (!engine->connected())
{
procid = engine.start(curdir, cmdarg, [&](auto utf8_shadow) { ondata(utf8_shadow); },
[&](auto exit_reason) { onexit(exit_reason); });
procid = engine->start(curdir, cmdarg, normal.panel, [&](auto utf8_shadow) { ondata(utf8_shadow); },
[&](auto code, auto msg) { onexit(code, msg); });
}
}
void shut()
{
active = faux;
if (engine) engine.shut();
if (!engine) return;
if (engine->connected()) engine->shut();
}

repl(Host& owner)
Expand All @@ -150,12 +264,13 @@ namespace netxs::scripting
auto cmd = config.take(attr::cmd, ""s);
auto run = config.take(attr::run, ""s);
auto tty = config.take(attr::tty, faux);
if (tty) engine = ptr::shared<os::runspace::tty<repl>>(*this);
else engine = ptr::shared<os::runspace::raw>();
start(cwd, cmd);
//todo run integration script
if (run.size()) write(run + "\n");
config.popd();
}

}
};
}
Loading

0 comments on commit 1d1626e

Please sign in to comment.