Skip to content

Commit

Permalink
refactor: change noConsoleClear flag to consoleClear and default to t…
Browse files Browse the repository at this point in the history
…rue for interactive mode, false for non-interactive mode
  • Loading branch information
snowyu committed Jun 18, 2024
1 parent 212ae5d commit 5e1fc1a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/lib/run-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface IRunScriptOptions {
newChat?: boolean,
agentDirs?: string[],
theme?: any,
noConsoleClear?: boolean,
consoleClear?: boolean,
}

function logUpdate(...text: string[]) {
Expand Down Expand Up @@ -74,6 +74,10 @@ export async function runScript(filename: string, options: IRunScriptOptions) {

const { logLevel: level, interactive, stream } = options

if (options.consoleClear === undefined) {
options.consoleClear = interactive
}

const scriptExtName = getMultiLevelExtname(filename, 2)
const scriptBasename = path.basename(filename, scriptExtName)

Expand Down Expand Up @@ -145,7 +149,7 @@ export async function runScript(filename: string, options: IRunScriptOptions) {
// llmLastContent = llmLastContent.slice(llmLastContent.length-100)
// }
if (!isSilence && llmLastContent) {
if (!options.noConsoleClear) {
if (options.consoleClear) {
logUpdate(llmLastContent)
} else {
process.stdout.write(s)
Expand All @@ -159,7 +163,7 @@ export async function runScript(filename: string, options: IRunScriptOptions) {
} catch(error: any) {
if (error.name !== 'AbortError') {throw error}
} finally {
if (!isSilence && !options.noConsoleClear) {logUpdate.clear()}
if (!isSilence && options.consoleClear) {logUpdate.clear()}
}

let result = runtime.result
Expand Down Expand Up @@ -238,7 +242,7 @@ export async function runScript(filename: string, options: IRunScriptOptions) {
const what = error.data?.what ? ':'+error.data.what : ''
input.write(colors.magentaBright(`<${error.name+what}>`))
} finally {
if (!isSilence && !options.noConsoleClear) {logUpdate.clear()}
if (!isSilence && options.consoleClear) {logUpdate.clear()}
}
if (result) {
if (typeof result !== 'string') { result = ux.colorizeJson(result, {pretty: true, theme: options.theme?.json})}
Expand Down
6 changes: 5 additions & 1 deletion src/oclif/commands/run/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ export default class RunScript extends AICommand {
static flags = {
...AICommand.flags,
...AICommonFlags,
'noConsoleClear': Flags.boolean({description: 'disable console clear, debug purpose'}),
'consoleClear': Flags.boolean({
aliases: ['console-clear'],
description: 'Whether console clear after stream output, default to true in interactive, false to non-interactive',
allowNo: true,
}),
}

async run(): Promise<any> {
Expand Down

0 comments on commit 5e1fc1a

Please sign in to comment.