Skip to content

Commit

Permalink
wk repl
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Oct 7, 2019
1 parent c8c3bf1 commit d4a9d3b
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 39 deletions.
8 changes: 8 additions & 0 deletions Blink/TermController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ extension TermController: SessionDelegate {
}

extension TermController: TermDeviceDelegate {
func runCommand(_ cmd: String!) {

}

public func deviceIsReady() {
startSession()
}
Expand Down Expand Up @@ -239,6 +243,10 @@ extension TermController: TermDeviceDelegate {
public func viewController() -> UIViewController! {
return self
}

public func lineSubmitted(_ line: String!) {
_session?.enqueueCommand(line)
}
}

extension TermController: SuspendableSession {
Expand Down
3 changes: 3 additions & 0 deletions Blink/TermDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
- (void)deviceSizeChanged;
- (void)viewFontSizeChanged:(NSInteger)size;
- (BOOL)handleControl:(NSString *)control;
- (void)lineSubmitted:(NSString *)line;
- (void)deviceFocused;
- (UIViewController *)viewController;

Expand All @@ -63,6 +64,8 @@
- (void)attachInput:(TermInput *)termInput;
- (void)attachView:(TermView *)termView;

- (void)onSubmit:(NSString *)line;

- (void)focus;
- (void)blur;

Expand Down
12 changes: 12 additions & 0 deletions Blink/TermDevice.m
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ - (id)init

- (void)write:(NSString *)input
{
if (!_rawMode) {
[self.view processKB:input];
return;
}
NSUInteger len = [input lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
write(_pinput[1], input.UTF8String, len);
if (_echoMode) {
Expand Down Expand Up @@ -352,11 +356,19 @@ - (void)viewWinSizeChanged:(struct winsize)newWinSize
[_delegate deviceSizeChanged];
}

- (void)onSubmit:(NSString *)line {
[_delegate lineSubmitted:line];
}

- (void)viewSendString:(NSString *)data
{
[self write:data];
}

- (void)viewSubmitLine:(NSString *)line {
[_delegate lineSubmitted:line];
}

- (void)viewCopyString:(NSString *)text
{
[[UIPasteboard generalPasteboard] setString:text];
Expand Down
4 changes: 4 additions & 0 deletions Blink/TermJS.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ NSString *term_setAutoCarriageReturn(BOOL state)
return [NSString stringWithFormat:@"term_setAutoCarriageReturn(%@);", state ? @"true" : @"false"];
}

NSString *term_processKB(NSString *str) {
return [NSString stringWithFormat:@"term_processKB(%@[0])", _encodeString(str)];
}

NSString *term_restore() {
return @"term_restore()";
}
Expand Down
2 changes: 2 additions & 0 deletions Blink/TermView.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
- (void)viewSendString:(NSString *)data;
- (void)viewCopyString:(NSString *)text;
- (void)viewShowAlert:(NSString *)title andMessage:(NSString *)message;
- (void)viewSubmitLine:(NSString *)line;

@end

Expand Down Expand Up @@ -76,6 +77,7 @@
- (void)setWidth:(NSInteger)count;
- (void)setFontSize:(NSNumber *)newSize;
- (void)write:(NSString *)data;
- (void)processKB:(NSString *)str;
- (void)setCursorBlink:(BOOL)state;
- (void)setBoldAsBright:(BOOL)state;
- (void)setBoldEnabled:(NSUInteger)state;
Expand Down
6 changes: 6 additions & 0 deletions Blink/TermView.m
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ - (void)blur
[_webView evaluateJavaScript:term_blur() completionHandler:nil];
}

- (void)processKB:(NSString *)str {
[self _evalJSScript: term_processKB(str)];
}

// Write data to terminal control
- (void)write:(NSString *)data
{
Expand Down Expand Up @@ -377,6 +381,8 @@ - (void)userContentController:(WKUserContentController *)userContentController
[_device viewShowAlert:data[@"title"] andMessage:data[@"message"]];
} else if ([operation isEqualToString:@"sendString"]) {
[_device viewSendString:data[@"string"]];
} else if ([operation isEqualToString:@"line"]) {
[_device viewSubmitLine:data[@"text"]];
}
}

Expand Down
4 changes: 2 additions & 2 deletions Resources/hterm_all.min.js

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions Resources/term.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ function term_setupDefaults() {
term_set('allow-images-inline', true); // need to make it work
}

var _kb;

function term_processKB(str) {
if (_kb && str) {
_kb.processInput(str);
}
}

function term_setup() {
t = new hterm.Terminal('blink');

Expand All @@ -77,6 +85,8 @@ function term_setup() {
document.body.style.backgroundColor =
t.scrollPort_.screen_.style.backgroundColor;
var bgColor = _colorComponents(t.scrollPort_.screen_.style.backgroundColor);

_kb = new TermPrompt("", t);
_postMessage('terminalReady', {size, bgColor});

t.keyboard.characterEncoding = 'raw'; // we are UTF8. Fix for #507
Expand Down
1 change: 1 addition & 0 deletions Sessions/MCPSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
- (void)registerSSHClient:(SSHClient *)sshClient;
- (void)unregisterSSHClient:(SSHClient *)sshClient;

- (void)enqueueCommand:(NSString *)cmd;
- (bool)isRunningCmd;

- (void)updateAllowedPaths;
Expand Down
97 changes: 60 additions & 37 deletions Sessions/MCPSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ @implementation MCPSession {
Session *_childSession;
NSString *_currentCmd;
NSMutableArray<WeakSSHClient *> *_sshClients;
dispatch_queue_t _cmdQueue;
}

@dynamic sessionParams;
Expand All @@ -70,20 +71,72 @@ - (id)initWithDevice:(TermDevice *)device andParams:(MCPParams *)params {
if (self = [super initWithDevice:device andParams:params]) {
_sshClients = [[NSMutableArray alloc] init];
_sessionUUID = [[NSProcessInfo processInfo] globallyUniqueString];
_cmdQueue = dispatch_queue_create("mcp.command.queue", DISPATCH_QUEUE_SERIAL);
}

return self;
}

- (int)main:(int)argc argv:(char **)argv
{
- (void)executeWithArgs:(NSString *)args {

}

- (void)enqueueCommand:(NSString *)cmd {
if (_currentCmd) {
[_device writeIn:cmd];
return;
}
dispatch_async(_cmdQueue, ^{
// [_device writeIn:@"\n"];
[self _runCommand:cmd];
});
}

- (BOOL)_runCommand:(NSString *)cmdline {
[self setActiveSession];
ios_setMiniRoot([BlinkPaths documents]);
ios_setStreams(_stream.in, _stream.out, _stream.err);
ios_setContext((__bridge void*)self);
[self updateAllowedPaths];
[[NSFileManager defaultManager] changeCurrentDirectoryPath:[BlinkPaths documents]];


NSArray *arr = [cmdline componentsSeparatedByString:@" "];
NSString *cmd = arr[0];

if ([cmd isEqualToString:@"exit"]) {
return NO;
} else if ([cmd isEqualToString:@"mosh"]) {
[self _runMoshWithArgs:cmdline];
if (self.sessionParams.hasEncodedState) {
return NO;
}
} else if ([cmd isEqualToString:@"ssh2"]) {
[self _runSSHWithArgs:cmdline];
} else if ([cmd isEqualToString:@"ssh-copy-id"]) {
[self _runSSHCopyIDWithArgs:cmdline];
} else {
[self.delegate indexCommand:cmdline];
_currentCmd = cmdline;
thread_stdout = nil;
thread_stdin = nil;
thread_stderr = nil;

// Re-evalute column number before each command
setenv("COLUMNS", [@(_device->win.ws_col) stringValue].UTF8String, 1); // force rewrite of value
ios_system(cmdline.UTF8String);
_currentCmd = nil;
_sshClients = [[NSMutableArray alloc] init];
}

[_device setRawMode:NO];

return YES;
}

- (int)main:(int)argc argv:(char **)argv
{

// We are restoring mosh session if possible first.
if ([@"mosh" isEqualToString:self.sessionParams.childSessionType] && self.sessionParams.hasEncodedState) {
_childSession = [[MoshSession alloc] initWithDevice:_device andParams:self.sessionParams.childSessionParams];
Expand All @@ -94,41 +147,6 @@ - (int)main:(int)argc argv:(char **)argv
}
}

// Running repl loop
_repl = [[Repl alloc] initWithDevice:_device andStream: _stream];
[_repl loopWithCallback:^BOOL(NSString *cmdline) {

NSArray *arr = [cmdline componentsSeparatedByString:@" "];
NSString *cmd = arr[0];

if ([cmd isEqualToString:@"exit"]) {
return NO;
} else if ([cmd isEqualToString:@"mosh"]) {
[self _runMoshWithArgs:cmdline];
if (self.sessionParams.hasEncodedState) {
return NO;
}
} else if ([cmd isEqualToString:@"ssh2"]) {
[self _runSSHWithArgs:cmdline];
} else if ([cmd isEqualToString:@"ssh-copy-id"]) {
[self _runSSHCopyIDWithArgs:cmdline];
} else {
[self.delegate indexCommand:cmdline];
_currentCmd = cmdline;
thread_stdout = nil;
thread_stdin = nil;
thread_stderr = nil;

// Re-evalute column number before each command
setenv("COLUMNS", [@(_device->win.ws_col) stringValue].UTF8String, 1); // force rewrite of value
ios_system(cmdline.UTF8String);
_currentCmd = nil;
_sshClients = [[NSMutableArray alloc] init];
}

return YES;
}];

return 0;
}

Expand Down Expand Up @@ -278,6 +296,11 @@ - (BOOL)handleControl:(NSString *)control
ios_kill();
}
return YES;
} else {
if ([_device rawMode]) {
return YES;
}
return NO;
}
return YES;
}
Expand Down

0 comments on commit d4a9d3b

Please sign in to comment.