|
| 1 | +/* |
| 2 | +* JTerm - a cross-platform terminal |
| 3 | +* Copyright (C) 2017 Sergix, NCSGeek |
| 4 | +* |
| 5 | +* This program is free software: you can redistribute it and/or modify |
| 6 | +* it under the terms of the GNU General Public License as published by |
| 7 | +* the Free Software Foundation, either version 3 of the License, or |
| 8 | +* (at your option) any later version. |
| 9 | +
|
| 10 | +* This program is distributed in the hope that it will be useful, |
| 11 | +* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +* GNU General Public License for more details. |
| 14 | +
|
| 15 | +* You should have received a copy of the GNU General Public License |
| 16 | +* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +*/ |
| 18 | + |
| 19 | +package main.java.jterm; |
| 20 | + |
| 21 | +import java.io.*; |
| 22 | +import java.util.ArrayList; |
| 23 | + |
| 24 | +public class Dir |
| 25 | +{ |
| 26 | + |
| 27 | + /* |
| 28 | + * Dir() void |
| 29 | + * |
| 30 | + * Constructor for calling methods. |
| 31 | + * |
| 32 | + * ArrayList<String> options - command options |
| 33 | + */ |
| 34 | + public Dir(ArrayList<String> options) { } |
| 35 | + |
| 36 | + /* |
| 37 | + * Process() void |
| 38 | + * |
| 39 | + * Display help information. |
| 40 | + * |
| 41 | + * ArrayList<String> options - command options |
| 42 | + */ |
| 43 | + public static void Process(ArrayList<String> options) |
| 44 | + { |
| 45 | + |
| 46 | + // Display help information |
| 47 | + System.out.println("Directory Commands\n\nls\tcd\nchdir\tpwd\nmd"); |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + /* |
| 52 | + * Ls() void (@pmorgan3) |
| 53 | + * |
| 54 | + * Prints the contents of a specified directory |
| 55 | + * to a file. |
| 56 | + * |
| 57 | + * ArrayList<String> options - command options |
| 58 | + * |
| 59 | + * -f |
| 60 | + * Changes the output format to only file |
| 61 | + * and directory names |
| 62 | + * -h |
| 63 | + * Prints help information |
| 64 | + * directory [...] |
| 65 | + * Prints this directory rather than the |
| 66 | + * current working directory. |
| 67 | + * |
| 68 | + * Examples |
| 69 | + * |
| 70 | + * Ls(options); |
| 71 | + * => [Contents of "dir/"] |
| 72 | + * => F RW myFile.txt 2 KB |
| 73 | + */ |
| 74 | + public static void Ls(ArrayList<String> options) throws NullPointerException |
| 75 | + { |
| 76 | + |
| 77 | + System.out.println(options); |
| 78 | + |
| 79 | + String path = JTerm.currentDirectory; |
| 80 | + boolean printFull = true; |
| 81 | + |
| 82 | + for (String option: options) |
| 83 | + { |
| 84 | + if (option.equals("-f")) |
| 85 | + printFull = false; |
| 86 | + |
| 87 | + else if (option.equals("-h")) |
| 88 | + { |
| 89 | + System.out.println("Command syntax:\n\tdir [-f] [-h] [directory]\n\nPrints a detailed table of the current working directory's subfolders and files."); |
| 90 | + return; |
| 91 | + |
| 92 | + } |
| 93 | + else |
| 94 | + path = option; |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + File dir = new File(path); |
| 99 | + File[] files = dir.listFiles(); |
| 100 | + |
| 101 | + /* |
| 102 | + * Format of output: |
| 103 | + * [FD] [RWHE] [filename] [size in KB] |
| 104 | + * |
| 105 | + * Prefix definitions: |
| 106 | + * F -- File |
| 107 | + * D -- Directory |
| 108 | + * R -- Readable |
| 109 | + * W -- Writable |
| 110 | + * H -- Hidden |
| 111 | + * |
| 112 | + * Example: |
| 113 | + * F RW myfile.txt 5 KB |
| 114 | + */ |
| 115 | + System.out.println("[Contents of \"" + path + "\"]"); |
| 116 | + for (File file: files) |
| 117 | + { |
| 118 | + if (printFull) |
| 119 | + System.out.println("\t" + (file.isFile() ? "F " : "D ") + (file.canRead() ? "R" : "") + (file.canWrite() ? "W" : "") + (file.isHidden() ? "H" : "") + "\t" + file.getName() + (file.getName().length() < 8 ? "\t\t\t" : (file.getName().length() > 15 ? "\t" : "\t\t")) + (file.length() / 1024) + " KB"); |
| 120 | + |
| 121 | + else |
| 122 | + System.out.println("\t" + file.getName()); |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + /* |
| 130 | + * Cd() void |
| 131 | + * |
| 132 | + * Changes the working directory to the specified |
| 133 | + * input. |
| 134 | + * |
| 135 | + * ArrayList<String> options - command options |
| 136 | + * |
| 137 | + * -h |
| 138 | + * Prints help information |
| 139 | + * directory [...] |
| 140 | + * Path to change the working directory to. |
| 141 | + */ |
| 142 | + public static void Cd(ArrayList<String> options) |
| 143 | + { |
| 144 | + |
| 145 | + String newDirectory = ""; |
| 146 | + |
| 147 | + for (String option: options) |
| 148 | + { |
| 149 | + if (option.equals("-h")) |
| 150 | + { |
| 151 | + System.out.println("Command syntax:\n\tcd [-h] directory\n\nChanges the working directory to the path specified."); |
| 152 | + return; |
| 153 | + |
| 154 | + } |
| 155 | + else |
| 156 | + newDirectory += option + " "; |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + newDirectory = newDirectory.trim(); |
| 161 | + |
| 162 | + if (newDirectory.startsWith("\"") && newDirectory.endsWith("\"")) |
| 163 | + { |
| 164 | + newDirectory = newDirectory.substring(1, newDirectory.length()); |
| 165 | + newDirectory = newDirectory.substring(0, newDirectory.length() - 1); |
| 166 | + |
| 167 | + } |
| 168 | + |
| 169 | + if (newDirectory.equals("")) |
| 170 | + { |
| 171 | + System.out.println("Path not specified. Type \"cd -h\" for more information."); |
| 172 | + return; |
| 173 | + |
| 174 | + } |
| 175 | + |
| 176 | + // Test if the input exists and if it is a directory |
| 177 | + File dir = new File(newDirectory); |
| 178 | + File newDir = new File(JTerm.currentDirectory + newDirectory); |
| 179 | + |
| 180 | + if (newDirectory.equals("/")) |
| 181 | + newDirectory = "/"; |
| 182 | + |
| 183 | + else if (newDir.exists() && newDir.isDirectory()) |
| 184 | + newDirectory = JTerm.currentDirectory + newDirectory; |
| 185 | + |
| 186 | + else if ((!dir.exists() || !dir.isDirectory()) && (!newDir.exists() || !newDir.isDirectory())) |
| 187 | + { |
| 188 | + System.out.println("ERROR: Directory \"" + newDirectory + "\" is either does not exist or is not a valid directory."); |
| 189 | + return; |
| 190 | + |
| 191 | + } |
| 192 | + |
| 193 | + if (!newDirectory.endsWith("/")) |
| 194 | + newDirectory += "/"; |
| 195 | + |
| 196 | + // It does exist, and it is a directory, so just change the global working directory variable to the input |
| 197 | + JTerm.currentDirectory = newDirectory; |
| 198 | + |
| 199 | + |
| 200 | + } |
| 201 | + |
| 202 | + /* |
| 203 | + * Chdir() void |
| 204 | + * |
| 205 | + * Identical to 'cd'; calls Cd(). |
| 206 | + * |
| 207 | + * ArrayList<String> options - command options |
| 208 | + */ |
| 209 | + public static void Chdir(ArrayList<String> options) |
| 210 | + { |
| 211 | + |
| 212 | + Cd(options); |
| 213 | + |
| 214 | + } |
| 215 | + |
| 216 | + /* |
| 217 | + * Pwd() void |
| 218 | + * |
| 219 | + * Prints the working directory to the console. |
| 220 | + * |
| 221 | + * ArrayList<String> options - command options |
| 222 | + * |
| 223 | + * -h |
| 224 | + * Prints help information |
| 225 | + */ |
| 226 | + public static void Pwd(ArrayList<String> options) |
| 227 | + { |
| 228 | + |
| 229 | + for (String option: options) |
| 230 | + { |
| 231 | + if (option.equals("-h")) |
| 232 | + { |
| 233 | + System.out.println("Command syntax:\n\tpwd\n\nPrints the current Working Directory."); |
| 234 | + return; |
| 235 | + |
| 236 | + } |
| 237 | + |
| 238 | + } |
| 239 | + |
| 240 | + // Simply print the currentDirectory variable to the console |
| 241 | + System.out.println(JTerm.currentDirectory); |
| 242 | + |
| 243 | + } |
| 244 | + |
| 245 | + /* |
| 246 | + * Md() void |
| 247 | + * |
| 248 | + * Creates a new directory. |
| 249 | + * |
| 250 | + * ArrayList<String> options - command options |
| 251 | + * |
| 252 | + * -h |
| 253 | + * Prints help information |
| 254 | + * name [...] |
| 255 | + * Name of the new directory |
| 256 | + */ |
| 257 | + public static void Md(ArrayList<String> options) |
| 258 | + { |
| 259 | + |
| 260 | + String name = ""; |
| 261 | + |
| 262 | + for (String option: options) |
| 263 | + { |
| 264 | + if (option.equals("-h")) |
| 265 | + { |
| 266 | + System.out.println("Command syntax:\n\tmd [-h] name\n\nCreates a new directory."); |
| 267 | + return; |
| 268 | + |
| 269 | + } |
| 270 | + else |
| 271 | + name += option + " "; |
| 272 | + |
| 273 | + } |
| 274 | + |
| 275 | + name = name.trim(); |
| 276 | + name = JTerm.currentDirectory + name; |
| 277 | + |
| 278 | + File dir = new File(name); |
| 279 | + dir.mkdir(); |
| 280 | + |
| 281 | + } |
| 282 | + |
| 283 | +} |
0 commit comments