From 13377aaac4475ffbb0aba4ee46f3fdceeefa5274 Mon Sep 17 00:00:00 2001 From: krassowski Date: Thu, 25 Feb 2021 18:32:27 +0000 Subject: [PATCH] Add colours for files in browser --- src/browserDecorations.ts | 61 +++++++++++++++++++++++ src/components/FileItem.tsx | 10 ++-- src/index.ts | 3 ++ src/style/BrowserFile.ts | 64 +++++++++++++++++++++++++ src/tokens.ts | 9 +++- tests/test-components/FileItem.spec.tsx | 2 +- 6 files changed, 141 insertions(+), 8 deletions(-) create mode 100644 src/browserDecorations.ts create mode 100644 src/style/BrowserFile.ts diff --git a/src/browserDecorations.ts b/src/browserDecorations.ts new file mode 100644 index 000000000..59a176211 --- /dev/null +++ b/src/browserDecorations.ts @@ -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 = 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; +} diff --git a/src/components/FileItem.tsx b/src/components/FileItem.tsx index 8756ff2be..b34811b8b 100644 --- a/src/components/FileItem.tsx +++ b/src/components/FileItem.tsx @@ -13,8 +13,7 @@ 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 = { M: 'Modified', A: 'Added', D: 'Deleted', @@ -22,7 +21,8 @@ export const STATUS_CODES = { C: 'Copied', U: 'Updated', '?': 'Untracked', - '!': 'Ignored' + '!': 'Ignored', + ' ': 'Unchanged' }; /** @@ -124,7 +124,7 @@ export interface IFileItemProps { } export class FileItem extends React.PureComponent { - protected _getFileChangedLabel(change: keyof typeof STATUS_CODES): string { + protected _getFileChangedLabel(change: Git.StatusCode): string { return STATUS_CODES[change]; } @@ -157,7 +157,7 @@ export class FileItem extends React.PureComponent { 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 (
{ const props: IFileItemProps = { contextMenu: () => {}, file: { - x: '', + x: ' ', y: 'M', to: 'some/file/path/file-name', from: '',