Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# truffula
# Truffula

An assignment for printing colorful file trees. Please see [instructions.md](instructions.md) for instructions.
A fun project done in class at GRC (Green River College). This project prints a directory tree in a vertical, colorful format.

It uses built-in Java utilities like Color, file manipulation, and data structures such as List and Array.

This project was part of Week 12 of my second quarter at GRC, during which we focused on data structures and commonly used algorithms.
30 changes: 30 additions & 0 deletions notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,42 @@ PLEASE MAKE FREQUENT COMMITS AS YOU FILL OUT THIS FILE.

## App.java

- Have no idea

- Looks like We have to implement something

- -h shows hidden files -nc disables color for files

## ConsoleColor.java

- Obstructs color?

- All setup I believe

- ansi text color codes always starts with \033[0;incrementfrom30-37 reset at 0]

- We define our objects and colors
the code provided defines a method getCode() and provides us with all the calls in english vs \033 blah blah

## ColorPrinter.java / ColorPrinterTest.java

- Example code to print red text
ColorPrinter printer = new ColorPrinter(System.out);
printer.setCurrentColor(ConsoleColor.RED);
printer.println("This is red text");

## TruffulaOptions.java / TruffulaOptionsTest.java

- Controller? Helps with functionality for displaying hidden files and displaying
-h for showing hidden files
-nc for displaying no color to the file

- 3 Constant variables to be worked with at declared at the top of the file

- methods that could be worked with?

## TruffulaPrinter.java / TruffulaPrinterTest.java

- We have to utilize colorSequence?

