From f8961a182cb023d941e6a241ee70e316aa8b2617 Mon Sep 17 00:00:00 2001 From: Jacob McDonnell Date: Fri, 10 Dec 2021 12:05:02 -0500 Subject: Added append after, help menu, and listed commands --- src/filebuffer.java | 12 ++++++++---- src/jed.java | 44 +++++++++++++++++++++++++++----------------- 2 files changed, 35 insertions(+), 21 deletions(-) (limited to 'src') 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 '<': // command works with d and s/ex/nex/ + case '<': // 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: 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 { -- cgit v1.2.3