-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5eb7295
commit 4beb62f
Showing
12 changed files
with
842 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* @poppinss/dumper | ||
* | ||
* (c) Poppinss | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { dump, themes } from '../formatters/console/main.js' | ||
import { obj } from './values.js' | ||
|
||
console.log( | ||
dump(obj, { | ||
styles: themes.dark, | ||
}) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* @poppinss/dumper | ||
* | ||
* (c) Poppinss | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { themes } from './themes.js' | ||
import type { Token } from '../../src/types.js' | ||
import { ConsolePrinters } from './printers/main.js' | ||
import type { ConsoleFormatterConfig, ConsolePrinterStyles } from './types.js' | ||
|
||
/** | ||
* ConsoleFormatter is used to format a collection of parser | ||
* tokens to CLI output. | ||
* | ||
* @example | ||
* ```ts | ||
* const parser = new Parser() | ||
* parser.parse(value) | ||
* | ||
* const tokens = parser.flush() | ||
* | ||
* const formatter = new ConsoleFormatter() | ||
* const output = formatter.format(tokens) | ||
* ``` | ||
*/ | ||
export class ConsoleFormatter { | ||
/** | ||
* Styles for output elements | ||
*/ | ||
readonly styles: ConsolePrinterStyles | ||
|
||
/** | ||
* Context maintained through out the printing | ||
* phase. Each instance has its own context | ||
* that gets mutated internally. | ||
*/ | ||
context: Record<string, any> | ||
|
||
/** | ||
* Value for the newline character | ||
*/ | ||
readonly newLine = '\n' | ||
|
||
/** | ||
* Utility to manage indentation | ||
*/ | ||
readonly indentation = { | ||
counter: 0, | ||
|
||
/** | ||
* Increment the identation by 1 step | ||
*/ | ||
increment() { | ||
this.counter++ | ||
}, | ||
|
||
/** | ||
* Decrement the identation by 1 step | ||
*/ | ||
decrement() { | ||
this.counter-- | ||
}, | ||
|
||
/** | ||
* Get the identation spaces as per the current | ||
* identation level | ||
*/ | ||
getSpaces() { | ||
return new Array(this.counter * 2 + 1).join(' ') | ||
}, | ||
} | ||
|
||
constructor(config?: ConsoleFormatterConfig, context?: Record<string, any>) { | ||
this.context = context || {} | ||
this.styles = Object.freeze({ ...themes.dark, ...config?.styles }) | ||
} | ||
|
||
/** | ||
* Format a collection of tokens to ANSI output | ||
*/ | ||
format(tokens: Token[]) { | ||
return tokens | ||
.map((token) => { | ||
const formatter = ConsolePrinters[token.type] | ||
return formatter(token as any, this) || '' | ||
}) | ||
.join('') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* @poppinss/dumper | ||
* | ||
* (c) Poppinss | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { Parser } from '../../src/parser.js' | ||
import { ConsoleFormatter } from './formatter.js' | ||
import type { ConsoleDumpConfig } from './types.js' | ||
|
||
export { ConsoleFormatter } | ||
export { themes } from './themes.js' | ||
export { ConsolePrinters } from './printers/main.js' | ||
|
||
/** | ||
* Generate pretty printed HTML output for the provided value. You can | ||
* specify the parser and the formatter options as the 2nd argument. | ||
* | ||
* @example | ||
* ```ts | ||
* const html = dump(someValue) | ||
* | ||
* // With Parser options | ||
* const html = dump(someValue, { | ||
* inspectObjectPrototype: true, | ||
* depth: 10, | ||
* showHidden: true, | ||
* }) | ||
* | ||
* // With Formatter options | ||
* const html = dump(someValue, { | ||
* styles: { | ||
* number: 'color: yellow; font-weight: bold;' | ||
* } | ||
* }) | ||
* ``` | ||
*/ | ||
export function dump(value: any, config?: ConsoleDumpConfig) { | ||
const parser = new Parser(config) | ||
parser.parse(value) | ||
return new ConsoleFormatter(config).format(parser.flush()) | ||
} |
Oops, something went wrong.