## AlphabeticalFileSorter.java
3 changes: 3 additions & 0 deletions src/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ public class App {
public static void main(String[] args) throws Exception {
// TODO: Implement this
// You should create a TruffulaOptions object using the args and
TruffulaOptions newOptions = new TruffulaOptions(args);
// pass it to a new TruffulaPrinter that uses System.out
TruffulaPrinter newPrinter = new TruffulaPrinter(newOptions, System.out);
// Then, call printTree on the TruffulaPrinter
newPrinter.printTree();
}
}
6 changes: 6 additions & 0 deletions src/ColorPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ public void print(String message) {
*/
public void print(String message, boolean reset) {
// TODO: Implement this!
printStream.print(currentColor.getCode());
printStream.print(message);
if(reset)
{
printStream.print(ConsoleColor.RESET.getCode());
}
}

/**
Expand Down
56 changes: 56 additions & 0 deletions src/ColorPrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,60 @@ void testPrintlnWithRedColorAndReset() {
// Assert: Verify the printed output
assertEquals(expectedOutput, outputStream.toString());
}


@Test
void testPrintlnWithGreenColorAndReset() {
// Arrange: Capture the printed output
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(outputStream);

ColorPrinter printer = new ColorPrinter(printStream);
printer.setCurrentColor(ConsoleColor.GREEN);

// Act: Print the message
String message = "I speak for the trees";
printer.println(message);


String expectedOutput = ConsoleColor.GREEN + "I speak for the trees" + System.lineSeparator() + ConsoleColor.RESET;

// Assert: Verify the printed output
assertEquals(expectedOutput, outputStream.toString());
}

@Test
void testPrintlnDefaultColor() {
// Arrange: Capture the printed output
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(outputStream);

ColorPrinter printer = new ColorPrinter(printStream);

// Assert: Verify the printed output
assertEquals(ConsoleColor.WHITE, printer.getCurrentColor());
}


@Test
void testPrintlnNoReset() {
// Arrange: Capture the printed output
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(outputStream);

ColorPrinter printer = new ColorPrinter(printStream);
printer.setCurrentColor(ConsoleColor.RED);

// Act: Print the message
String message = "I speak for the trees";
printer.println(message, false);


String expectedOutput = ConsoleColor.RED + "I speak for the trees" + System.lineSeparator();

// Assert: Verify the printed output
assertEquals(expectedOutput, outputStream.toString());
}


}
44 changes: 41 additions & 3 deletions src/TruffulaOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,47 @@ public boolean isUseColor() {
*/
public TruffulaOptions(String[] args) throws IllegalArgumentException, FileNotFoundException {
// TODO: Replace the below lines with your implementation
root = null;
showHidden = false;
useColor = false;
boolean hidden = false;
boolean color = true;
String tempRoot = null;

for(int i = 0; i < args.length; i++)
{

String arg = args[i];


if(arg.equals("-h"))
{
hidden = true;
}
else if(arg.equals("-nc"))
{
color = false;
}
else if(i == args.length - 1)
{
tempRoot = arg;
}
else
{
throw new IllegalArgumentException("Invalid entry: " + arg);
}
}

File path = new File(tempRoot);
if(!path.exists())
{
throw new FileNotFoundException("Directory not available: " + tempRoot);
}
if(!path.isDirectory())
{
throw new FileNotFoundException("Path not a directory: " + tempRoot);
}

root = new File(tempRoot);
showHidden = hidden;
useColor = color;
}

/**
Expand Down
54 changes: 52 additions & 2 deletions src/TruffulaPrinter.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.io.Console;
import java.io.File;
import java.io.PrintStream;
import java.util.List;

Expand Down Expand Up @@ -102,6 +104,46 @@ public TruffulaPrinter(TruffulaOptions options, PrintStream outStream, List<Cons
* README.md
* zebra.txt
*/

//Wave 4
private void printDirectoryHelper(File dir, int depth)
{
File[] files = dir.listFiles();

if(files == null) return;

//Wave 7
files = AlphabeticalFileSorter.sort(files);

for(File file: files)
{
//Wave 5
if(!options.isShowHidden() && file.isHidden()) continue;

String indented = " ".repeat(depth);
String name = file.getName() + (file.isDirectory() ? "/" : "");

//Wave 6
if(options.isUseColor())
{
ConsoleColor color = DEFAULT_COLOR_SEQUENCE.get(depth % DEFAULT_COLOR_SEQUENCE.size());
out.setCurrentColor(color);
}
else
{
out.setCurrentColor(ConsoleColor.WHITE);
}

out.println(indented + name);
if(file.isDirectory())
{
printDirectoryHelper(file, depth + 1);
}
}

}


public void printTree() {
// TODO: Implement this!
// REQUIRED: ONLY use java.io, DO NOT use java.nio
Expand All @@ -111,8 +153,16 @@ public void printTree() {
// - For Wave 6: Use AlphabeticalFileSorter
// DO NOT USE SYSTEM.OUT.PRINTLN
// USE out.println instead (will use your ColorPrinter)

File root = options.getRoot(); // Returns file we will be working with

if(root == null || !root.exists())
{
out.println("Root was not found");
return;
}

out.println("printTree was called!");
out.println("My options are: " + options);
out.println(root.getName() + "/");
printDirectoryHelper(root, 1);
}
}
80 changes: 80 additions & 0 deletions src/TruffulaPrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,84 @@ public void testPrintTree_ExactOutput_WithCustomPrintStream(@TempDir File tempDi
// Assert that the output matches the expected output exactly
assertEquals(expected.toString(), output);
}



@Test
public void testWave4OutputWorkingCorrectly(@TempDir File tempDir) throws IOException{
File myFolder = new File(tempDir, "myFolder");
assertTrue(myFolder.mkdir(), "myFolder should be created");

// Create visible files in myFolder
File apple = new File(myFolder, "Apple.txt");
File banana = new File(myFolder, "banana.txt");
File zebra = new File(myFolder, "zebra.txt");
apple.createNewFile();
banana.createNewFile();
zebra.createNewFile();

TruffulaOptions options = new TruffulaOptions(myFolder, true, false);

ByteArrayOutputStream output = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(output);
TruffulaPrinter printer = new TruffulaPrinter(options, ps);
printer.printTree();

String result = output.toString();

assertTrue(result.contains("Apple.txt"));
assertTrue(result.contains("banana.txt"));
assertTrue(result.contains("zebra.txt"));
}


@Test
public void testWave5HiddenFiles(@TempDir File tempDir) throws IOException{
File myFolder = new File(tempDir, "myFolder");
assertTrue(myFolder.mkdir(), "myFolder should be created");

// Create visible files in myFolder
File apple = new File(myFolder, "visible.txt");
assertTrue(apple.createNewFile());

createHiddenFile(myFolder, ".hidden.txt");
TruffulaOptions options = new TruffulaOptions(myFolder, false, false);

ByteArrayOutputStream output = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(output);
TruffulaPrinter printer = new TruffulaPrinter(options, ps);
printer.printTree();

String result = output.toString();

assertTrue(result.contains("visible.txt"));
assertTrue(!result.contains(".hidden.txt"));
}

@Test
public void testWave6ColorFiles(@TempDir File tempDir) throws IOException{
File root = new File(tempDir, "root");
assertTrue(root.mkdir());

File subDir1 = new File(root, "subDir1");
assertTrue(subDir1.mkdir());

File subDir2 = new File(subDir1, "subDir2");
assertTrue(subDir2.mkdir());

TruffulaOptions options = new TruffulaOptions(root, true, true); // useColor = true
ByteArrayOutputStream output = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(output);
TruffulaPrinter printer = new TruffulaPrinter(options, ps);

printer.printTree();

String result = output.toString();

// Check colors cycle correctly: WHITE, PURPLE, YELLOW
assertTrue(result.contains(ConsoleColor.WHITE.getCode()), "Level 0 should be white");
assertTrue(result.contains(ConsoleColor.PURPLE.getCode()), "Level 1 should be purple");
assertTrue(result.contains(ConsoleColor.YELLOW.getCode()), "Level 2 should be yellow");
}

}