Skip to content

Commit bcf8b68

Browse files
committed
feat: add configuration of ignored path while generating tree string for directory
1 parent db944ff commit bcf8b68

File tree

14 files changed

+71
-49
lines changed

14 files changed

+71
-49
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"args": [
2626
"--disable-extensions",
2727
"--extensionDevelopmentPath=${workspaceFolder}",
28-
"--extensionTestsPath=${workspaceFolder}/out/test/suite/index"
28+
"--extensionTestsPath=${workspaceFolder}/out/test/suite"
2929
],
3030
"outFiles": ["${workspaceFolder}/out/test/**/*.js"],
3131
"preLaunchTask": "npm: watch"

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,16 @@ In all cases, you can revert formatting tree strings back to their preformatted
5151

5252
### Generate Tree String for Directory
5353

54-
In addition to being able to format pre-formatted tree strings, you may also generate tree strings for directories inside your current workspace (directories appearing in `Explorer` tab). To do so, right-click on any directory within the `Explorer` tab and select the `Generate Tree String for Directory` menu option. An example is shown below.
54+
In addition to being able to format pre-formatted tree strings, you may also generate tree strings for directories inside your current workspace (directories appearing in `Explorer` tab). To do so, right-click on any directory, or empty space, within the `Explorer` tab and select the `Generate Tree String for Directory` menu option. An example is shown below.
5555

5656
![Generate Tree String for Directory](./images/directory.gif)
5757

5858
#### Addendum
5959

6060
The walking process through files is performed asynchronously. Therefore, selecting heavily-nested folders (e.g. `node_modules`) will directly affect performance speed.
6161

62+
By default, `node_modules` and `.git` are ignored while generating tree string for directories. However, this can be customized by setting `asciiTreeGenerator.directoryIgnore` in configurations.
63+
6264
## Configuration
6365

6466
Each tree string character can be defined by its ASCII code representation (UTF character code, more generally). As such, the theorectical range for character codes is `0` to `65535` (two bytes). However, and important to note, is that not every character code is printable and/or may cause formatting issues.

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@
6363
"type": "integer",
6464
"default": 32,
6565
"description": "For blank / space elements (0-65535; Default: 32 ' ')"
66+
},
67+
"asciiTreeGenerator.directoryIgnore": {
68+
"type": "array",
69+
"items": {
70+
"type": "string"
71+
},
72+
"default": ["node_modules", ".git"],
73+
"description": "the glob patterns of ignored path while generate tree string for directory"
6674
}
6775
}
6876
}

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { IVsCodeConfig } from './lib/interface';
33

