What will give me something like ruby readline with a default value?

后端 未结 5 645
孤街浪徒
孤街浪徒 2020-12-03 15:11

If I want to have a prompt on the terminal with a default value already typed in, how can I do that?

Ruby\'s standard Readline.readline() lets me set th

5条回答
  •  佛祖请我去吃肉
    2020-12-03 15:36

    I'm struggling with the same thing.

    The way I'm doing it right now is:

    options = ["the_text_you_want"]
    question = "use TAB or up arrow to show the text > "
    
    Readline.completion_append_character = " "
    Readline::HISTORY.push options.first
    Readline.completion_proc = proc { |s| options.grep( /^#{Regexp.escape(s)}/ ) }
    
    while value = Readline.readline(question, true)
      exit if value == 'q'
      puts value.chomp.strip #do something with the value here
    end
    

    yes, it's silly, but it has been the only way I've found to do it.

    did anybody find any solution to this?

提交回复
热议问题