Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

working example for my own $PS1

  • Loading branch information...
commit 9e303ace0318b99fe6da9ea696b488405dea84a7 0 parents
James Halliday authored
Showing with 38 additions and 0 deletions.
  1. +3 −0  example/prompt.js
  2. +35 −0 index.js
3  example/prompt.js
@@ -0,0 +1,3 @@
+var decode = require('../');
+var s = decode(process.argv[2], { env: process.env });
+console.log(s);
35 index.js
@@ -0,0 +1,35 @@
+var path = require('path');
+var re = RegExp(
+ '\\\\[adehHjlnrstT@AuvVwW!#$\\\\[\\]]|\\\\[0-7]{3}'
+ + '|\\${[^}]*}',
+ 'g'
+);
+
+module.exports = function (str, opts) {
+ if (!opts) opts = {};
+ if (!opts.env) opts.env = {};
+
+ return str.replace(re, replacer);
+
+ function replacer (s) {
+ var m;
+ if (m = /^\${/.exec(s)) {
+ return opts.env[m[1]] || '';
+ }
+ var x = s.slice(1);
+ if (/^[0-7]{1,3}$/.test(x)) {
+ return String.fromCharCode(parseInt(x, 8));
+ }
+ if (x === 'w') return process.cwd();
+ if (x === 'W') return path.basename(process.cwd());
+
+ var rep = {
+ a: '\x07',
+ e: '\x1b',
+ u: opts.user || opts.env.USER,
+ '[': '',
+ ']': ''
+ }[x];
+ return rep === undefined ? x : rep;
+ }
+};
Please sign in to comment.
Something went wrong with that request. Please try again.