Skip to content

Commit 4c06b41

Browse files
committed
1.8.0 - Customizable status bar message format waderyan#5
1 parent 6db2c60 commit 4c06b41

File tree

6 files changed

+99
-22
lines changed

6 files changed

+99
-22
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## 1.8.0 (May 01, 2017)
4+
5+
* Feature: Customizable status bar message format [#5](https://github.com/Sertion/vscode-gitblame/issues/5)
6+
* Feature: Customizable `infoMessage` format
7+
* Enhancement: Updating installation instructions
8+
39
## 1.7.1 (April 30, 2017)
410

511
* Enhancement: Use the same cache for `showMessage` and `view.refresh`

README.md

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,48 @@ See Git Blame information in the status bar for the currently selected line.
66

77
# Install
88

9-
Open up VS Code.
10-
11-
1. Press `F1`
12-
2. Type `ext` in command palette
13-
3. Select "install" and hit enter
14-
4. Type `blame`
15-
5. Select "Git Blame" extension and hit enter
9+
1. Open _Visual Studio Code_
10+
1. Press `Ctrl+Shift+X` or `⇧⌘X`
11+
1. Type `blame`
12+
1. Click install on _Git Blame_
1613

1714
# Configuration
1815

1916
- `gitblame.commitUrl` (`string`, default `""`)
2017
- url where you can see the commit by hash
21-
- Available tokens:
18+
- available tokens:
2219
- `${hash}` - the commit hash
2320
- _Example:_ `https://github.com/Sertion/vscode-gitblame/commit/${hash}`
2421
- `gitblame.ignoreWhitespace` (`boolean`, default `false`)
2522
- use the git blame `-w` flag
23+
- `gitblame.infoMessageFormat` (`string`, default `"${commit.hash} ${commit.summary}"`)
24+
- message that appears when the `extension.blame` command executes (when you click the status bar message)
25+
- available tokens:
26+
- `${commit.hash}` - 40-bit hash unique to the commit
27+
- `${commit.summary}` - the first line of the commit message
28+
- `${commit.filename}` - the file name where the line was committed
29+
- `${author.name}` - the commit author's name
30+
- `${author.email}` - the commit author's e-mail
31+
- `${author.timestamp}` - timestamp for the commit author's commit
32+
- `${author.tz}` - the commit author's time zone
33+
- `${committer.name}` - the committer's name
34+
- `${committer.email}` - the committer's e-mail
35+
- `${committer.timestamp}` - timestamp for the committer's commit
36+
- `${committer.tz}` - the committer's time zone
37+
- `${time.ago}` - displays an estimation of how long ago the author committed (e.g. `10 hours ago`, `20 days ago`, `4 months ago`)
38+
- `${time.custom,your_format}` - custom time format based on [momentjs.format(your_format)](https://momentjs.com/docs/#/displaying/format/) (uses author timestamp)
39+
- `${time.from}` - format based on [momentjs.fromNow()](https://momentjs.com/docs/#/displaying/fromnow/) (uses author timestamp)
40+
- `${time.c_ago}` - displays an estimation of how long ago the committer committed (e.g. `10 hours ago`, `20 days ago`, `4 months ago`)
41+
- `${time.c_custom,your_format}` - custom time format based on [momentjs.format(your_format)](https://momentjs.com/docs/#/displaying/format/) (uses committer timestamp)
42+
- `${time.c_from}` - format based on [momentjs.fromNow()](https://momentjs.com/docs/#/displaying/fromnow/) (uses committer timestamp)
43+
- `gitblame.statusBarMessageFormat` (`string`, default `"Blame ${author.name} ( ${time.ago} )"`)
44+
- message in the status bar about the current line's git blame commit
45+
- available tokens:
46+
- See `gitblame.infoMessageFormat`
47+
- `gitblame.statusBarMessageNoCommit` (`string`, default `"Not Committed Yet"`)
48+
- message in the status bar about the current line when no commit can be found
49+
- available tokens:
50+
- _No available tokens_
2651

2752
# [Planned Features](https://github.com/Sertion/vscode-gitblame/labels/Planned)
2853

package.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "gitblame",
33
"displayName": "Git Blame",
44
"description": "See git blame information in the status bar.",
5-
"version": "1.7.1",
5+
"version": "1.8.0",
66
"publisher": "waderyan",
77
"engines": {
88
"vscode": "^1.10.0"
@@ -72,6 +72,21 @@
7272
"type": "string",
7373
"default": "",
7474
"description": "The link to an online tool to view a commit (use ${hash} for the commit hash)"
75+
},
76+
"gitblame.statusBarMessageNoCommit": {
77+
"type": "string",
78+
"default": "Not Committed Yet",
79+
"description": "Customize the status bar message"
80+
},
81+
"gitblame.statusBarMessageFormat": {
82+
"type": "string",
83+
"default": "Blame ${author.name} ( ${time.ago} )",
84+
"description": "Customize the status bar message"
85+
},
86+
"gitblame.infoMessageFormat": {
87+
"type": "string",
88+
"default": "${commit.hash} ${commit.summary}",
89+
"description": "Customize the info message"
7590
}
7691
}
7792
}

src/extension.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ function showMessage(context: ExtensionContext, repoDir: string) {
5656
const viewOnlineTitle = 'View';
5757
const config = workspace.getConfiguration('gitblame');
5858
const commitUrl = <string>config.get('commitUrl');
59+
const messageFormat = <string>config.get('infoMessageFormat');
5960

6061
fs.access(repoPath, (err) => {
6162
if (err) {
@@ -85,11 +86,12 @@ function showMessage(context: ExtensionContext, repoDir: string) {
8586

8687
const hash = info['lines'][lineNumber]['hash'];
8788
const commitInfo = info['commits'][hash];
89+
let normalizedCommitInfo = TextDecorator.normalizeCommitInfoTokens(commitInfo);
8890
let infoMessageArguments = [];
8991
let urlToUse = null;
9092

9193
// Add the message
92-
infoMessageArguments.push(hash + ' ' + commitInfo['summary']);
94+
infoMessageArguments.push(TextDecorator.parseTokens(messageFormat, normalizedCommitInfo));
9395

9496
if (commitUrl) {
9597
// If we have a commitUrl we parse it and add it

src/textdecorator.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1+
import {workspace} from 'vscode';
12
import * as moment from 'moment';
23
import * as ObjectPath from 'object-path';
34

45
export class TextDecorator {
56

67
static toTextView(commit: Object) : string {
7-
const dateNow = new Date();
8-
const author = commit['author'];
9-
const dateText = TextDecorator.toDateText(dateNow, new Date(author['timestamp'] * 1000));
8+
const config = workspace.getConfiguration('gitblame');
109

1110
if (commit['hash'] === '0000000000000000000000000000000000000000') {
12-
return author['name'];
11+
return <string>config.get('statusBarMessageNoCommit');
1312
}
1413
else {
15-
return 'Blame ' + author['name'] + ' ( ' + dateText + ' )';
14+
let normalizedCommitInfo = TextDecorator.normalizeCommitInfoTokens(commit);
15+
let messageFormat = <string>config.get('statusBarMessageFormat');
16+
return TextDecorator.parseTokens(messageFormat, normalizedCommitInfo);
1617
}
1718
}
1819

@@ -44,7 +45,7 @@ export class TextDecorator {
4445
}
4546

4647
static parseTokens(target: string, tokens: object = {}): string {
47-
const tokenRegex = /\$\{([a-z\.\-]{1,})[,]*(|[a-z\-,]{1,})}/gi;
48+
const tokenRegex = /\$\{([a-z\.\-]{1,})[,]*(|.{1,}?)(?=\})}/gi;
4849

4950
return target.replace(tokenRegex, (string: string, key: string, value: string): string => {
5051
let currentToken = ObjectPath.get(tokens, key)
@@ -56,8 +57,7 @@ export class TextDecorator {
5657
return currentToken.toString();
5758
}
5859
else if (typeof currentToken === 'function') {
59-
let values = value.split(',');
60-
let newString = currentToken.call(this, key, values);
60+
let newString = currentToken.call(this, value, key);
6161

6262
if (typeof newString === 'string') {
6363
return newString;
@@ -70,4 +70,27 @@ export class TextDecorator {
7070
return key;
7171
});
7272
}
73+
74+
static normalizeCommitInfoTokens(commitInfo) {
75+
const now = new Date();
76+
const authorTime = moment.unix(commitInfo.author.timestamp);
77+
const committerTime = moment.unix(commitInfo.committer.timestamp);
78+
return {
79+
'commit': {
80+
'hash': commitInfo.hash,
81+
'summary': commitInfo.summary,
82+
'filename': commitInfo.filename
83+
},
84+
'author': commitInfo.author,
85+
'committer': commitInfo.committer,
86+
'time': {
87+
'ago': () => TextDecorator.toDateText(now, authorTime.toDate()),
88+
'from': () => authorTime.fromNow(),
89+
'custom': (momentFormat) => authorTime.format(momentFormat),
90+
'c_ago': () => TextDecorator.toDateText(now, committerTime.toDate()),
91+
'c_from': () => committerTime.fromNow(),
92+
'c_custom': (momentFormat) => committerTime.format(momentFormat)
93+
}
94+
}
95+
}
7396
}

test/extension.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,25 @@ suite('GitBlame Tests', () => {
3030
'replace-word': () => 'replaced'
3131
}));
3232
assert.equal('Function value tested', TextDecorator.parseTokens('Function value ${replace,test}', {
33-
'replace': (key, value) => value + 'ed'
33+
'replace': (value) => value + 'ed'
3434
}));
3535
assert.equal('Multiple mixed replacers', TextDecorator.parseTokens('Multiple ${type} ${what,replacer}', {
3636
'type': 'mixed',
37-
'what': (key, value) => value + 's'
37+
'what': (value) => value + 's'
3838
}));
39-
assert.equal('Should set to key if array', TextDecorator.parseTokens('Should set to key if ${array}', {
40-
'array': []
39+
assert.equal('Multiple of the same replacer should yield the same result', TextDecorator.parseTokens('Multiple of the ${replace} replacer should yield the ${replace} result', {
40+
'replace': 'same'
41+
}));
42+
assert.equal('Should set to key if non-valid-value', TextDecorator.parseTokens('Should set to key if ${non-valid-value}', {
43+
'non-valid-value': []
4144
}));
4245
assert.equal('Uses path', TextDecorator.parseTokens('Uses ${climb.down}', {
4346
'climb': {
4447
'down': 'path'
4548
}
4649
}));
50+
assert.equal('😃 should 💦 👌💯👌', TextDecorator.parseTokens('😃 should 💦 ${ok,💯}', {
51+
'ok': (value) => '👌' + value + '👌'
52+
}));
4753
})
4854
});

0 commit comments

Comments
 (0)