Skip to content

Commit

Permalink
fixed dumb shell
Browse files Browse the repository at this point in the history
added shell connecting state
  • Loading branch information
mariotaku committed Apr 4, 2023
1 parent 1b53494 commit 069f858
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 25 deletions.
3 changes: 2 additions & 1 deletion src-tauri/src/shell_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Shell {
pub token: ShellToken,
created_at: Instant,
device: Device,
has_pty: bool,
pub(crate) has_pty: Mutex<bool>,
pub(crate) sender: Mutex<Option<Sender<ShellMessage>>>,
pub(crate) callback: Mutex<Option<Box<dyn ShellCallback + Send + Sync>>>,
pub(crate) parser: Mutex<Parser>,
Expand All @@ -42,6 +42,7 @@ pub struct ShellToken(Uuid);
pub struct ShellInfo {
pub token: ShellToken,
pub title: String,
pub ready: bool,
#[serde(rename = "hasPty")]
pub has_pty: bool,
#[serde(skip_serializing)]
Expand Down
27 changes: 20 additions & 7 deletions src-tauri/src/shell_manager/shell.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use libssh_rs::Error::RequestDenied;
use libssh_rs::SshResult;
use std::collections::HashMap;
use std::io::Write;
use std::sync::mpsc::channel;
Expand All @@ -19,7 +21,7 @@ impl Shell {
}

pub fn resize(&self, rows: u16, cols: u16) -> Result<(), Error> {
if !self.has_pty {
if self.has_pty.lock().unwrap().eq(&false) {
return Err(Error::Unsupported);
}
self.parser.lock().unwrap().set_size(rows, cols);
Expand All @@ -33,7 +35,7 @@ impl Shell {
}

pub fn screen(&self, cols: u16) -> Result<ShellScreen, Error> {
if !self.has_pty {
if self.has_pty.lock().unwrap().eq(&false) {
return Err(Error::Unsupported);
}
let guard = self.parser.lock().unwrap();
Expand Down Expand Up @@ -72,7 +74,8 @@ impl Shell {
return ShellInfo {
token: self.token.clone(),
title: self.title(),
has_pty: self.has_pty,
ready: self.sender.lock().unwrap().is_some(),
has_pty: *self.has_pty.lock().unwrap(),
created_at: self.created_at,
};
}
Expand All @@ -88,7 +91,7 @@ impl Shell {
token: ShellToken::new(),
created_at: Instant::now(),
device,
has_pty,
has_pty: Mutex::new(false),
sender: Mutex::default(),
callback: Mutex::new(None),
parser: Mutex::new(Parser::new(rows, cols, 1000)),
Expand All @@ -99,7 +102,7 @@ impl Shell {
}

fn process(&self, data: &[u8]) -> bool {
if !self.has_pty {
if self.has_pty.lock().unwrap().eq(&false) {
return false;
}
let mut parser = self.parser.lock().unwrap();
Expand Down Expand Up @@ -128,13 +131,20 @@ impl Shell {

fn worker(&self) -> Result<(), Error> {
let (sender, receiver) = channel::<ShellMessage>();
*self.sender.lock().unwrap() = Some(sender);
let connection = DeviceConnection::new(self.device.clone())?;
let channel = connection.new_channel()?;
channel.open_session()?;
let (rows, cols) = self.parser.lock().unwrap().screen().size();
channel.request_pty("xterm", cols as u32, rows as u32)?;
match channel.request_pty("xterm", cols as u32, rows as u32) {
Ok(_) => *self.has_pty.lock().unwrap() = true,
Err(RequestDenied(s)) => log::warn!("Failed to request pty {:?}", s),
e => e?,
}
channel.request_shell()?;
*self.sender.lock().unwrap() = Some(sender);
if let Some(callback) = self.callback.lock().unwrap().as_ref() {
callback.info(self.info());
}
let mut buf = [0; 8192];
while !channel.is_closed() {
if let Ok(msg) = receiver.recv_timeout(Duration::from_micros(1)) {
Expand All @@ -151,6 +161,9 @@ impl Shell {
}
}
let size = channel.read_timeout(&mut buf, false, Some(Duration::from_micros(5)))?;
if size == 0 {
continue;
}
if let Some(callback) = self.callback.lock().unwrap().as_ref() {
callback.rx(&buf[..size]);
}
Expand Down
1 change: 1 addition & 0 deletions src/app/core/services/remote-shell.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type ShellToken = string;
export interface ShellInfo {
token: ShellToken;
title: string;
ready: boolean;
hasPty: boolean;
}

Expand Down
6 changes: 3 additions & 3 deletions src/app/terminal/dumb/dumb.component.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<div class="p-2 pb-3 w-100 h-100 container" #container>
<div class="w-100 text-warning fw-bold small">
<div class="p-2 pb-3 w-100 h-100 container" #container (click)="input.focus()">
<div class="w-100 text-warning fw-bold small" (click)="$event.stopPropagation()">
Interactive terminal is not supported for this connection. Check <a
href="https://github.com/webosbrew/dev-manager-desktop/wiki/About-Dumb-Shell" appExternalLink>here</a> for
more info.
</div>
<div *ngFor="let item of logs">
<div *ngFor="let item of logs" (click)="$event.stopPropagation()">
<div class="d-flex flex-row align-items-baseline">
<label class="mx-1 text-secondary">&gt;</label>
<pre class="w-100 mb-0 user-select-text">{{item.input}}</pre>
Expand Down
10 changes: 6 additions & 4 deletions src/app/terminal/dumb/dumb.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Component, ElementRef, Input, NgZone, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {RemoteShellService, ShellObservable, ShellSubject, ShellToken} from "../../core/services/remote-shell.service";
import {Component, ElementRef, Input, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {RemoteShellService, ShellObservable, ShellToken} from "../../core/services/remote-shell.service";
import {Subscription} from "rxjs";
import {Buffer} from "buffer";

Expand Down Expand Up @@ -72,8 +72,10 @@ export class DumbComponent implements OnInit, OnDestroy {
str = str.substring(0, end);
}
lastLog.output = `${lastLog.output}${str}`;
const container = this.container!.nativeElement;
container.scrollTop = container.scrollHeight;
setTimeout(() => {
const container = this.container!.nativeElement;
container.scrollTop = container.scrollHeight;
}, 10);
}
}

Expand Down
28 changes: 18 additions & 10 deletions src/app/terminal/terminal.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@
[animation]="false" [(activeId)]="currentShell">
<li *ngFor="let shell of shells; trackBy:shellTracker" [ngbNavItem]="shell.token" [destroyOnHide]="false">
<a class="text-nowrap" ngbNavLink>
<div class="spinner-border spinner-border-sm text-primary" role="status" *ngIf="!shell.ready">
<span class="visually-hidden">Connecting...</span>
</div>
{{shell.title}}
<span class="close position-relative ps-2 font-weight-light"
(click)="closeTab(shell.token);$event.preventDefault();$event.stopImmediatePropagation();">×</span>
</a>
<ng-template ngbNavContent [ngSwitch]="shell.hasPty">
<ng-container *ngSwitchCase="true">
<app-terminal-pty class="terminal-tab-page" [token]="shell.token" [size]="termSize"
*ngIf="termSize"></app-terminal-pty>
<div class="terminal-resize" *ngIf="pendingResize">
{{pendingResize.rows}} &times; {{pendingResize.cols}}
</div>
<ng-template ngbNavContent [ngSwitch]="shell.ready">
<ng-container *ngSwitchCase="true" [ngSwitch]="shell.hasPty">
<ng-container *ngSwitchCase="true">
<app-terminal-pty class="terminal-tab-page" [token]="shell.token" [size]="termSize"
*ngIf="termSize"></app-terminal-pty>
<div class="terminal-resize" *ngIf="pendingResize">
{{pendingResize.rows}} &times; {{pendingResize.cols}}
</div>
</ng-container>
<app-terminal-dumb class="terminal-tab-page" [token]="shell.token" *ngSwitchDefault></app-terminal-dumb>
</ng-container>
<app-terminal-dumb class="terminal-tab-page" [token]="shell.token" *ngSwitchDefault></app-terminal-dumb>
<div class="terminal-tab-page p-1" *ngSwitchDefault>
Connecting to {{shell.title}}...
</div>
</ng-template>
</li>
<li>
Expand All @@ -34,8 +42,8 @@
<div class="dropdown-menu" ngbDropdownMenu>
<button ngbDropdownItem *ngFor="let device of (deviceManager.devices$ | async)"
(click)="newTab(device)">{{device.name}}</button>
<!-- <div class="dropdown-divider"></div>-->
<!-- <button ngbDropdownItem>Open in system terminal...<i class="bi bi-box-arrow-up-right ms-2"></i></button>-->
<!-- <div class="dropdown-divider"></div>-->
<!-- <button ngbDropdownItem>Open in system terminal...<i class="bi bi-box-arrow-up-right ms-2"></i></button>-->
</div>
</div>
</div>
Expand Down

0 comments on commit 069f858

Please sign in to comment.