Skip to content

Commit

Permalink
trying statemachines
Browse files Browse the repository at this point in the history
  • Loading branch information
herrfugbaum committed Apr 25, 2020
1 parent 3413869 commit 81923ed
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 16 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"table": "^5.1.0",
"untildify": "^3.0.3",
"update-check": "^1.5.2",
"xstate": "^4.9.1",
"yargs": "^12.0.2"
},
"devDependencies": {
Expand All @@ -62,4 +63,4 @@
"stryker-javascript-mutator": "^0.12.1",
"stryker-jest-runner": "^1.2.7"
}
}
}
141 changes: 126 additions & 15 deletions src/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,143 @@

const chalk = require('chalk')
const readline = require('readline')
const { Machine, interpret, assign } = require('xstate')
const renderTable = require('./renderTable')
const parse = require('@herrfugbaum/q')

const executeSql = require('./excuteSql')

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: chalk.cyan('QSV> '),
const pageLength = 1

const replMachine = Machine(
{
id: 'replMachine',
initial: 'repl',
context: {
cursor: 0,
data: [],
},
states: {
repl: {
on: {
ENTER_KEY: 'parsing',
},
},
parsing: {
on: {
SUCCESS: {
target: 'readMore',
actions: ['dataAssign'],
},
ERROR: 'error',
},
},
readMore: {
on: {
SPACE_KEY: [
{
target: 'readMore',
cond: 'notOnLastPage',
actions: ['cursorIncrement'],
},
{
target: 'repl',
actions: ['cursorReset', 'dataReset'],
},
],
readAll: {
target: 'repl',
actions: ['cursorReset'],
},
},
},
error: {
on: {
'': {
target: 'repl',
actions: ['printError'],
},
},
},
},
},
{
actions: {
cursorIncrement: assign({
cursor: context => context.cursor + pageLength,
}),
cursorReset: assign({
cursor: context => (context.cursor = 0),
}),
dataAssign: assign({ data: (context, event) => event.data }),
dataReset: context => (context.data = []),
printError: () => console.log('Some Error occured!'),
},
guards: {
notOnLastPage: (context, event) => context.cursor < context.data.length,
},
},
)

const replService = interpret(replMachine).onTransition(state => {
console.log(state.value)
console.log(
`State: ${state.value}
Cursor: ${state.context.cursor},
DataLength: ${state.context.data.length}`,
)
})

const repl = parsedData => {
rl.prompt()
replService.start()

rl.on('line', line => {
try {
const sqlParserResult = parse(line)
const processedTable = executeSql(sqlParserResult, parsedData)
readline.emitKeypressEvents(process.stdin)
process.stdin.setRawMode(true)

process.stdout.write(renderTable(processedTable))
process.stdout.write('\n')
} catch (error) {
process.stdout.write(error.message + '\n')
const repl = parsedData => {
replService.onTransition(state => {
if (state.value === 'repl') {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: chalk.cyan('QSV> '),
})
rl.removeAllListeners()

rl.prompt()
rl.on('line', line => {
replService.send('ENTER_KEY')
const sqlParserResult = parse(line)
const processedTable = executeSql(sqlParserResult, parsedData)
replService.send('SUCCESS', { data: processedTable })
const data = replService.state.context.data
const cursor = replService.state.context.cursor
const page = data.slice(cursor, cursor + pageLength)
process.stdout.write(renderTable(page))
process.stdout.write('\n')
})
}
rl.prompt()
})
}

process.stdin.on('keypress', (str, key) => {
if (replService.state.value !== 'readMore') return false

if (key.ctrl && key.name === 'c') {
process.exit()
}

if (key.name === 'space') {
replService.send('SPACE_KEY')

const data = replService.state.context.data
const cursor = replService.state.context.cursor
if (cursor < data.length) {
const page = data.slice(cursor, cursor + pageLength)
process.stdout.write('\n')
process.stdout.write(renderTable(page))
process.stdout.write('\n')
}
}
})

module.exports = repl
6 changes: 6 additions & 0 deletions test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
A;B;C
1;2;3
4;5;6
7;8;9
10;11;12
13;14;15
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4677,6 +4677,11 @@ xmlcreate@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/xmlcreate/-/xmlcreate-1.0.2.tgz#fa6bf762a60a413fb3dd8f4b03c5b269238d308f"

xstate@^4.9.1:
version "4.9.1"
resolved "https://registry.yarnpkg.com/xstate/-/xstate-4.9.1.tgz#da883ae0993b129ba0b54592c59b069963b0fe0a"
integrity sha512-cfNnRaBebnr1tvs0nHBUTyomfJx36+8MWwXceyNTZfjyELMM8nIoiBDcUzfKmpNlnAvs2ZPREos19cw6Zl4nng==

y18n@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-3.2.1.tgz#6d15fba884c08679c0d77e88e7759e811e07fa41"
Expand Down

0 comments on commit 81923ed

Please sign in to comment.