Skip to content

Commit cce283d

Browse files
committed
Branch list & checkout
1 parent cedb665 commit cce283d

File tree

7 files changed

+188
-12
lines changed

7 files changed

+188
-12
lines changed

controller/branch.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
var _ = require('lodash');
2+
3+
var BranchView = require('../view/branch');
4+
5+
var parent = null,
6+
view = null;
7+
8+
var branch = {
9+
loadBranches: function () {
10+
return parent.git.loadBranches()
11+
.map(function (branchName, index) {
12+
if (index === parent.git.currentBranchIndex) {
13+
return " * " + branchName;
14+
} else {
15+
return " " + branchName;
16+
}
17+
});
18+
},
19+
20+
show: function () {
21+
view.list.setItems(branch.loadBranches());
22+
view.list.select(parent.git.currentBranchIndex);
23+
24+
view.layout.show();
25+
view.list.focus();
26+
view.layout.parent.render();
27+
},
28+
29+
hide: function (reload) {
30+
view.layout.hide();
31+
parent.show(reload);
32+
},
33+
34+
init: function (delegate) {
35+
parent = delegate;
36+
37+
view = BranchView(parent.screen);
38+
39+
view.list.key(['enter'], function () {
40+
try {
41+
parent.git.checkout(this.selected);
42+
branch.hide(true);
43+
} catch (e) {
44+
branch.hide(true);
45+
parent.showPopup(_.trim(e.stderr.toString()), 3);
46+
}
47+
});
48+
49+
view.list.key(['escape'], function () {
50+
branch.hide();
51+
});
52+
}
53+
};
54+
55+
56+
module.exports = branch;
57+

controller/main.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var Git = require('../model/git'),
44
view = require('../view/main');
55

66
var editor = require('./editor'),
7+
branch = require('./branch'),
78
log = require('./log'),
89
diff = require('./diff');
910

@@ -148,12 +149,12 @@ var main = {
148149
this.setItem(this.selected, " " + content.substr(3));
149150
},
150151

