This repository was archived by the owner on Mar 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
POC: add ink and draw simple counter with it #5
Open
littldr
wants to merge
6
commits into
master
Choose a base branch
from
poc_ink
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3cb29e4
POC: add ink and draw simple counter with it
5865fc0
WIP: implement boxes layout with (broken) spinner
29caf54
Merge branch 'master' into poc_ink
skorfmann 3555238
Adapt it to ink#next with actual React
skorfmann eb1dd71
Initial State / Better layout
skorfmann 21da626
WIP: Add diff output
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
/dist | ||
/tmp | ||
node_modules | ||
build/** |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,51 @@ | ||
const log4js = require("log4js"); | ||
const PropTypes = require("prop-types"); | ||
import { Component } from "react"; | ||
|
||
class Row extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.switchFrame = this.switchFrame.bind(this); | ||
} | ||
|
||
render(props) { | ||
log4js.getLogger("vis").info(props); | ||
let renderedChilds = []; | ||
for (const child of props.children) { | ||
let childRows = renderToString(child).split("\n"); | ||
renderedChilds.push(childRows); | ||
} | ||
|
||
let rows = []; | ||
let maxRows = Math.max(...renderedChilds.map(r => r.length)); | ||
|
||
for (const row of Array(maxRows).keys()) { | ||
// TODO handle elements which are not the same height | ||
rows.push(renderedChilds.map(c => c[row]).join(props.separator)); | ||
} | ||
|
||
return h("div", {}, rows.join("\n")); | ||
} | ||
|
||
componentDidMount() { | ||
this.timer = setInterval(this.switchFrame, 100); | ||
} | ||
|
||
componentWillUnmount() { | ||
clearInterval(this.timer); | ||
} | ||
|
||
switchFrame() { | ||
this.forceUpdate(); | ||
} | ||
} | ||
|
||
Row.propTypes = { | ||
children: PropTypes.node | ||
}; | ||
Row.defaultProps = { | ||
separator: " " | ||
}; | ||
|
||
module.exports = { Row }; |
This file contains hidden or 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,44 @@ | ||
import { Component } from "react"; | ||
import spinners from "cli-spinners"; | ||
|
||
class Spinner extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { frame: 0 }; | ||
this.switchFrame = this.switchFrame.bind(this); | ||
} | ||
|
||
getSpinner() { | ||
return spinners.dots; | ||
} | ||
|
||
render() { | ||
const spinner = this.getSpinner(); | ||
return spinner.frames[this.state.frame]; | ||
} | ||
|
||
componentDidMount() { | ||
const spinner = this.getSpinner(); | ||
|
||
this.timer = setInterval(this.switchFrame, spinner.interval); | ||
} | ||
|
||
componentWillUnmount() { | ||
clearInterval(this.timer); | ||
} | ||
|
||
switchFrame() { | ||
const { frame } = this.state; | ||
|
||
const spinner = this.getSpinner(); | ||
const isLastFrame = frame === spinner.frames.length - 1; | ||
const nextFrame = isLastFrame ? 0 : frame + 1; | ||
|
||
this.setState({ | ||
frame: nextFrame | ||
}); | ||
} | ||
} | ||
|
||
module.exports = { Spinner }; |
This file contains hidden or 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,24 @@ | ||
import React, { Component } from "react"; | ||
import { Box, Text } from "@terrastack/ink"; | ||
|
||
class StackComponent extends Component { | ||
render() { | ||
return ( | ||
<Box border={1} borderColor={this.color()}> | ||
<Text> | ||
{this.props.item.name} - {this.props.item.status} | ||
</Text> | ||
</Box> | ||
); | ||
} | ||
|
||
color() { | ||
if (this.props.item.status === "success") { | ||
return "green"; | ||
} else { | ||
return "white"; | ||
} | ||
} | ||
} | ||
|
||
module.exports = { StackComponent }; |
This file contains hidden or 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,44 @@ | ||
import React, { Component } from "react"; | ||
import { StackComponent } from "./stack-component"; | ||
import { connect } from "react-redux"; | ||
import { Box } from "@terrastack/ink"; | ||
import _ from "lodash"; | ||
|
||
class Row extends Component { | ||
render() { | ||
return <Box>{this.props.children}</Box>; | ||
} | ||
} | ||
|
||
class Stack extends Component { | ||
render() { | ||
if (_.isEmpty(this.props.rows)) { | ||
return "Nothing to see"; | ||
} else { | ||
const elements = this.props.rows.map((row, index) => ( | ||
<Row key={index}> | ||
{row.map(item => ( | ||
<StackComponent key={item.name} item={item} /> | ||
))} | ||
</Row> | ||
)); | ||
|
||
return elements; | ||
} | ||
} | ||
} | ||
|
||
const mapStateToProps = state => { | ||
const rows = Object.keys(state).map(layerIndex => { | ||
const layer = state[layerIndex]; | ||
return Object.keys(layer).map(name => { | ||
return { | ||
name, | ||
status: layer[name] | ||
}; | ||
}); | ||
}); | ||
return { rows }; | ||
}; | ||
|
||
module.exports = { Stack: connect(mapStateToProps)(Stack) }; |
This file contains hidden or 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,82 @@ | ||
import { render, Box } from "@terrastack/ink"; | ||
// import store from "./store"; | ||
import { Stack } from "./components/stack"; | ||
import React from "react"; | ||
import { Provider } from "react-redux"; | ||
|
||
const { createStore } = require("redux"); | ||
|
||
const store = createStore((state = {}, action) => { | ||
if (action.component == undefined) { | ||
return state; | ||
} | ||
|
||
let layer = action.component._layer; | ||
let name = action.component.name; | ||
let newState = {}; | ||
switch (action.type) { | ||
case "ADDED": | ||
if (state[layer] == undefined) { | ||
state[layer] = {}; | ||
} | ||
newState = Object.assign({}, state); | ||
newState[layer][name] = "added"; | ||
return newState; | ||
case "START": | ||
if (state[layer] == undefined) { | ||
state[layer] = {}; | ||
} | ||
newState = Object.assign({}, state); | ||
newState[layer][name] = "start"; | ||
return newState; | ||
case "SUCCESS": | ||
newState = Object.assign({}, state); | ||
newState[layer][name] = "success"; | ||
return newState; | ||
case "FAILED": | ||
newState = Object.assign({}, state); | ||
newState[layer][name] = "failed"; | ||
return newState; | ||
default: | ||
return state; | ||
} | ||
}); | ||
|
||
const applyVisualization = base => { | ||
let buffer = []; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can publish the initial state here? Or somehow fill it in the state directly? IMHO: It make sense to move the initial state publishing to the clinic, as it is not needed anywhere else.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, thought about something similar. We could just get the resolved stack and set it as initial state. |
||
base.events.on("component:added", function(component) { | ||
store.dispatch({ type: "ADDED", component: component }); | ||
}); | ||
|
||
base.events.on("component:*:start", function(component) { | ||
store.dispatch({ type: "START", component: component }); | ||
}); | ||
|
||
base.events.on("component:*:success", function(component) { | ||
store.dispatch({ type: "SUCCESS", component: component }); | ||
}); | ||
|
||
base.events.on("component:*:failed", function(component) { | ||
store.dispatch({ type: "FAILED", component: component }); | ||
}); | ||
|
||
base.events.on("output:*", function(_component, output) { | ||
buffer.push(output); | ||
}); | ||
|
||
base.events.on("error", function(component) { | ||
// console.log(chalk.red.bold.underline(`Error: ${component.name}`)); | ||
// console.log(`Recent output: ${_.takeRight(buffer, 50).join("")}`); | ||
}); | ||
|
||
render( | ||
<Provider store={store}> | ||
<Stack /> | ||
</Provider> | ||
); | ||
}; | ||
|
||
module.exports = { | ||
applyVisualization | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole file is not used anymore, or?
(Can't verify it properly on my phone 😅)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll have to check, not sure :)