From 70ded6f8c1853e6a3b5d9d401200ff7f65a83c39 Mon Sep 17 00:00:00 2001 From: Sheehan Date: Mon, 31 Oct 2016 01:01:12 -0700 Subject: [PATCH] Implemented Issue 715's function (#840) * Implemented Issue 715's function - status bar will show file change status - fix a bug all git directory treated as dirty * Implemented Issue 715's function - status bar will show file change status - fix a bug all git directory treated as dirty * fix small bug related to file changes count * only show changed git status on bottom right * Remove console.log --- src/plugins/GitWatcher.ts | 39 +++++++++++++++++++++++++++++++- src/views/StatusBarComponent.tsx | 12 ++++++++++ src/views/css/main.ts | 2 ++ typings/Interfaces.d.ts | 14 +++++++++++- 4 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/plugins/GitWatcher.ts b/src/plugins/GitWatcher.ts index 901c6e90f..c64e953c6 100644 --- a/src/plugins/GitWatcher.ts +++ b/src/plugins/GitWatcher.ts @@ -56,7 +56,7 @@ class GitWatcher extends EventEmitter { private async updateGitData() { executeCommand("git", ["status", "-b", "--porcelain"], this.directory).then(changes => { - const status: VcsStatus = changes.length ? "dirty" : "clean"; + const status: VcsStatus = (changes.split("\n").length > 2) ? "dirty" : "clean"; let head: string = changes.split(" ")[1]; let push: string = "0"; let pull: string = "0"; @@ -86,18 +86,55 @@ class GitWatcher extends EventEmitter { } } + let fileChanges = linesToFileChanges(changes); + const data: VcsData = { kind: "repository", branch: head, push: push, pull: pull, status: status, + changes: fileChanges, }; this.emit(GIT_WATCHER_EVENT_NAME, data); }); } } +function linesToFileChanges(lines: string): FileChanges { + let stagedChanges = new Map([["+", 0], ["~", 0], ["-", 0], ["!", 0]]); + let unstagedChanges = new Map([["+", 0], ["~", 0], ["-", 0], ["!", 0]]); + lines.split("\n").slice(1).forEach((line) => { + switch (line[0]) { + case "A": stagedChanges.set("+", stagedChanges.get("+") + 1); break; + case "M": + case "R": + case "C": stagedChanges.set("~", stagedChanges.get("~") + 1); break; + case "D": stagedChanges.set("-", stagedChanges.get("-") + 1); break; + case "U": stagedChanges.set("!", stagedChanges.get("!") + 1); break; + default: break; + } + + switch (line[1]) { + case "?": + case "A": unstagedChanges.set("+", stagedChanges.get("+") + 1); break; + case "M": unstagedChanges.set("~", stagedChanges.get("~") + 1); break; + case "D": unstagedChanges.set("-", stagedChanges.get("-") + 1); break; + case "U": unstagedChanges.set("!", stagedChanges.get("!") + 1); break; + default: break; + } + }); + let stagedResult: string = [...stagedChanges] + .filter((pair) => (pair[1] !== 0)) + .map(([key, v]) => (key + String(v) + " ")) + .reduce((left, right) => (left + right), ""); + let unstagedResult: string = [...unstagedChanges] + .filter((pair) => (pair[1] !== 0)) + .map(([key, v]) => key + String(v) + " ") + .reduce((left, right) => left + right, ""); + return {stagedChanges: stagedResult, unstagedChanges: unstagedResult}; +} + interface WatchesValue { listeners: Set; diff --git a/src/views/StatusBarComponent.tsx b/src/views/StatusBarComponent.tsx index 60528997b..cd8449189 100644 --- a/src/views/StatusBarComponent.tsx +++ b/src/views/StatusBarComponent.tsx @@ -15,6 +15,18 @@ const VcsDataComponent = ({data}: { data: VcsData }) => { return (
+ {(data.status === "dirty") ? + ( + + {data.changes.stagedChanges} + + {(data.changes.stagedChanges === "") ? "" : "| "} + + {data.changes.unstagedChanges} + + ) : + undefined + } {data.pull} diff --git a/src/views/css/main.ts b/src/views/css/main.ts index dbe7a3a9f..7da904557 100644 --- a/src/views/css/main.ts +++ b/src/views/css/main.ts @@ -254,6 +254,8 @@ export const statusBar = { marginRight: 10, }, icon: Object.assign({}, icon, {marginRight: 5, marginLeft: 5}), + stagedFileChanges: {color: colors.green}, + unstagedFileChanges: {color: colors.red}, status: (status: VcsStatus) => { return { color: status === "dirty" ? colors.blue : colors.white, diff --git a/typings/Interfaces.d.ts b/typings/Interfaces.d.ts index 11e1f4ab8..f9d4407e9 100644 --- a/typings/Interfaces.d.ts +++ b/typings/Interfaces.d.ts @@ -24,7 +24,19 @@ interface PartialRowColumn { type VcsStatus = "dirty" | "clean"; -type VcsData = { kind: "repository", branch: string, push: string, pull: string; status: VcsStatus; } | { kind: "not-repository"; } +type VcsData = { + kind: "repository", + branch: string, + push: string, + pull: string, + changes: FileChanges, + status: VcsStatus; +} | { kind: "not-repository"; } + +interface FileChanges { + stagedChanges: string; + unstagedChanges: string; +} interface Margins { top: number;