Skip to content

Commit

Permalink
feat(CLI): Add line and chunk continuation
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Dec 4, 2019
1 parent 46607f0 commit 397771d
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,13 @@ const repl = (executor: Executor, lang = 'python', debug: boolean): void => {
})
repl.prompt()

let buffer = ''

// Function that handles each line
const getLine = async (line: string): Promise<void> => {
// Check if user wants to switch language
// Check if user wants to...

// ...switch language
const match = /^<\s*(\w+)/.exec(line)
if (match !== null) {
// Change language and provide new prompt
Expand All @@ -167,7 +171,23 @@ const repl = (executor: Executor, lang = 'python', debug: boolean): void => {
return
}

// User entered a 'normal' line: execute a `CodeChunk`..
// ...continue the line
// We use backslash which is line continuation char in most
// common languages e.g. JS, Python
if (line.endsWith('\\')) {
buffer += line.slice(0, -1)
return
}
// ...continue the chunk on a new line
// Useful where nelines are important e.g. `for` loops in Python
if (line.endsWith('...')) {
buffer += line.slice(0, -3) + '\n'
return
}

// User entered a 'normal' line so add to buffer and
// execute a `CodeChunk`
buffer += line

// Spinner: it's useful feedback for long cells
const spinnerText = (seconds = 0) => chalk.grey(`${seconds}s`)
Expand All @@ -192,15 +212,18 @@ const repl = (executor: Executor, lang = 'python', debug: boolean): void => {
'yellow',
'magenta',
'red'
][Math.min(seconds, 6)] as Ora['color']
][Math.min(Math.round(Math.log(seconds)), 6)] as Ora['color']
}, 1000)

const result = (await executor.execute({
type: 'CodeChunk',
programmingLanguage: lang,
text: line
text: buffer
})) as CodeChunk

// Clear the buffer
buffer = ''

// Stop the spinner
clearInterval(interval)
spinner.stop()
Expand Down

0 comments on commit 397771d

Please sign in to comment.