Skip to content

Commit

Permalink
Add colours for files in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
krassowski committed Feb 25, 2021
1 parent 3e08ea2 commit 13377aa
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 8 deletions.
61 changes: 61 additions & 0 deletions src/browserDecorations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Git, IGitExtension } from './tokens';
import * as fileStyle from './style/BrowserFile';
import { DirListing, FileBrowser } from '@jupyterlab/filebrowser';
import { Contents } from '@jupyterlab/services';
import { DocumentRegistry } from '@jupyterlab/docregistry';
import { ITranslator } from '@jupyterlab/translation';

const statusStyles: Map<Git.StatusCode, string> = new Map([
['M', fileStyle.modified],
['A', fileStyle.added],
['D', fileStyle.deleted],
['R', fileStyle.modified],
['C', fileStyle.modified],
['U', fileStyle.modified],
['?', fileStyle.untracked],
['!', fileStyle.ignored]
]);

class GitListingRenderer extends DirListing.Renderer {
constructor(private gitExtension: IGitExtension) {
super();
}

updateItemNode(
node: HTMLElement,
model: Contents.IModel,
fileType?: DocumentRegistry.IFileType,
translator?: ITranslator
) {
super.updateItemNode(node, model, fileType, translator);
const file = this.gitExtension.getFile(model.path);
console.log(model.path, 'file status', file?.status);
let status_code: Git.StatusCode = null;
if (file) {
status_code = file.status === 'staged' ? file.x : file.y;
console.log(model.path, 'file x, y', file.x, file.y);
console.log(model.path, 'file status code', status_code);
}

for (const [otherStatus, className] of statusStyles.entries()) {
if (status_code === otherStatus) {
node.classList.add(className);
} else {
node.classList.remove(className);
}
}
}
}

export function substituteListingRenderer(
extension: IGitExtension,
fileBrowser: FileBrowser
): void {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const listing: DirListing = fileBrowser._listing;
const renderer = new GitListingRenderer(extension);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
listing._renderer = renderer;
}
10 changes: 5 additions & 5 deletions src/components/FileItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import {
import { Git } from '../tokens';
import { FilePath } from './FilePath';

// Git status codes https://git-scm.com/docs/git-status
export const STATUS_CODES = {
export const STATUS_CODES: Record<Git.StatusCode, string> = {
M: 'Modified',
A: 'Added',
D: 'Deleted',
R: 'Renamed',
C: 'Copied',
U: 'Updated',
'?': 'Untracked',
'!': 'Ignored'
'!': 'Ignored',
' ': 'Unchanged'
};

/**
Expand Down Expand Up @@ -124,7 +124,7 @@ export interface IFileItemProps {
}

export class FileItem extends React.PureComponent<IFileItemProps> {
protected _getFileChangedLabel(change: keyof typeof STATUS_CODES): string {
protected _getFileChangedLabel(change: Git.StatusCode): string {
return STATUS_CODES[change];
}

Expand Down Expand Up @@ -157,7 +157,7 @@ export class FileItem extends React.PureComponent<IFileItemProps> {
render(): JSX.Element {
const { file } = this.props;
const status_code = file.status === 'staged' ? file.x : file.y;
const status = this._getFileChangedLabel(status_code as any);
const status = this._getFileChangedLabel(status_code);

return (
<div
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { GitWidget } from './widgets/GitWidget';
import { addStatusBarWidget } from './widgets/StatusWidget';

export { Git, IGitExtension } from './tokens';
import { substituteListingRenderer } from './browserDecorations';

/**
* The default running sessions extension.
Expand Down Expand Up @@ -181,6 +182,8 @@ async function activate(
app.commands,
app.contextMenu
);

substituteListingRenderer(gitExtension, factory.defaultBrowser);
}

return gitExtension;
Expand Down
64 changes: 64 additions & 0 deletions src/style/BrowserFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { style } from 'typestyle';

export const modified = style({
$nest: {
'&:not(.jp-mod-selected)': {
$nest: {
'.jp-DirListing-itemText': {
color: 'var(--md-blue-700)'
}
}
}
}
});

export const untracked = style({
$nest: {
'&:not(.jp-mod-selected)': {
$nest: {
'.jp-DirListing-itemText': {
color: 'var(--md-red-700)'
}
}
}
}
});

export const added = style({
$nest: {
'&:not(.jp-mod-selected)': {
$nest: {
'.jp-DirListing-itemText': {
color: 'var(--md-green-700)'
}
}
}
}
});

export const ignored = style({
$nest: {
'&:not(.jp-mod-selected)': {
$nest: {
'.jp-DirListing-itemText': {
color: 'var(--md-grey-700)'
}
}
}
}
});

export const deleted = style({
$nest: {
'&:not(.jp-mod-selected)': {
$nest: {
'.jp-DirListing-itemText': {
color: 'var(--md-grey-700)'
}
}
},
'.jp-DirListing-itemText': {
textDecoration: 'line-through'
}
}
});
9 changes: 7 additions & 2 deletions src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,8 @@ export namespace Git {
* has the status of each changed file
*/
export interface IStatusFileResult {
x: string;
y: string;
x: StatusCode;
y: StatusCode;
to: string;
from: string;
is_binary: boolean | null;
Expand Down Expand Up @@ -750,6 +750,11 @@ export namespace Git {
| 'partially-staged'
| null;

/**
* Git status codes https://git-scm.com/docs/git-status
*/
export type StatusCode = 'M' | 'A' | 'D' | 'R' | 'C' | 'U' | '?' | '!' | ' ';

export interface ITagResult {
code: number;
message?: string;
Expand Down
2 changes: 1 addition & 1 deletion tests/test-components/FileItem.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('FileItem', () => {
const props: IFileItemProps = {
contextMenu: () => {},
file: {
x: '',
x: ' ',
y: 'M',
to: 'some/file/path/file-name',
from: '',
Expand Down

0 comments on commit 13377aa

Please sign in to comment.