Skip to content

Commit

Permalink
feat: a new dedicated execute command for the R Shell to allow also m…
Browse files Browse the repository at this point in the history
…ore declarative execution :D
  • Loading branch information
EagleoutIce committed Aug 29, 2023
1 parent c1f9fe3 commit 0ddce3b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
9 changes: 8 additions & 1 deletion src/cli/repl/commands/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { rawPrompt } from '../prompt'
import { bold, italic } from '../../../statistics'
import { versionCommand } from './version'
import { parseCommand } from './parse'
import { guard } from '../../../util/assert'
import { executeCommand } from './execute'

function printHelpForScript(script: [string, ReplCommand]): string {
const base = ` ${bold(padCmd(':' + script[0]))}${script[1].description}`
Expand All @@ -33,11 +35,13 @@ ${
Array.from(Object.entries(commands)).filter(([, {script}]) => !script).map(
printHelpForScript).join('\n')
}
Furthermore, you can directly call the following scripts which accept arguments. If you are unsure, try to add ${italic('--help')} after the command.
${
Array.from(Object.entries(commands)).filter(([, {script}]) => script).map(
([command, { description }]) => ` ${bold(padCmd(':' + command))}${description}`).join('\n')
}
You can combine commands by separating them with a semicolon ${bold(';')}.
`)
}
Expand All @@ -49,7 +53,8 @@ const commands: Record<string, ReplCommand> = {
'help': helpCommand,
'quit': quitCommand,
'version': versionCommand,
'parse': parseCommand
'parse': parseCommand,
'execute': executeCommand
}

for(const [script, { target, description, type}] of Object.entries(scripts)) {
Expand All @@ -75,8 +80,10 @@ export const commandNames: string[] = []
const commandMapping: Record<string, string> = {}

for(const [command, { aliases }] of Object.entries(commands)) {
guard(commandMapping[command] as string | undefined === undefined, `Command ${command} is already registered!`)
commandMapping[command] = command
for(const alias of aliases) {
guard(commandMapping[alias] as string | undefined === undefined, `Command (alias) ${alias} is already registered!`)
commandMapping[alias] = command
}
commandNames.push(command)
Expand Down
27 changes: 27 additions & 0 deletions src/cli/repl/commands/execute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { RShell } from '../../../r-bridge'
import { italic } from '../../../statistics'
import { ReplCommand } from './main'


export async function executeRShellCommand(shell: RShell, statement: string) {
try {
const result = await shell.sendCommandWithOutput(statement, {
from: 'both',
automaticallyTrimOutput: true
})
console.log(`${italic(result.join('\n'))}\n`)
} catch(e) {
console.error(`Error while executing '${statement}': ${(e as Error).message}`)
}
}


export const executeCommand: ReplCommand = {
description: 'Execute the given code as R code (essentially similar to using now command)',
usageExample: ':execute',
aliases: [ 'e', 'r' ],
script: false,
fn: async(shell, _tokenMap, remainingLine) => {
await executeRShellCommand(shell, remainingLine)
}
}
11 changes: 2 additions & 9 deletions src/cli/repl/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { prompt } from './prompt'
import { commandNames, getCommand } from './commands'
import { ReadLineOptions } from 'node:readline'
import { splitAtEscapeSensitive } from '../../util/args'
import { executeRShellCommand } from './commands/execute'

const replCompleterKeywords = Array.from(commandNames, s => `:${s}`)

Expand Down Expand Up @@ -39,15 +40,7 @@ async function replProcessStatement(statement: string, shell: RShell, tokenMap:
console.log(`the command '${command}' is unknown, try ${bold(':help')} for more information`)
}
} else {
try {
const result = await shell.sendCommandWithOutput(statement, {
from: 'both',
automaticallyTrimOutput: true
})
console.log(`${italic(result.join('\n'))}\n`)
} catch(e) {
console.error(`Error while executing '${statement}': ${(e as Error).message}`)
}
await executeRShellCommand(shell, statement)
}
}

Expand Down

0 comments on commit 0ddce3b

Please sign in to comment.