Skip to content

Commit

Permalink
feat: add color to identify file and folder in terminal (closes #6)
Browse files Browse the repository at this point in the history
  • Loading branch information
w2xi committed Dec 11, 2024
1 parent 8ccc185 commit 1162eb0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .prettierrc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export default {
trailingComma: 'es5',
tabWidth: 2,
semi: false,
singleQuote: true
singleQuote: true,
}
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
"scripts": {
"prepublishOnly": "nr build",
"dev": "tsup src/index.ts --watch",
"build": "tsup src/index.ts --minify",
"build": "nr type && tsup src/index.ts --minify",
"release": "bumpp --commit --push --tag && pnpm publish",
"lint": "eslint src/**/*.ts",
"lint:fix": "eslint src --fix",
"test": "vitest"
"test": "vitest",
"type": "tsc --noEmit"
},
"bin": {
"treei": "dist/index.js"
Expand All @@ -39,6 +40,8 @@
"vitest": "^1.3.1"
},
"dependencies": {
"commander": "^12.0.0"
"commander": "^12.0.0",
"picocolors": "^1.1.1"
}
}

14 changes: 8 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
export const emoji = {
directory: '📁',
file: '📄'
file: '📄',
}

export const characters = {
border: '|',
contain: '├',
line: '─',
last: '└'
last: '└',
}

export const defaultOptions = {
// strategy of finding tree structure, bfs by default
strategy: 'bfs'
strategy: 'bfs',
}

export const NodeTypes = {
ROOT: 'root',
DIRECTORY: 'directory',
FILE: 'file'
}
FILE: 'file',
} as const
7 changes: 5 additions & 2 deletions src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { emoji, characters, NodeTypes } from './config'
import { Options, TreeNode } from './type'
import { blue } from 'picocolors'

const lastDirStack: boolean[] = []

Expand All @@ -25,9 +26,11 @@ export function generate(
let contentPrefix =
index === data!.length - 1 ? characters.last : characters.contain
contentPrefix += characters.line.repeat(2)

const name = item.type === NodeTypes.DIRECTORY ? blue(item.name) : item.name
const content = options.icon
? `${emoji[item.type as keyof typeof emoji]}${item.name}`
: `${item.name}`
? `${emoji[item.type as keyof typeof emoji]}${name}`
: `${name}`

let currentLineStr = `${borderPrefix}${contentPrefix}${content}`
currentLineStr += '\n'
Expand Down
4 changes: 3 additions & 1 deletion src/type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NodeTypes } from './config'

export interface Options {
directory: string
ignore?: string | string[]
Expand All @@ -9,7 +11,7 @@ export interface Options {
}

export interface TreeNode {
type: string
type: (typeof NodeTypes)[keyof typeof NodeTypes]
name: string
path?: string
children?: TreeNode[]
Expand Down

0 comments on commit 1162eb0

Please sign in to comment.