Skip to content

Instantly share code, notes, and snippets.

@coldcue
Created October 2, 2013 10:09
Show Gist options
  • Save coldcue/6791562 to your computer and use it in GitHub Desktop.
Save coldcue/6791562 to your computer and use it in GitHub Desktop.
package com.cmdapp;
/**
* Created with IntelliJ IDEA.
* User: Andrew
* Date: 10/1/13
* Time: 5:02 PM
* To change this template use File | Settings | File Templates.
*/
public enum Commands {
EXIT, RECLIST, PWD, CD, LS, RM, LENGTH, TAIL, GREP;
}
package com.cmdapp;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
public class Main {
private static File location = new File(System.getProperty("user.dir"));
public static void main(String[] args) throws IOException {
Scanner cli = new Scanner(System.in);
//noinspection InfiniteLoopStatement
while (true) {
String[] cmdLine = cli.nextLine().split(" ");
List<String> params = new ArrayList<String>();
Collections.addAll(params, cmdLine);
Commands command = Commands.valueOf(cmdLine[0].toUpperCase());
switch (command) {
case EXIT:
exit();
break;
case RECLIST:
reclist();
break;
case PWD:
pwd();
break;
case CD:
cd(cmdLine[1]);
break;
case LS:
ls(params);
break;
case RM:
rm(cmdLine[1]);
break;
case LENGTH:
length(cmdLine[1]);
break;
case TAIL:
tail(cmdLine[1], params);
break;
case GREP:
grep(cmdLine[1], cmdLine[2]);
break;
}
}
}
private static void grep(String fileName, String regexp) throws IOException {
File file = new File(fileName);
if (file.exists()) {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
if (line.matches(regexp)) System.out.println(line);
}
reader.close();
} else System.out.println("Hiba");
}
private static void tail(String fileName, List<String> params) throws IOException {
int n = 10;
if (params.contains("-n"))
n = Integer.parseInt(params.get(params.indexOf("-n") + 1));
File file = new File(fileName);
if (file.exists()) {
BufferedReader reader = new BufferedReader(new FileReader(file));
Queue<String> lines = new ArrayBlockingQueue<String>(n);
String line;
while ((line = reader.readLine()) != null) {
if (!lines.offer(line))
lines.poll();
lines.offer(line);
}
reader.close();
for (String ln : lines) {
System.out.println(ln);
}
} else System.out.println("Hiba");
}
private static void length(String fileName) {
File file = new File(fileName);
if (file.exists()) {
System.out.println(file.getTotalSpace());
} else System.out.println("Hiba");
}
private static void rm(String fileName) {
File file = new File(fileName);
if (!file.delete()) System.out.println("Hiba");
}
private static void ls(List<String> params) {
File[] files = location.listFiles();
for (File file : files != null ? files : new File[0]) {
if (params.contains("-l"))
System.out.println(((file.isDirectory()) ? "d" : "f") + " " + file.getName());
else
System.out.println(file.getName());
}
}
private static void exit() {
System.exit(0);
}
private static void reclist() {
reclist(location, 1);
}
private static void reclist(File dir, int level) {
char[] temp = new char[level];
Arrays.fill(temp, '-');
String levelPrefix = new String(temp);
File[] files = dir.listFiles();
for (File file : files != null ? files : new File[0]) {
System.out.println(levelPrefix + file.getName());
if (file.isDirectory()) reclist(file, level + 1);
}
}
private static void pwd() {
System.out.println(location.getAbsolutePath());
}
private static void cd(String dir) {
if (dir.equals("..")) location = location.getParentFile();
File[] files = location.listFiles();
for (File file : files != null ? files : new File[0]) {
if (file.getName().equals(dir)) {
if (!file.isDirectory()) System.out.println("Not directory");
location = file;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment