0

I'm wanting to build a "input history" script within Python 2.x, but I'm having a bit of trouble getting a secondary input while using raw_input(), and writing to what the user inputs.

Because I found it a wee bit difficult to explain, I'm providing an example in JavaSript + HTML to try and clear things up.

JavaScript + HTML Example

(In snippet form)

var input = document.getElementById("input"),
  button = document.getElementById("button"),
  ihistory = document.getElementById("ihistory"),
  history = [],
  position = 0,
  text = "";

button.onclick = function() {
  if (input.value != "") {
    history.push(input.value);
    input.value = "";
    position++;
    input.focus();
  }
};

input.onkeydown = function(e) {
  if (e.keyCode == 38 && position - 1 >= 0) {
    e.preventDefault();
    position--;
    input.value = history[position].toString();
  } else if (e.keyCode == 40 && position + 1 <= history.length) {
    e.preventDefault();
    position++;
    input.value = history[position].toString();
  } else if (e.keyCode == 13) {
    button.click();
  }
}
<input type="text" id="input"></input>
<button id="button">Submit</button>

<br />
<br />
<br />
<hr />
<p>To Submit text to the text to the history, press "submit".</p>
<p>To access previous history, press the up arrow key. To access future history, press the down arrow key.</p>

I'm not sure it is possible to write to what the user inputs, so that they can edit it, etc, without writing C/++ code. I'm fine with it if it does require C/++, or if it requires any weird (but preferably small) modules. Also, I'm progamming this in a Linux environment, so I am not interested it Windows/Mac-only answers.

Callum
  • 40
  • 1
  • 10
  • Anything I can do to make my intentions clearer, I apologise for not doing in the first place. Tired-programming is never fun. – Callum Jul 18 '15 at 12:37

1 Answers1

1

What you're describing is readline functionality. There are some samples of how to use it here. Once you've got your head around the function, you'll probably find that this is already answered on SO here.

Community
  • 1
  • 1
Peter Brittain
  • 13,489
  • 3
  • 41
  • 57
  • Can't believe I didn't find that post, or that module... Nevertheless, I am now less-of-a-newb. – Callum Jul 18 '15 at 13:21