Skip to content

Commit

Permalink
nano command: added copy, cut and uncut text operations
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jul 27, 2019
1 parent 5d12bcc commit 7e4263e
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions builtins/src/main/java/org/jline/builtins/Nano.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public class Nano {
protected int searchTermId = -1;
protected WriteMode writeMode = WriteMode.WRITE;
protected boolean writeBackup;
protected List<String> cutbuffer = new ArrayList<>();

protected boolean readNewBuffer = true;

Expand Down Expand Up @@ -717,6 +718,14 @@ public void moveTo(int x, int y) {
cursorDown(y);
}

public void gotoLine(int x, int y) {
if (printLineNumbers) {
x = Math.max(x - 8, 0);
}
line = y < lines.size() ? y : lines.size() - 1;
ensureCursorVisible();
}

public int getDisplayedCursor() {
int rwidth = size.getColumns() + 1;
int cursor = (printLineNumbers ? 8 : 0);
Expand Down Expand Up @@ -940,6 +949,33 @@ public void matching() {
private int length(String line, int tabs) {
return new AttributedStringBuilder().tabs(tabs).append(line).columnLength();
}

void copy() {
cutbuffer.add(lines.get(line));
gotoLine(0, line + 1);
}

void cut() {
if (lines.size() > 1) {
cutbuffer.add(lines.get(line));
lines.remove(line);
if (line > lines.size() - 1) {
line--;
}
display.clear();
computeAllOffsets();
dirty = true;
}
}

void uncut() {
lines.addAll(line, cutbuffer);
cutbuffer = new ArrayList<>();
display.clear();
computeAllOffsets();
dirty = true;
}

}

public Nano(Terminal terminal, File root) {
Expand Down Expand Up @@ -1137,6 +1173,15 @@ public void run() throws IOException {
case TOGGLE_SUSPENSION:
toggleSuspension();
break;
case COPY:
buffer.copy();
break;
case CUT:
buffer.cut();
break;
case UNCUT:
buffer.uncut();
break;
default:
setMessage("Unsupported " + op.name().toLowerCase().replace('_', '-'));
break;
Expand Down

1 comment on commit 7e4263e

@mattirn
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.