From a87e8d9db1c580b8278a5d04c13f3c827b2fc014 Mon Sep 17 00:00:00 2001 From: Viet-Trung Luu Date: Wed, 11 Mar 2015 14:34:25 -0700 Subject: [PATCH] Add a simple prototype "terminal" example. (Together with an app that echoes stuff from the terminal.) R=erg@chromium.org Review URL: https://codereview.chromium.org/999193002 --- examples/terminal/README.md | 9 ++ examples/terminal/index.sky | 8 ++ examples/terminal/terminal.sky | 115 ++++++++++++++++++++ examples/terminal/terminal_display.dart | 16 +++ examples/terminal/terminal_file_impl.dart | 124 ++++++++++++++++++++++ 5 files changed, 272 insertions(+) create mode 100644 examples/terminal/README.md create mode 100644 examples/terminal/index.sky create mode 100644 examples/terminal/terminal.sky create mode 100644 examples/terminal/terminal_display.dart create mode 100644 examples/terminal/terminal_file_impl.dart diff --git a/examples/terminal/README.md b/examples/terminal/README.md new file mode 100644 index 0000000000..3ac0dd29b8 --- /dev/null +++ b/examples/terminal/README.md @@ -0,0 +1,9 @@ +Terminal +======== + +This is a prototype "terminal" application that can connect to any Mojo +application (providing the |terminal.TerminalClient| interface) and provide +interactive terminal facilities via an implementation of |mojo.files.File|. +I.e., once connected, the application can write to/read from the terminal by +performing the corresponding operations on a "file" (thus replicating +decades-old technology, poorly). diff --git a/examples/terminal/index.sky b/examples/terminal/index.sky new file mode 100644 index 0000000000..74d56221b9 --- /dev/null +++ b/examples/terminal/index.sky @@ -0,0 +1,8 @@ +#!mojo mojo:sky_viewer + + + diff --git a/examples/terminal/terminal.sky b/examples/terminal/terminal.sky new file mode 100644 index 0000000000..03d784c25d --- /dev/null +++ b/examples/terminal/terminal.sky @@ -0,0 +1,115 @@ + + + + + + + diff --git a/examples/terminal/terminal_display.dart b/examples/terminal/terminal_display.dart new file mode 100644 index 0000000000..c932825dcd --- /dev/null +++ b/examples/terminal/terminal_display.dart @@ -0,0 +1,16 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:async'; + +// Interface for a terminal display, able to accept bytes (from the computer) +// and typically displaying them (or possibly handling them as escape codes, +// etc.) and able to get bytes from the "user". +abstract class TerminalDisplay { + void putChar(int byte); + Future getChar(); + + // TODO(vtl): Should probably also have facilities for putting many bytes at a + // time or getting as many bytes as available. +} diff --git a/examples/terminal/terminal_file_impl.dart b/examples/terminal/terminal_file_impl.dart new file mode 100644 index 0000000000..34b48c84fc --- /dev/null +++ b/examples/terminal/terminal_file_impl.dart @@ -0,0 +1,124 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:async'; +import 'dart:core'; +import 'mojo:core'; +import 'package:services/files/file.mojom.dart' as files; +import 'package:services/files/types.mojom.dart' as files; + +import 'terminal_display.dart'; + +// This implements a |mojo.files.File| that acts like a (pseudo)terminal. Bytes +// written to the |File| will be read by this implementation and passed on to +// the (Dart) |TerminalDisplay| (using |putChar()|). A read from the |File| will +// be completed if/when |TerminalDisplay| makes a byte available (via +// |getChar()|). +// TODO(vtl): This implementation is very incomplete. +class TerminalFileImpl implements files.File { + final files.FileStub stub; + final TerminalDisplay _display; + + TerminalFileImpl(this._display) : stub = new files.FileStub.unbound() { + stub.impl = this; + } + + // |files.File| implementation: + + @override + Future close(Function responseFactory) async { + // TODO(vtl): We should probably do more than just say OK. + return responseFactory(files.Error_OK); + } + + @override + Future read(int numBytesToRead, int offset, int whence, + Function responseFactory) async { + if (numBytesToRead < 0) { + return responseFactory(files.Error_INVALID_ARGUMENT, null); + } + + // TODO(vtl): Error if |offset|/|whence| not appropriate. + + if (numBytesToRead == 0) { + return responseFactory(files.Error_OK, []); + } + + return responseFactory(files.Error_OK, [await _display.getChar()]); + } + + @override + Future write(List bytesToWrite, int offset, int whence, + Function responseFactory) async { + // TODO(vtl): Error if |offset|/|whence| not appropriate. + + for (var c in bytesToWrite) { + _display.putChar(c); + } + return responseFactory(files.Error_OK, bytesToWrite.length); + } + + @override + Future readToStream(MojoDataPipeProducer source, int offset, int whence, + int numBytesToRead, Function responseFactory) async { + // TODO(vtl) + return responseFactory(files.Error_UNIMPLEMENTED); + } + + @override + Future writeFromStream(MojoDataPipeConsumer sink, int offset, int whence, + Function responseFactory) async { + // TODO(vtl) + return responseFactory(files.Error_UNIMPLEMENTED); + } + + @override + Future tell(Function responseFactory) async { + // TODO(vtl) + return responseFactory(files.Error_UNIMPLEMENTED, 0); + } + + @override + Future seek(int offset, int whence, Function responseFactory) async { + // TODO(vtl) + return responseFactory(files.Error_UNIMPLEMENTED, 0); + } + + @override + Future stat(Function responseFactory) async { + // TODO(vtl) + return responseFactory(files.Error_UNIMPLEMENTED, null); + } + + @override + Future truncate(int size, Function responseFactory) async { + // TODO(vtl) + return responseFactory(files.Error_UNIMPLEMENTED); + } + + @override + Future touch(files.TimespecOrNow atime, files.TimespecOrNow mtime, + Function responseFactory) async { + // TODO(vtl) + return responseFactory(files.Error_UNIMPLEMENTED); + } + + @override + Future dup(Object file, Function responseFactory) async { + // TODO(vtl) + return responseFactory(files.Error_UNIMPLEMENTED); + } + + @override + Future reopen(Object file, int openFlags, Function responseFactory) async { + // TODO(vtl) + return responseFactory(files.Error_UNIMPLEMENTED); + } + + @override + Future asBuffer(Function responseFactory) async { + // TODO(vtl) + return responseFactory(files.Error_UNIMPLEMENTED, null); + } +}