-
-
Notifications
You must be signed in to change notification settings - Fork 622
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
Vadim Demedes
authored
Jun 18, 2020
1 parent
2bcb4c0
commit 155e1a0
Showing
9 changed files
with
133 additions
and
131 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
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 |
---|---|---|
@@ -1,61 +1,54 @@ | ||
import Yoga from 'yoga-layout-prebuilt'; | ||
import renderNodeToOutput from './render-node-to-output'; | ||
import Output from './output'; | ||
import {setStyle} from './dom'; | ||
import type {DOMElement} from './dom'; | ||
|
||
export type Renderer = ( | ||
node: DOMElement | ||
) => { | ||
interface Result { | ||
output: string; | ||
outputHeight: number; | ||
staticOutput: string; | ||
}; | ||
|
||
export default ({terminalWidth = 100}: {terminalWidth: number}): Renderer => { | ||
return (node: DOMElement) => { | ||
setStyle(node, { | ||
width: terminalWidth | ||
}); | ||
} | ||
|
||
if (node.yogaNode) { | ||
node.yogaNode.calculateLayout(undefined, undefined, Yoga.DIRECTION_LTR); | ||
|
||
const output = new Output({ | ||
width: node.yogaNode.getComputedWidth(), | ||
height: node.yogaNode.getComputedHeight() | ||
}); | ||
export default (node: DOMElement, terminalWidth: number): Result => { | ||
node.yogaNode!.setWidth(terminalWidth); | ||
|
||
renderNodeToOutput(node, output, {skipStaticElements: true}); | ||
if (node.yogaNode) { | ||
node.yogaNode.calculateLayout(undefined, undefined, Yoga.DIRECTION_LTR); | ||
|
||
let staticOutput; | ||
const output = new Output({ | ||
width: node.yogaNode.getComputedWidth(), | ||
height: node.yogaNode.getComputedHeight() | ||
}); | ||
|
||
if (node.staticNode?.yogaNode) { | ||
staticOutput = new Output({ | ||
width: node.staticNode.yogaNode.getComputedWidth(), | ||
height: node.staticNode.yogaNode.getComputedHeight() | ||
}); | ||
renderNodeToOutput(node, output, {skipStaticElements: true}); | ||
|
||
renderNodeToOutput(node.staticNode, staticOutput, { | ||
skipStaticElements: false | ||
}); | ||
} | ||
let staticOutput; | ||
|
||
const {output: generatedOutput, height: outputHeight} = output.get(); | ||
if (node.staticNode?.yogaNode) { | ||
staticOutput = new Output({ | ||
width: node.staticNode.yogaNode.getComputedWidth(), | ||
height: node.staticNode.yogaNode.getComputedHeight() | ||
}); | ||
|
||
return { | ||
output: generatedOutput, | ||
outputHeight, | ||
// Newline at the end is needed, because static output doesn't have one, so | ||
// interactive output will override last line of static output | ||
staticOutput: staticOutput ? `${staticOutput.get().output}\n` : '' | ||
}; | ||
renderNodeToOutput(node.staticNode, staticOutput, { | ||
skipStaticElements: false | ||
}); | ||
} | ||
|
||
const {output: generatedOutput, height: outputHeight} = output.get(); | ||
|
||
return { | ||
output: '', | ||
outputHeight: 0, | ||
staticOutput: '' | ||
output: generatedOutput, | ||
outputHeight, | ||
// Newline at the end is needed, because static output doesn't have one, so | ||
// interactive output will override last line of static output | ||
staticOutput: staticOutput ? `${staticOutput.get().output}\n` : '' | ||
}; | ||
} | ||
|
||
return { | ||
output: '', | ||
outputHeight: 0, | ||
staticOutput: '' | ||
}; | ||
}; |
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
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,19 @@ | ||
import EventEmitter from 'events'; | ||
import {spy} from 'sinon'; | ||
|
||
// Fake process.stdout | ||
interface Stream extends EventEmitter { | ||
output: string; | ||
columns: number; | ||
write(str: string): void; | ||
get(): string; | ||
} | ||
|
||
export default (columns?: number): Stream => { | ||
const stdout = new EventEmitter(); | ||
stdout.columns = columns ?? 100; | ||
stdout.write = spy(); | ||
stdout.get = () => stdout.write.lastCall.args[0]; | ||
|
||
return stdout; | ||
}; |
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 |
---|---|---|
@@ -1,38 +1,17 @@ | ||
import {render} from '../../src'; | ||
|
||
// Fake process.stdout | ||
interface Stream { | ||
output: string; | ||
columns: number; | ||
write(str: string): void; | ||
get(): string; | ||
} | ||
|
||
const createStream: (options: {columns: number}) => Stream = ({columns}) => { | ||
let output = ''; | ||
return { | ||
output, | ||
columns, | ||
write(str: string) { | ||
output = str; | ||
}, | ||
get() { | ||
return output; | ||
} | ||
}; | ||
}; | ||
import createStdout from './create-stdout'; | ||
|
||
export const renderToString: ( | ||
node: JSX.Element, | ||
options?: {columns: number} | ||
) => string = (node, options = {columns: 100}) => { | ||
const stream = createStream(options); | ||
const stdout = createStdout(options.columns); | ||
|
||
render(node, { | ||
// @ts-ignore | ||
stdout: stream, | ||
stdout, | ||
debug: true | ||
}); | ||
|
||
return stream.get(); | ||
return stdout.get(); | ||
}; |
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
Oops, something went wrong.