Skip to content

Commit

Permalink
Initial commit for export capabilities
Browse files Browse the repository at this point in the history
Also updated library to v2023.07.14
Updated debugPrint() to debugPrintln()
  • Loading branch information
frossm committed Jul 15, 2023
1 parent 3e0b154 commit 171bf1d
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 75 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ buildNumber.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar
dirsize*.snap
*.csv
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>org.fross</groupId>
<artifactId>dirsize</artifactId>
<version>2.2.18</version>
<version>2.2.20</version>

<name>dirsize</name>

Expand Down Expand Up @@ -186,7 +186,7 @@
<dependency>
<groupId>org.fross</groupId>
<artifactId>library</artifactId>
<version>2022.11.21</version>
<version>2023.07.14</version>
</dependency>

</dependencies>
Expand Down
4 changes: 2 additions & 2 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: dirsize
version: '2.2.18'
version: '2.2.20'
summary: The Command line Directory Reporting Tool
description: |
DirSize is a directory reporting tool. It recursively scans
Expand Down Expand Up @@ -36,7 +36,7 @@ parts:
plugin: maven
source: https://github.com/frossm/library.git
source-type: git
source-tag: 'v2022.11.21'
source-tag: 'v2023.07.14'
maven-options: [install]

dirsize:
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/fross/dirsize/Benchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class Benchmark {
*/
public Benchmark() {
startTime = LocalTime.now();
Output.debugPrint("Benchmark startTime = " + startTime.toString());
Output.debugPrintln("Benchmark startTime = " + startTime.toString());
}

/**
Expand All @@ -60,8 +60,8 @@ public long Stop() {

delta = ChronoUnit.MILLIS.between(startTime, endTime);

Output.debugPrint("Benchmark endTime = " + endTime.toString());
Output.debugPrint("Milliseconds delta = " + delta);
Output.debugPrintln("Benchmark endTime = " + endTime.toString());
Output.debugPrintln("Milliseconds delta = " + delta);

return (delta);
}
Expand Down
141 changes: 141 additions & 0 deletions src/main/java/org/fross/dirsize/Export.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/******************************************************************************
* DirSize
*
* DirSize is a simple command line based directory size reporting tool
*
* Copyright (c) 2019-2023 Michael Fross
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
******************************************************************************/
package org.fross.dirsize;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.fross.library.Output;
import org.fusesource.jansi.Ansi;

public class Export {
File exportFile = null;
List<String> entryDirName = new ArrayList<>();
List<Long> entryTotalSize = new ArrayList<>();
List<Long> entryTotalFiles = new ArrayList<>();

/**
* Constructor: Set export file via passed FILE
*
* @param f
*/
public Export() {

}

public Export(String f) {
setExportFilename(f);
}

/**
* setExportFilename(): Overwrite an existing output filename
*
* @param fn
*/
public void setExportFilename(String fn) {
exportFile = new File(fn);
Output.debugPrintln("Output file set to: '" + exportFile.getName() + "'");
}

/**
* addExportLine(): Add a line to the output lists done after each directory scan
*
* @param directory
* @param totalSize
* @param totalFiles
*/
public void addExportLine(String directory, long totalSize, long totalFiles) {
entryDirName.add(directory);
entryTotalSize.add(totalSize);
entryTotalFiles.add(totalFiles);
}

/**
* writeToDisk(): Dump the contents of all directories to the export file
*
*/
public boolean writeToDisk() {
try {
if (exportFile.canWrite()) {
FileWriter exportFW;
try {
// Define the output file
exportFW = new FileWriter(exportFile);

// Output the header to the CSV file
exportFW.append("\"" + "Directory" + "\",\"" + "Size in Bytes" + "\",\"" + "Files" + "\"\n");

// Loop through the results and export the output
for (int i = 0; i < entryDirName.size(); i++) {
exportFW.append("\"" + entryDirName.get(i) + "\",");
exportFW.append("\"" + entryTotalSize.get(i) + "\",");
exportFW.append("\"" + entryTotalFiles.get(i) + "\"");
exportFW.append("\n");
}
exportFW.flush();
exportFW.close();

Output.printColorln(Ansi.Color.CYAN, "\nExport Completed to file: " + exportFile.getAbsolutePath());

} catch (IOException ex) {
Output.printColorln(Ansi.Color.RED, "Error writing to export file: " + ex.getMessage());
}
}
} catch (Exception ex) {
return false;
}

return true;

}

/**
* createNewFile(): Creates a new file (duh)
* @return
*/
public boolean createNewFile() {
try {
return exportFile.createNewFile();
} catch (Exception ex) {
Output.printColorln(Ansi.Color.RED, ex.getMessage());
return false;
}
}

/**
* getName(): Returns the name of the file
*
* @return
*/
public String getName() {
return exportFile.getName();
}

}
Loading

0 comments on commit 171bf1d

Please sign in to comment.