Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist history of flowR REPL sessions #641

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/cli/repl/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ import { commandNames, getCommand, standardReplOutput } from './commands'
import * as readline from 'node:readline'
import { splitAtEscapeSensitive } from '../../util/args'
import { executeRShellCommand } from './commands/execute'
import os from 'os'
import path from 'path'
import fs from 'fs'

const replCompleterKeywords = Array.from(commandNames, s => `:${s}`)
const defaultHistoryFile = path.join(os.tmpdir(), '.flowrhistory')

/**
* Used by the repl to provide automatic completions for a given (partial) input line
Expand All @@ -26,6 +30,7 @@ export const DEFAULT_REPL_READLINE_CONFIGURATION: readline.ReadLineOptions = {
output: process.stdout,
tabSize: 4,
terminal: true,
history: loadReplHistory(defaultHistoryFile),
removeHistoryDuplicates: true,
completer: replCompleter
}
Expand Down Expand Up @@ -72,11 +77,14 @@ export async function replProcessAnswer(output: ReplOutput, expr: string, shell:
* If you want to provide a custom one but use the same `completer`, refer to {@link replCompleter}.
* For the default arguments, see {@link DEFAULT_REPL_READLINE_CONFIGURATION}.
* @param output - Defines two methods that every function in the repl uses to output its data.
* @param historyFile - The file to use for persisting the repl's history. Passing undefined causes history not to be saved.
*
* For the execution, this function makes use of {@link replProcessAnswer}
*
*/
export async function repl(shell = new RShell({ revive: 'always' }), rl = readline.createInterface(DEFAULT_REPL_READLINE_CONFIGURATION), output = standardReplOutput) {
export async function repl(shell = new RShell({ revive: 'always' }), rl = readline.createInterface(DEFAULT_REPL_READLINE_CONFIGURATION), output = standardReplOutput, historyFile: string | undefined = defaultHistoryFile) {
if(historyFile)
rl.on('history', h => fs.writeFileSync(historyFile, h.join('\n'), {encoding: 'utf-8'}))

// the incredible repl :D, we kill it with ':quit'
// eslint-disable-next-line no-constant-condition,@typescript-eslint/no-unnecessary-condition
Expand All @@ -92,3 +100,9 @@ export async function repl(shell = new RShell({ revive: 'always' }), rl = readli
})
}
}

export function loadReplHistory(historyFile: string): string[] | undefined {
if(!fs.existsSync(historyFile))
return undefined
return fs.readFileSync(historyFile, {encoding: 'utf-8'}).split('\n')
}
Loading