151-
showPopup: function (msg) {
152-
view.popup.content = "{center}" + msg + "{/center}";
152+
showPopup: function (msg, hideAfter) {
153+
view.popup.content = msg;
153154
view.popup.hidden = false;
154155
redraw();
155156

156-
setTimeout(main.hidePopup, 1000);
157+
setTimeout(main.hidePopup, hideAfter ? hideAfter : 1000);
157158
},
158159

159160
hidePopup: function () {
@@ -171,6 +172,9 @@ log.init(main);
171172
// bind diff viewer
172173
diff.init(main);
173174

175+
// bind branch viewer
176+
branch.init(main);
177+
174178
// bind keys
175179
// TODO: Need to refactor
176180
_.each(view.list, function (elem) {
@@ -221,8 +225,6 @@ _.each(view.list, function (elem) {
221225
main.show(log);
222226
});
223227

224-
//elem.unkey(['C-d']);
225-
226228
elem.key(['C-d'], function () {
227229
if (this.getItem(this.selected) === undefined) {
228230
main.showPopup("There is no diff file you selected");
@@ -232,6 +234,10 @@ _.each(view.list, function (elem) {
232234
main.show(diff);
233235
});
234236

237+
elem.key(['C-b'], function () {
238+
main.show(branch);
239+
});
240+
235241
elem.key(['tab'], function () {
236242
if (view.list.staged.interactive) {
237243
main.moveToUnstaged();

model/git.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ Git.prototype.loadBranches = function () {
216216

217217
var stdout = execSync(gitCommand);
218218

219-
this.branches = stdout.toString().split('\n')
219+
this.branches = _.trim(stdout.toString()).split('\n')
220220
.map(function (line, index) {
221221
if (line.charAt(0) === '*') {
222222
self.currentBranchIndex = index;
@@ -232,4 +232,14 @@ Git.prototype.getCurrentBranchName = function () {
232232
return this.branches[this.currentBranchIndex];
233233
};
234234

235+
Git.prototype.checkout = function (branchIndex) {
236+
var gitCommand = gitExec + ' checkout ' + this.branches[branchIndex];
237+
238+
var stdout = execSync(gitCommand, {stdio: [0, 1, 'pipe']});
239+
240+
this.currentBranchIndex = branchIndex;
241+
242+
return this.currentBranchIndex;
243+
};
244+
235245
module.exports = Git;

view/branch.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var blessed = require('blessed'),
2+
styles = require('./style/branch.json');
3+
4+
var layout = null,
5+
list = null,
6+
menubar = null;
7+
8+
var init = function (screen) {
9+
styles.layout.parent = screen;
10+
11+
layout = blessed.layout(styles.layout);
12+
13+
styles.list.parent = layout;
14+
styles.menubar.parent = layout;
15+
16+
list = blessed.list(styles.list);
17+
menubar = blessed.listbar(styles.menubar);
18+
19+
return {
20+
layout : layout,
21+
list : list,
22+
menubar: menubar
23+
};
24+
};
25+
26+
module.exports = init;

view/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ blessed.listbar(styles.menubar2);
5252

5353
var loading = blessed.loading(styles.loading);
5454

55-
var popup = blessed.box(styles.popup);
55+
var popup = blessed.text(styles.popup);
5656

5757
module.exports = {
5858
screen : screen,

view/style/branch.json

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"layout": {
3+
"hidden": true,
4+
"top": "center",
5+
"left": "center",
6+
"width": "50%",
7+
"height": "50%"
8+
},
9+
"list": {
10+
"top": "top",
11+
"left": "left",
12+
"width": "100%",
13+
"height": "100%-4",
14+
"data": null,
15+
"border": "line",
16+
"align": "left",
17+
"vi": true,
18+
"keys": true,
19+
"style": {
20+
"border": {
21+
"fg": "white"
22+
},
23+
"selected": {
24+
"bg": "blue"
25+
}
26+
}
27+
},
28+
"menubar": {
29+
"align": "center",
30+
"bottom": 0,
31+
"width": "100%",
32+
"height": 3,
33+
"border": "line",
34+
"mouse": true,
35+
"vi": true,
36+
"keys": true,
37+
"style": {
38+
"prefix": {
39+
"fg": "white"
40+
},
41+
"item": {
42+
"fg": "cyan"
43+
},
44+
"selected": {
45+
"fg": "cyan"
46+
}
47+
},
48+
"commands": {
49+
"CHECKOUT": {
50+
"keys": [
51+
"ENTER"
52+
]
53+
},
54+
"ADD": {
55+
"keys": [
56+
"C-a"
57+
]
58+
},
59+
"DEL": {
60+
"keys": [
61+
"C-d"
62+
]
63+
}
64+
}
65+
}
66+
}

view/style/main.json

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,23 @@
159159
"C-l"
160160
]
161161
},
162-
"DIFF": {
162+
"DIFF ": {
163163
"keys": [
164164
"C-d"
165165
]
166+
},
167+
"BRANCH": {
168+
"keys": [
169+
"C-b"
170+
]
166171
}
167172
}
168173
},
169174
"loading": {
170175
"top": "center",
171176
"left": "center",
172-
"height": 5,
173177
"align": "center",
178+
"height": 5,
174179
"width": "50%",
175180
"tags": true,
176181
"hidden": true,
@@ -179,12 +184,18 @@
179184
"popup": {
180185
"top": "center",
181186
"left": "center",
182-
"height": 3,
183-
"align": "center",
184-
"width": "50%",
187+
"align": "left",
188+
"height": "shrink",
189+
"width": "shrink",
185190
"tags": true,
186191
"hidden": true,
187192
"border": "line",
193+
"padding": {
194+
"top": 1,
195+
"bottom": 1,
196+
"left": 3,
197+
"right": 3
198+
},
188199
"style": {
189200
"border": {
190201
"fg": "red"

0 commit comments

Comments
 (0)