mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-04 23:05:47 +02:00
History behaves now like bash: editing entries in history are not persistent
This commit is contained in:
parent
99e3f6d3f9
commit
7f2787c217
1 changed files with 46 additions and 19 deletions
|
@ -27,13 +27,31 @@ import org.eclipse.swt.widgets.Text;
|
||||||
/**
|
/**
|
||||||
* Manages the Command History for the command line input
|
* Manages the Command History for the command line input
|
||||||
* of the terminal control.
|
* of the terminal control.
|
||||||
|
* <li>
|
||||||
|
* <ul>Navigate with ARROW_UP,ARROW_DOWN,PAGE_UP,PAGE_DOWN
|
||||||
|
* <ul>ESC to cancel history editing
|
||||||
|
* <ul>History can be edited (by moving up and edit) but changes are
|
||||||
|
* not persistent (like in bash).
|
||||||
|
* </li>
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class CommandInputFieldWithHistory implements ICommandInputField {
|
public class CommandInputFieldWithHistory implements ICommandInputField {
|
||||||
final List fHistory=new ArrayList();
|
final List fHistory=new ArrayList();
|
||||||
private int fHistoryPos=0;
|
/**
|
||||||
|
* Keeps a modifiable history while in history editing mode
|
||||||
|
*/
|
||||||
|
List fEditedHistory;
|
||||||
|
/**
|
||||||
|
* The current position in the edit history
|
||||||
|
*/
|
||||||
|
private int fEditHistoryPos=0;
|
||||||
|
/**
|
||||||
|
* The limit of the history.
|
||||||
|
*/
|
||||||
private final int fMaxSize;
|
private final int fMaxSize;
|
||||||
private boolean fInHistory=false;
|
/**
|
||||||
|
* The input text field.
|
||||||
|
*/
|
||||||
private Text fInputField;
|
private Text fInputField;
|
||||||
public CommandInputFieldWithHistory(int maxHistorySize) {
|
public CommandInputFieldWithHistory(int maxHistorySize) {
|
||||||
fMaxSize=maxHistorySize;
|
fMaxSize=maxHistorySize;
|
||||||
|
@ -43,11 +61,7 @@ public class CommandInputFieldWithHistory implements ICommandInputField {
|
||||||
* @param line
|
* @param line
|
||||||
*/
|
*/
|
||||||
protected void pushLine(String line) {
|
protected void pushLine(String line) {
|
||||||
// if we used the history. therefore we added the current as 0th item
|
endHistoryMode();
|
||||||
if(fInHistory)
|
|
||||||
fHistory.remove(0);
|
|
||||||
fInHistory=false;
|
|
||||||
fHistoryPos=0;
|
|
||||||
// anything to remember?
|
// anything to remember?
|
||||||
if(line==null || line.trim().length()==0)
|
if(line==null || line.trim().length()==0)
|
||||||
return;
|
return;
|
||||||
|
@ -64,6 +78,7 @@ public class CommandInputFieldWithHistory implements ICommandInputField {
|
||||||
* @param history or null
|
* @param history or null
|
||||||
*/
|
*/
|
||||||
public void setHistory(String history) {
|
public void setHistory(String history) {
|
||||||
|
endHistoryMode();
|
||||||
fHistory.clear();
|
fHistory.clear();
|
||||||
if(history==null)
|
if(history==null)
|
||||||
return;
|
return;
|
||||||
|
@ -94,18 +109,22 @@ public class CommandInputFieldWithHistory implements ICommandInputField {
|
||||||
* if the limit is reached.
|
* if the limit is reached.
|
||||||
*/
|
*/
|
||||||
public String move(String currLine, int count) {
|
public String move(String currLine, int count) {
|
||||||
if(!fInHistory) {
|
if(!inHistoryMode()) {
|
||||||
fInHistory=true;
|
fEditedHistory=new ArrayList(fHistory.size()+1);
|
||||||
fHistory.add(0,currLine);
|
fEditedHistory.add(currLine);
|
||||||
} else {
|
fEditedHistory.addAll(fHistory);
|
||||||
fHistory.set(fHistoryPos,currLine);
|
fEditHistoryPos=0;
|
||||||
}
|
}
|
||||||
if(fHistoryPos+count>=fHistory.size())
|
fEditedHistory.set(fEditHistoryPos,currLine);
|
||||||
|
if(fEditHistoryPos+count>=fEditedHistory.size())
|
||||||
return null;
|
return null;
|
||||||
if(fHistoryPos+count<0)
|
if(fEditHistoryPos+count<0)
|
||||||
return null;
|
return null;
|
||||||
fHistoryPos+=count;
|
fEditHistoryPos+=count;
|
||||||
return (String) fHistory.get(fHistoryPos);
|
return (String) fEditedHistory.get(fEditHistoryPos);
|
||||||
|
}
|
||||||
|
private boolean inHistoryMode() {
|
||||||
|
return fEditedHistory!=null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -113,10 +132,18 @@ public class CommandInputFieldWithHistory implements ICommandInputField {
|
||||||
* @return the string to be shown in the command line
|
* @return the string to be shown in the command line
|
||||||
*/
|
*/
|
||||||
protected String escape() {
|
protected String escape() {
|
||||||
if(!fInHistory)
|
if(!inHistoryMode())
|
||||||
return null;
|
return null;
|
||||||
fHistoryPos=0;
|
String line= (String) fEditedHistory.get(0);
|
||||||
return (String) fHistory.get(fHistoryPos);
|
endHistoryMode();
|
||||||
|
return line;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* End history editing
|
||||||
|
*/
|
||||||
|
private void endHistoryMode() {
|
||||||
|
fEditedHistory=null;
|
||||||
|
fEditHistoryPos=0;
|
||||||
}
|
}
|
||||||
public void createControl(Composite parent,final ITerminalViewControl terminal) {
|
public void createControl(Composite parent,final ITerminalViewControl terminal) {
|
||||||
fInputField=new Text(parent, SWT.SINGLE|SWT.BORDER);
|
fInputField=new Text(parent, SWT.SINGLE|SWT.BORDER);
|
||||||
|
|
Loading…
Add table
Reference in a new issue