summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob McDonnell <jacob@simplelittledream.com>2021-12-10 12:05:02 -0500
committerJacob McDonnell <jacob@simplelittledream.com>2021-12-10 12:05:02 -0500
commitf8961a182cb023d941e6a241ee70e316aa8b2617 (patch)
tree81cc19b70acc225cbd2203300158239fe4a86972
parent4fd05739633d1699992a833d59a9e102017e545f (diff)
Added append after, help menu, and listed commands
-rw-r--r--README.md26
-rw-r--r--src/filebuffer.java12
-rw-r--r--src/jed.java44
-rw-r--r--tests/test.txt5
4 files changed, 64 insertions, 23 deletions
diff --git a/README.md b/README.md
index 8cabc1b..97fbb96 100644
--- a/README.md
+++ b/README.md
@@ -2,6 +2,28 @@
Jed is a line mode text editor written in Java. It is similar to ed but not a clone, infact the commands are changed. It currently works but has much room for improvement.
+## Commands
+
+```
+Jed commands:
+q: quits.
+w: writes the file.
+w: filename: writes with inputted name.
+o: filename: opens the file.
+a: appends user input to the end of the file.
+A: appends user input after the current line.
+p: prints the file.
+n: prints the file with line numbers.
+c: deletes and changes the current line.
+d: deletes the current line.
+Any integer: changes to that line number.
+g/expression/: finds and prints the expression.
+%s/expression/newExpression/: replaces an expression with new user input through the whole file.
+<integer, integer>: prefix works with d and s/ex/nex/ for a range of line.
+s/expression/newExpression/: replaces an expression with new user input in the current line.
+h: prints the commands and their description.
+```
+
## Building Jed
```
@@ -11,6 +33,6 @@ sudo cp jed /usr/bin/jed && sudo cp jed.jar /usr/bin/jed.jar
## TODO
-- Rewrite Parser function
+- Add Regex support
-- Add Regex support \ No newline at end of file
+- check and rewrite poorly written functions \ No newline at end of file
diff --git a/src/filebuffer.java b/src/filebuffer.java
index 833f695..7ee27c2 100644
--- a/src/filebuffer.java
+++ b/src/filebuffer.java
@@ -121,7 +121,7 @@ public class filebuffer {
/* deleteLine: deletes line in the range of start to stop */
public void deleteLine(int start, int stop) {
for (int i = start; i < stop; i++) {
- jed.file.buffer.remove(start);
+ buffer.remove(start);
}
}
@@ -161,10 +161,14 @@ public class filebuffer {
buffer.remove(currentLine);
}
- /* append: appends user input to the end of a file */
- public void append(Scanner kbIn) {
+ /* append: appends user input to the end of a file or after the current line */
+ public void append(Scanner kbIn, boolean after) {
String input;
+ int line = currentLine + 1;
while (!(input = kbIn.nextLine()).equals("."))
- buffer.add(input);
+ if (after)
+ buffer.add(line++, input);
+ else
+ buffer.add(input);
}
} \ No newline at end of file
diff --git a/src/jed.java b/src/jed.java
index ddd3718..3aae502 100644
--- a/src/jed.java
+++ b/src/jed.java
@@ -5,7 +5,7 @@ import java.util.Scanner;
public class jed {
private static Scanner kbIn = new Scanner(System.in);
- public static filebuffer file;
+ private static filebuffer file;
public static void main(String args[]) {
String fileName = null;
@@ -31,8 +31,8 @@ public class jed {
private static int parser(String cmd) {
int err = 0;
if (isNum(cmd)) {
- jed.file.changeCurrentLine(Integer.parseInt(cmd) - 1);
- System.out.printf("%d %s\n", jed.file.getCurrentLine() + 1, jed.file.getLine(jed.file.getCurrentLine()));
+ file.changeCurrentLine(Integer.parseInt(cmd) - 1);
+ System.out.printf("%d %s\n", file.getCurrentLine() + 1, file.getLine(file.getCurrentLine()));
} else {
char c = cmd.charAt(0);
switch (c) {
@@ -40,48 +40,54 @@ public class jed {
if (cmd.length() > 1) {
String s[] = new String[2];
s = cmd.split(" ");
- jed.file.changeName(s[1]);
+ file.changeName(s[1]);
}
- err = jed.file.writeFile();
+ err = file.writeFile();
break;
case 'q': // q quits
return 1;
case 'a': // a appends
- jed.file.append(kbIn);
+ file.append(kbIn, false);
+ break;
+ case 'A': // A appends after the current line
+ file.append(kbIn, true);
break;
case 'p': // p prints lines
- jed.file.printFile(false);
+ file.printFile(false);
break;
case 'n': //n prints lines with numbers
- jed.file.printFile(true);
+ file.printFile(true);
break;
case 'c': // c change current line
- jed.file.changeLine(kbIn);
+ file.changeLine(kbIn);
break;
case 'd':
- jed.file.deleteLine(); // d delete current line
+ file.deleteLine(); // d delete current line
break;
case 'g': // g/expression/ finds user input
- jed.file.find(cmd.substring(2, cmd.length() - 1));
+ file.find(cmd.substring(2, cmd.length() - 1));
break;
case '%': // %s/expression/newExpression/ replaces an expression with new user input through the whole file
String s[] = new String[3];
s = cmd.split("/");
- jed.file.replace(0, jed.file.getFileSize(), s[1], s[2]);
+ file.replace(0, file.getFileSize(), s[1], s[2]);
break;
- case '<': // <integer, integer>command works with d and s/ex/nex/
+ case '<': // <integer, integer> prefix works with d and s/ex/nex/
String t[] = new String[2];
t = cmd.split(">");
- jed.file.range(t[0], t[1]);
+ file.range(t[0], t[1]);
break;
case 's': // s/expression/newExpression/ replaces an expression with new user input in the current line
String m[] = new String[3];
m = cmd.split("/");
- jed.file.replace(jed.file.getCurrentLine(), jed.file.getCurrentLine() + 1, m[1], m[2]);
+ file.replace(file.getCurrentLine(), file.getCurrentLine() + 1, m[1], m[2]);
break;
case 'o': // o fileName opens a file
- jed.file.changeName(cmd.substring(2,cmd.length()));
- jed.file.readFile();
+ file.changeName(cmd.substring(2,cmd.length()));
+ file.readFile();
+ break;
+ case 'h': // help command
+ help();
break;
default:
System.out.println("?");
@@ -93,6 +99,10 @@ public class jed {
return 0;
}
+ private static void help() {
+ System.out.println("Jed commands:\nq: quits.\nw: writes the file.\nw: filename: writes with inputted name.\no: filename: opens the file.\na: appends user input to the end of the file.\nA: appends user input after the current line.\np: prints the file.\nn: prints the file with line numbers.\nc: deletes and changes the current line.\nd: deletes the current line.\nAny integer: changes to that line number.\ng/expression/: finds and prints the expression.\n%s/expression/newExpression/: replaces an expression with new user input through the whole file.\n<integer, integer>: prefix works with d and s/ex/nex/ for a range of line.\ns/expression/newExpression/: replaces an expression with new user input in the current line.\nh: prints the commands and their description.");
+ }
+
/* isNum: checks if a string is a number, only to be used for changing lines */
private static boolean isNum(String s) {
try {
diff --git a/tests/test.txt b/tests/test.txt
index c075a0a..58b1c66 100644
--- a/tests/test.txt
+++ b/tests/test.txt
@@ -8,6 +8,11 @@ more test
motestre
test testpenistest test test petestpe
;al;skjfjfjfjjfjfjfjjfjfjfjfjf
+penis
+;alsdkjf;sa
+penis
sdfasdlfkjas;ldf
asdfals;dfjas;ldkfj;a
laskdfj;lasdfkj
+l;askdjf;alsdfja;sldfja;sldf
+al;skfdjdas