javascript - Change REPL prompt from within the REPL - Stack Overflow

I'm trying to change the prompt for Node's REPL with text given inside the REPL. My original

I'm trying to change the prompt for Node's REPL with text given inside the REPL.

My original attempt was something like this.

import repl from 'repl'

let PROMPT = 'original > '

repl.start({
  ignoreUndefined: true,
  prompt: PROMPT,
  eval: (text, context, filename, callback) => {
    PROMPT = `${text} >`
    return callback()
  }
})

I realized this isn't gonna work because the prompt for the repl is set when the process starts.

I came up with something like this, which starts a new repl with a new prompt after ever entered mand.

import repl from 'repl'

let PROMPT = 'original > '
let REPL = createRepl(PROMPT)

function createRepl (prompt) {
  return repl.start({
    ignoreUndefined: true,
    prompt: PROMPT,
    eval: (text, context, filename, callback) => {
      PROMPT = `${text.replace(/\n/, '')} >`
      REPL.close()
      REPL = createRepl(PROMPT)
    }
  })
}

This solution is almost there the issue is that the process is exiting when I close the original REPL.

I'm simply trying to change the REPL prompt from inside the script after the process starts. I'm open to any means to do this. Ideally the prompt prop took a function with the prompt argument and allowed you to set it from within the function, then I could use an event or something else.

I'm trying to change the prompt for Node's REPL with text given inside the REPL.

My original attempt was something like this.

import repl from 'repl'

let PROMPT = 'original > '

repl.start({
  ignoreUndefined: true,
  prompt: PROMPT,
  eval: (text, context, filename, callback) => {
    PROMPT = `${text} >`
    return callback()
  }
})

I realized this isn't gonna work because the prompt for the repl is set when the process starts.

I came up with something like this, which starts a new repl with a new prompt after ever entered mand.

import repl from 'repl'

let PROMPT = 'original > '
let REPL = createRepl(PROMPT)

function createRepl (prompt) {
  return repl.start({
    ignoreUndefined: true,
    prompt: PROMPT,
    eval: (text, context, filename, callback) => {
      PROMPT = `${text.replace(/\n/, '')} >`
      REPL.close()
      REPL = createRepl(PROMPT)
    }
  })
}

This solution is almost there the issue is that the process is exiting when I close the original REPL.

I'm simply trying to change the REPL prompt from inside the script after the process starts. I'm open to any means to do this. Ideally the prompt prop took a function with the prompt argument and allowed you to set it from within the function, then I could use an event or something else.

Share Improve this question asked Mar 31, 2016 at 5:10 ThomasReggiThomasReggi 59.7k97 gold badges259 silver badges459 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

You should be able to call repl.setPrompt('foo> '); to change the prompt.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744743985a4591198.html

相关推荐

  • javascript - Change REPL prompt from within the REPL - Stack Overflow

    I'm trying to change the prompt for Node's REPL with text given inside the REPL. My original

    23小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信