forked from oleq/nodeprompt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodeprompt.js
executable file
·186 lines (152 loc) · 4.87 KB
/
nodeprompt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env node
'use strict';
var args = require( 'optimist' ).argv,
styles = require( './styles.js' )( !args.raw ),
configDefault = require( './config.default.js' ),
configUser = {};
try {
configUser = require( './config.js' );
} catch ( e ) {}
var NODEPROMPT = {
/**
* Re-shapes given `path` according to `config#pathLength`,
* and stores it in `data` object.
*
* @param {Object} data
* @param {String} path
* @param {String} home
*/
getPath: function( data, path, home ) {
var homeRegex = new RegExp( '^' + home ),
inHome = path.match( homeRegex );
if ( inHome )
path = path.replace( homeRegex, '' );
path = path.split( '/' );
if ( !path[ 0 ] )
path = path.slice( 1 );
if ( !path[ path.length - 1 ] )
path = path.slice( 0, -1 );
if ( path.length > this.config.pathLength ) {
path = path.slice( -this.config.pathLength );
path[ 0 ] = '...' + path[ 0 ];
}
path = path.join( '/' );
if ( inHome )
path = '~' + ( path.length === 0 ? '' : '/' ) + path;
else
path = '/' + path;
data.path = path;
},
/**
* Parses `git status --porcelain` output into
* `data` object format.
*
* @param {Object} data
* @param {String} status
*/
getStatus: function( data, status ) {
var statusArray = status.split( /\n|\r/ ),
branchLine = statusArray.shift().slice( 3 );
data.modified = data.added = data.untracked = 0;
if ( !data.detached && !data.init ) {
var ahead = ( /\[ahead (\d+)/g ).exec( branchLine ),
behind = ( /behind (\d+)\]/g ).exec( branchLine );
data.ahead = ahead ? parseInt( ahead[ 1 ], 10 ) : 0;
data.behind = behind ? parseInt( behind[ 1 ], 10 ) : 0;
data.branch = status && ( /^(.+?)(?:(?=\.{2}| )|$)/g ).exec( branchLine )[ 1 ];
}
// See: http://git-scm.com/docs/git-status.html
// For paths with merge conflicts, X and Y show the modification states of each side of the merge.
// For paths that do not have merge conflicts, X shows the status of the index, and Y shows the
// status of the work tree.
// For untracked paths, XY are ??.
// Other status codes can be interpreted as follows:
// -------------------------------------------------
// X Y Meaning
// -------------------------------------------------
// [MD] not updated
// M [ MD] updated in index
// A [ MD] added to index
// D [ M] deleted from index
// R [ MD] renamed in index
// C [ MD] copied in index
// [MARC] index and work tree matches
// [ MARC] M work tree changed since index
// [ MARC] D deleted in work tree
// -------------------------------------------------
// D D unmerged, both deleted *
// A U unmerged, added by us *
// U D unmerged, deleted by them
// U A unmerged, added by them *
// D U unmerged, deleted by us
// A A unmerged, both added
// U U unmerged, both modified
// -------------------------------------------------
// ? ? untracked
// ! ! ignored
// -------------------------------------------------
var recordedInIndex = { 'M': 1, 'A': 1, 'D': 1, 'R': 1, 'C': 1 },
modifiedInWorkTree = { 'M': 1, 'A': 1, 'U': 1, 'D': 1 };
statusArray.forEach( function( item ) {
var status = item.slice( 0, 2 );
if ( status == '??' )
data.untracked++;
else {
if ( status[ 0 ] in recordedInIndex )
data.added++;
if ( status[ 1 ] in modifiedInWorkTree )
data.modified++;
}
} );
data.diverged = !!( data.ahead && data.behind );
},
/**
* Returns pretty `PS1` prompt string, using
* available data.
*
* @returns {String}
*/
getPS1: function() {
var data = {
// Check if not in ".git" folder (#7).
git: !!args.git && args.git != '.',
host: args.host,
user: args.user
};
this.getPath( data, process.env.PWD, process.env.HOME );
if ( data.git ) {
data.namerev = args.namerev;
// There's no hash when inited.
data.init = args.hash == 'HEAD';
// .git/HEAD starts with hash when detached.
data.detached = !args.head.match( /^ref: /g ) || args[ 'bisect-log' ];
// .git/MERGE_HEAD is a hash of a branch when merging.
data.merging = args[ 'merge-head' ];
this.getStatus( data, args.status );
data.hash = args.hash.slice( 0, this.config.hashLength );
}
return this.config.template( data, this.styles );
},
/**
* Configuration object. See `config.default.js` file to know more.
*
* @property {Object}
*/
config: ( function() {
for ( var c in configUser )
configDefault[ c ] = configUser[ c ];
return configDefault;
} )(),
/**
* Styles object. See `config.js.tpl` file to know more.
*
* @property {Object}
*/
styles: styles,
// For dev purposes only.
args: args
};
if ( module.parent )
module.exports = NODEPROMPT;
else
process.stdout.write( NODEPROMPT.getPS1() );