summaryrefslogtreecommitdiff
path: root/shell
diff options
context:
space:
mode:
authorJoonas Lahtinen <joonas.lahtinen@linux.intel.com>2018-08-22 17:36:41 +0300
committerJoonas Lahtinen <joonas.lahtinen@linux.intel.com>2018-08-22 17:48:25 +0300
commit5ec509b546e64e18e4e227cf7973766b223552b4 (patch)
treea410f68eed9c0fc6f0a5805a932d290ffc6d3fa9 /shell
parent221855c6fe22abc5c70587482bae3e8c4809eb39 (diff)
igt/shell: Add console.{log,error} and modernize/prettier.io samples.v8-shell
Diffstat (limited to 'shell')
-rw-r--r--shell/.editorconfig9
-rw-r--r--shell/.prettierrc.js4
-rw-r--r--shell/lib/console.js19
-rw-r--r--shell/lib/index.js2
-rw-r--r--shell/lib/util.js35
-rw-r--r--shell/samples/igt.toy.print.js (renamed from shell/samples/print.js)0
-rw-r--r--shell/samples/lib.console.js7
-rw-r--r--shell/samples/os.fork.js88
-rw-r--r--shell/samples/os.sleep.js13
9 files changed, 147 insertions, 30 deletions
diff --git a/shell/.editorconfig b/shell/.editorconfig
new file mode 100644
index 00000000..6a0ac007
--- /dev/null
+++ b/shell/.editorconfig
@@ -0,0 +1,9 @@
+root = true
+
+[*.cc, *.js]
+indent_style = space
+indent_size = 2
+end_of_line = lf
+charset = utf-8
+trim_trailing_whitespace = true
+insert_final_newline = true
diff --git a/shell/.prettierrc.js b/shell/.prettierrc.js
new file mode 100644
index 00000000..de2f53cd
--- /dev/null
+++ b/shell/.prettierrc.js
@@ -0,0 +1,4 @@
+module.exports = {
+ singleQuote: true,
+ trailingComma: 'all',
+};
diff --git a/shell/lib/console.js b/shell/lib/console.js
new file mode 100644
index 00000000..2845dcfe
--- /dev/null
+++ b/shell/lib/console.js
@@ -0,0 +1,19 @@
+import { format } from './util';
+
+class Console {
+ constructor(stdout, stderr) {
+ Object.assign(this, { stdout, stderr });
+ }
+
+ log(...args) {
+ this.stdout(format(...args));
+ }
+
+ error(...args) {
+ this.stdout(format(...args));
+ }
+}
+
+const console = new Console(igt.toy.print, igt.toy.print);
+
+export default console;
diff --git a/shell/lib/index.js b/shell/lib/index.js
new file mode 100644
index 00000000..f36c691d
--- /dev/null
+++ b/shell/lib/index.js
@@ -0,0 +1,2 @@
+export { default as console } from './console';
+export { * as util } from './util';
diff --git a/shell/lib/util.js b/shell/lib/util.js
new file mode 100644
index 00000000..6f87dfd7
--- /dev/null
+++ b/shell/lib/util.js
@@ -0,0 +1,35 @@
+export const inspect = arg => {
+ if (arg === null || typeof arg !== 'object') return arg;
+
+ try {
+ return JSON.stringify(arg, undefined, 2);
+ } catch (error) {
+ return '[Circular]';
+ }
+};
+
+const placeholderRegExp = /%[sdo%]/g;
+const placeholderHandlers = {
+ '%s': arg => String(arg),
+ '%d': arg => Number(arg),
+ '%o': arg => inspect(arg),
+};
+
+export const format = (fmt, ...args) => {
+ if (typeof fmt !== 'string') return args.map(inspect).join(' ');
+
+ const str = String(fmt).replace(placeholderRegExp, placeholder => {
+ if (placeholder === '%%') return '%';
+ if (!args.length) return placeholder;
+ const [arg] = args.splice(0, 1);
+ const handler = placeholderHandlers[placeholder];
+ return handler ? handler(arg) : placeholder;
+ });
+
+ return [str, ...args.map(inspect)].join(' ');
+};
+
+export const range = (start, end) => {
+ const size = end - start;
+ return new Array(size).map((v, i) => start + i);
+};
diff --git a/shell/samples/print.js b/shell/samples/igt.toy.print.js
index dee56dda..dee56dda 100644
--- a/shell/samples/print.js
+++ b/shell/samples/igt.toy.print.js
diff --git a/shell/samples/lib.console.js b/shell/samples/lib.console.js
new file mode 100644
index 00000000..ec4a713b
--- /dev/null
+++ b/shell/samples/lib.console.js
@@ -0,0 +1,7 @@
+import console from '../lib/console';
+
+const number = 1;
+const string = 'foo';
+const object = { a: 1, b: 2 };
+
+console.log('number = %d\nstring = %s\nobject = %o', number, string, object);
diff --git a/shell/samples/os.fork.js b/shell/samples/os.fork.js
index debdebcc..5833fdc4 100644
--- a/shell/samples/os.fork.js
+++ b/shell/samples/os.fork.js
@@ -1,30 +1,66 @@
-igt.os.fork('hello', print).wait();
-
-igt.os.fork([ {delay: 1, msg:'goodbye' }, {delay:0, msg:'world'} ],
- function(x) { igt.os.sleep(x.delay); print(x.msg) }).wait();
-
-var job = igt.os.fork('', function(x) { igt.os.exit(1); igt.os.sleep(999); });
-print('Exitcode = ' + job.wait());
-
-job = igt.os.fork();
-if (job) {
- print("Parent shall wait for its child!");
- job.wait();
-} else {
- print("The child wants to play!");
- for (var x = 0; x < 10; x++) {
- print("Play " + x);
- igt.os.sleep(0.1);
+import console from '../lib/console';
+import { range } from '../lib/util';
+
+const { fork, sleep, exit } = igt.os;
+
+{
+ fork('hello', console.log).wait();
+}
+
+{
+ const children = [
+ {
+ delay: 1,
+ msg: 'goodbye',
+ },
+ {
+ delay: 0,
+ msg: 'world',
+ },
+ ];
+
+ const childrenMain = ({ delay, msg }) => {
+ sleep(delay);
+ console.log(msg);
+ };
+
+ fork(children, childrenMain).wait();
+
+ {
+ const job = fork('', () => {
+ exit(1);
+ sleep(999);
+ });
+ console.log('Exitcode = %d', job.wait());
}
- print("Tired now!");
- igt.os.exit(0);
}
-print("Only parents allowed after children sleep, exitcode:", job.result);
-job = igt.os.fork();
-if (job) {
- print("No Mr Bond, I expect you to", job.wait());
-} else {
- badwolf();
+{
+ const job = fork();
+ if (job) {
+ console.log('Parent shall wait for its child!');
+ job.wait();
+ } else {
+ console.log('The child wants to play!');
+ for (const i of range(0, 10)) {
+ console.log('Play ' + i);
+ sleep(0.1);
+ }
+ console.log('Tired now!');
+ exit(0);
+ }
+ console.log(
+ 'Only parents allowed after children sleep, exitcode: %d',
+ job.result,
+ );
+}
+
+{
+ const job = fork();
+ if (job) {
+ console.log('No Mr Bond, I expect you to', job.wait());
+ } else {
+ badwolf();
+ }
+ console.log('Onwards, all by myself.');
}
-print("Onwards, all by myself.");
diff --git a/shell/samples/os.sleep.js b/shell/samples/os.sleep.js
index 19f6aac9..fd2a4c54 100644
--- a/shell/samples/os.sleep.js
+++ b/shell/samples/os.sleep.js
@@ -1,9 +1,14 @@
+import console from '../lib/console';
+
+// FIXME: Make 'now' into function.
+const { sleep } = igt.os;
+
let elapsed = -igt.os.now;
let expected = 0;
-for (var i = 2; i > 0.1; i /= 2) {
- print("Sleeping for " + i + "s");
- igt.os.sleep(i);
+for (let i = 2; i > 0.1; i /= 2) {
+ console.log('Sleeping for %d seconds', i);
+ sleep(i);
expected += i;
}
elapsed += igt.os.now;
-print("Elapsed " + elapsed + "ms, expected " + expected*1000 + "ms");
+console.log('Elapsed %d ms, expected %s ms', elapsed, expected * 1000);