44
export function getConfig(): IVsCodeConfig {
55
const config: IVsCodeConfig = {
6-
ignore: ['node_modules'],
6+
directoryIgnore: vscode.workspace.getConfiguration().get<string[]>('asciiTreeGenerator.directoryIgnore'),
77
rootCharCode: vscode.workspace
88
.getConfiguration()
99
.get<number>('asciiTreeGenerator.rootElement'),

src/extension.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as path from 'path';
66

77
import { formatFileTreeItemsFromDirectory } from './lib/directory';
88
import { generate } from './lib/generator';
9-
import { getUserEOL, createWebview, revertTreeString } from './utils';
9+
import { getUserEOL, createWebview, revertTreeString, getCharCodesFromConfig, getDirectoryIgnoreFromConfig } from './utils';
1010
import { formatFileTreeItemsFromText } from './lib/text';
1111

1212
export function activate(context: vscode.ExtensionContext) {
@@ -62,11 +62,10 @@ export function activate(context: vscode.ExtensionContext) {
6262
const root =
6363
path.relative(rootWorkspace.uri.fsPath, target.fsPath) || '.';
6464

65-
// Todo: read plugin configuration
6665
const items = await formatFileTreeItemsFromDirectory(target!.fsPath, {
6766
maxDepth: Number.MAX_VALUE,
6867
sort: true,
69-
ignore: [],
68+
ignore: getDirectoryIgnoreFromConfig(),
7069
});
7170
const text = generate(items, {
7271
eol: getUserEOL(),
@@ -102,7 +101,7 @@ export function activate(context: vscode.ExtensionContext) {
102101
const items = formatFileTreeItemsFromText(rawText);
103102
const text = generate(items, {
104103
eol: editor.document.eol === vscode.EndOfLine.CRLF ? '\r\n' : '\n',
105-
// Todo: read plugin configurations
104+
charset: getCharCodesFromConfig(),
106105
});
107106
editor.edit((edit) => {
108107
edit.replace(editor.selection, text);

src/lib/generator.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
import { IFileTreeItem, ICharset, IFormatOptions } from './interface';
2-
import { getUserEOL, getCharCodesFromConfig } from '../utils';
3-
4-
export const defaultCharset: ICharset = {
5-
root: String.fromCharCode(46), // '.',
6-
child: String.fromCharCode(9500), // '├',
7-
last: String.fromCharCode(9492), // '└',
8-
parent: String.fromCharCode(9474), // '|',
9-
dash: String.fromCharCode(9472), // '─',
10-
blank: String.fromCharCode(32), // ' ',
11-
};
1+
import { EOL } from 'os';
2+
import { IFileTreeItem, IFormatOptions } from './interface';
3+
import { defaultCharset } from '../utils';
124

135
function createTreeString(start: string, fill: string, size = 3) {
146
let result = '';
@@ -25,8 +17,8 @@ function createTreeString(start: string, fill: string, size = 3) {
2517
/** generate tree string */
2618
export function generate(items: IFileTreeItem[], options: IFormatOptions = {}) {
2719
const {
28-
eol = getUserEOL(),
29-
charset = getCharCodesFromConfig(),
20+
eol = EOL,
21+
charset = defaultCharset,
3022
fillLeft = true,
3123
} = options;
3224
let leftSpace = '';

src/lib/interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface ICharset {
3636

3737
// the VS code config interface
3838
export interface IVsCodeConfig {
39-
ignore: string[];
39+
directoryIgnore: string[] | undefined;
4040
rootCharCode: number | undefined;
4141
childCharCode: number | undefined;
4242
lastCharCode: number | undefined;
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
33
import * as assert from 'assert';
4-
import { revertTreeString } from '../../utils';
4+
import { revertTreeString } from '../utils';
55

66
// Defines a Mocha test suite to group tests of similar kind together
77
suite('Extension Tests', function () {
@@ -14,11 +14,11 @@ suite('Extension Tests', function () {
1414
// });
1515
test('revert tree string to text', function () {
1616
const rootText = fs.readFileSync(
17-
path.join(__dirname, '../../../fixtures/root.txt'),
17+
path.join(__dirname, '../../fixtures/root.txt'),
1818
'utf8'
1919
);
2020
const rootReverted = fs.readFileSync(
21-
path.join(__dirname, '../../../fixtures/root-reverted.txt'),
21+
path.join(__dirname, '../../fixtures/root-reverted.txt'),
2222
'utf8'
2323
);
2424
const reverted = revertTreeString(rootText);
@@ -35,11 +35,11 @@ suite('Extension Tests', function () {
3535
// https://github.com/aprilandjan/ascii-tree-generator/pull/11
3636
test('revert more-depth tree string to text', function () {
3737
const origin = fs.readFileSync(
38-
path.join(__dirname, '../../../fixtures/more-depth-reverted.txt'),
38+
path.join(__dirname, '../../fixtures/more-depth-reverted.txt'),
3939
'utf8'
4040
);
4141
const treeString = fs.readFileSync(
42-
path.join(__dirname, '../../../fixtures/more-depth.txt'),
42+
path.join(__dirname, '../../fixtures/more-depth.txt'),
4343
'utf8'
4444
);
4545
const reverted = revertTreeString(treeString);
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import * as path from 'path';
44
import {
55
listDirectory,
66
formatFileTreeItemsFromDirectory,
7-
} from '../../../lib/directory';
8-
import { IFileStat } from '../../../lib/interface';
7+
} from '../../lib/directory';
8+
import { IFileStat } from '../../lib/interface';
99

1010
function findFileByName(files: IFileStat[] = [], name: string) {
1111
let result: IFileStat | undefined = undefined;
@@ -22,7 +22,7 @@ function findFileByName(files: IFileStat[] = [], name: string) {
2222
suite('lib/directory functions', function () {
2323
this.timeout(120000);
2424

25-
const rootDir: string = path.resolve(__dirname, '../../../../fixtures/root');
25+
const rootDir: string = path.resolve(__dirname, '../../../fixtures/root');
2626

2727
test('should correctly list directory', async () => {
2828
const files = await listDirectory(rootDir);
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@ import * as assert from 'assert';
22
import * as fs from 'fs';
33
import * as path from 'path';
44

5-
import { formatFileTreeItemsFromDirectory } from '../../../lib/directory';
6-
import { formatFileTreeItemsFromText } from '../../../lib/text';
7-
import { generate } from '../../../lib/generator';
8-
import { setTestMode } from '../../../utils';
5+
import { formatFileTreeItemsFromDirectory } from '../../lib/directory';
6+
import { formatFileTreeItemsFromText } from '../../lib/text';
7+
import { generate } from '../../lib/generator';
8+
import { setTestMode } from '../../utils';
99

1010
suite('lib/generator functions', function () {
1111
this.timeout(120000);
1212
// Enable test mode to override user defined chars with defaults
1313
setTestMode();
14-
const rootDir: string = path.resolve(__dirname, '../../../../fixtures/root');
14+
const rootDir: string = path.resolve(__dirname, '../../../fixtures/root');
1515
const rootText = fs.readFileSync(
16-
path.join(__dirname, '../../../../fixtures/root.txt'),
16+
path.join(__dirname, '../../../fixtures/root.txt'),
1717
'utf8'
1818
);
1919
const rootSorted = fs.readFileSync(
20-
path.join(__dirname, '../../../../fixtures/root-sorted.txt'),
20+
path.join(__dirname, '../../../fixtures/root-sorted.txt'),
2121
'utf8'
2222
);
2323
const rootFillLeft = fs.readFileSync(
24-
path.join(__dirname, '../../../../fixtures/root-fill-left.txt'),
24+
path.join(__dirname, '../../../fixtures/root-fill-left.txt'),
2525
'utf8'
2626
);
2727

@@ -36,7 +36,7 @@ suite('lib/generator functions', function () {
3636

3737
test('should correctly generate tree from hash text', () => {
3838
const text = fs.readFileSync(
39-
path.join(__dirname, '../../../../fixtures/hash.txt'),
39+
path.join(__dirname, '../../../fixtures/hash.txt'),
4040
'utf8'
4141
);
4242
const items = formatFileTreeItemsFromText(text);
@@ -46,7 +46,7 @@ suite('lib/generator functions', function () {
4646

4747
test('should correctly generate tree from indent text', () => {
4848
const text = fs.readFileSync(
49-
path.join(__dirname, '../../../../fixtures/indent.txt'),
49+
path.join(__dirname, '../../../fixtures/indent.txt'),
5050
'utf8'
5151
);
5252
const items = formatFileTreeItemsFromText(text);
@@ -56,7 +56,7 @@ suite('lib/generator functions', function () {
5656

5757
test('should correctly generate tree from hash-indented text if fill-left', () => {
5858
const text = fs.readFileSync(
59-
path.join(__dirname, '../../../../fixtures/hash-indented.txt'),
59+
path.join(__dirname, '../../../fixtures/hash-indented.txt'),
6060
'utf8'
6161
);
6262
const items = formatFileTreeItemsFromText(text);
@@ -66,7 +66,7 @@ suite('lib/generator functions', function () {
6666

6767
test('should correctly generate tree from hash-indented text if not fill-left', () => {
6868
const text = fs.readFileSync(
69-
path.join(__dirname, '../../../../fixtures/hash-indented.txt'),
69+
path.join(__dirname, '../../../fixtures/hash-indented.txt'),
7070
'utf8'
7171
);
7272
const items = formatFileTreeItemsFromText(text);

0 commit comments

Comments
 (0)