Fix carriage return

This commit is contained in:
zontreck 2024-08-24 07:02:52 -07:00
parent efa039b64b
commit 676684397e
5 changed files with 103 additions and 78 deletions

View file

@ -16,7 +16,9 @@ import 'package:libac_dart/consts.dart';
import 'package:libac_dart/utils/IOTools.dart';
Future<int> main(List<String> args) async {
String VERSION = "1.0.082424.0001";
String VERSION = "1.0.082424.0524";
String HEADER =
"Make File System Report\nVersion: $VERSION\nUsing LibAC Version: ${Constants.VERSION}\nAuthor: Tara Piccari\n\n";
Arguments defaults = ArgumentsBuilder.builder()
.withArgument(BoolArgument(
@ -36,14 +38,15 @@ Future<int> main(List<String> args) async {
.build();
Arguments parsed = await CLIHelper.parseArgs(args, Arguments());
String helpTest =
"Make File System Report\nVersion: $VERSION\nUsing LibAC Version: ${Constants.VERSION}\nAuthor: Tara Piccari\n\n${CLIHelper.makeArgCLIHelp(defaults)}";
String helpTest = "$HEADER${CLIHelper.makeArgCLIHelp(defaults)}";
if (parsed.count == 0 || !parsed.hasArg("path") || parsed.hasArg("help")) {
print(helpTest);
return 0;
}
print(HEADER);
bool ascending = false;
if (parsed.hasArg("asc")) ascending = true;
StringArgument pathArg = parsed.getArg("path") as StringArgument;
@ -52,7 +55,7 @@ Future<int> main(List<String> args) async {
cacheSize: true, recursive: true, verbose: parsed.hasArg("o"));
FileInformationCache FIC = FileInformationCache.obtain();
String report =
await FIC.generateHTMLReport(ascending: ascending, VERSION: VERSION);
await generateHTMLReport(FIC, ascending: ascending, VERSION: VERSION);
if (parsed.hasArg("o")) {
prnt("> Generating report...\r");
@ -65,3 +68,52 @@ Future<int> main(List<String> args) async {
return 0;
}
Future<String> generateHTMLReport(FileInformationCache FIC,
{bool ascending = false, String VERSION = ""}) async {
String header = '''
<html>
<title>MKFSReport</title>
<body style="background-color: black;color: #00D2FA">
<h2>File System Report</h2><br/>
<b>Generated by mkfsreport v${VERSION}</b><br/>
<b>Bundled <a href="https://git.zontreck.com/AriasCreations/LibAC-dart">LibAC</a> Version: ${Constants.VERSION}</b><br/>
<pre style="color: #7a0c17">
MKFSREPORT and LibAC are provided free of charge with no implied warranties. The software is provided as-is.
This program was created by Tara Piccari, as a part of the Aria's Creations common code library as a helper utility.
Aria's Creations is a alias for online-only works. Aria is an anagram derived from my name. piccARI, tarA. You place my last name first, take the last 3, skip the first 3 in my first name and take the remaining character(s) and you have my alias.
This utility analyzed the specified folder path to generate a detailed report of directory and file sizes as seen below.
</pre><br/>
''';
header += '''
<table>
<tr>
<th>Type</th>
<th>Path</th>
<th>Size</th>
</tr>
''';
for (var entry in (await FIC.getOrderedList(ascending: ascending))) {
header += '''
<tr>
<td>${entry.isFile ? "FILE" : "DIR"}</td>
<td>${entry.path}</td>
<td>${entry.toString()}</td>
</tr>
''';
}
header += '''
</table>
''';
header += '''
</body>
</html>
''';
return header;
}

View file

@ -1,3 +1,3 @@
class Constants {
static const VERSION = "1.2.082424+0006";
static const VERSION = "1.2.082424+0701";
}

View file

@ -147,55 +147,6 @@ class FileInformationCache {
_registry[path] = info;
}
Future<String> generateHTMLReport(
{bool ascending = false, String VERSION = ""}) async {
String header = '''
<html>
<title>MKFSReport</title>
<body style="background-color: black;color: #00D2FA">
<h2>File System Report</h2><br/>
<b>Generated by mkfsreport v${VERSION}</b><br/>
<b>Bundled <a href="https://git.zontreck.com/AriasCreations/LibAC-dart">LibAC</a> Version: ${Constants.VERSION}</b><br/>
<pre style="color: #7a0c17">
MKFSREPORT and LibAC are provided free of charge with no implied warranties. The software is provided as-is.
This program was created by Tara Piccari, as a part of the Aria's Creations common code library as a helper utility.
Aria's Creations is a alias for online-only works. Aria is an anagram derived from my name. piccARI, tarA. You place my last name first, take the last 3, skip the first 3 in my first name and take the remaining character(s) and you have my alias.
This utility analyzed the specified folder path to generate a detailed report of directory and file sizes as seen below.
</pre><br/>
''';
header += '''
<table>
<tr>
<th>Type</th>
<th>Path</th>
<th>Size</th>
</tr>
''';
for (var entry in (await getOrderedList(ascending: ascending))) {
header += '''
<tr>
<td>${entry.isFile ? "FILE" : "DIR"}</td>
<td>${entry.path}</td>
<td>${entry.toString()}</td>
</tr>
''';
}
header += '''
</table>
''';
header += '''
</body>
</html>
''';
return header;
}
Future<List<FileInfo>> getOrderedList({bool ascending = false}) async {
List<FileInfo> infoList = [];
for (var entry in _registry.entries) {
@ -267,12 +218,12 @@ class FileInfo {
Future<int> getFileSize(String path,
{bool cacheSize = false, bool verbose = false}) async {
if (verbose) prnt("> Checking size of file $path\r");
if (verbose) await prnt("${trunc("> Checking size of file $path")}\r");
/*final fileBytes = await File(path).readAsBytes();
int size = fileBytes.lengthInBytes;*/
final size = await File(path).length();
if (verbose) prnt(">> Size of file $path is $size bytes\r");
if (verbose) await prnt("${trunc(">> Size of file $path")} is $size bytes\r");
if (cacheSize) {
FileInformationCache FIC = FileInformationCache.obtain();
@ -287,10 +238,9 @@ Future<int> getDirectorySize(String path,
bool cacheSize = false,
bool verbose = false}) async {
int totalSize = 0;
if (verbose) prnt("> Check dir size of $path\r");
final entityList = await Directory(path)
.list(recursive: recursive, followLinks: true)
.toList();
if (verbose) await prnt("${trunc("> Check dir size of $path")}\r");
final entityList =
await Directory(path).list(recursive: false, followLinks: true).toList();
await Future.forEach(entityList, (entity) async {
if (entity is File) {
@ -302,7 +252,8 @@ Future<int> getDirectorySize(String path,
}
});
if (verbose) prnt(">> Size of dir $path is $totalSize bytes\r");
if (verbose)
await prnt("${trunc(">> Size of dir $path")} is $totalSize bytes\r");
if (cacheSize) {
FileInformationCache FIC = FileInformationCache.obtain();
@ -313,7 +264,7 @@ Future<int> getDirectorySize(String path,
return totalSize;
}
void prnt(String text) {
Future<void> prnt(String text) async {
// Convert the string to a list of runes to handle escape sequences properly.
final runes = text.runes.toList();
@ -321,23 +272,28 @@ void prnt(String text) {
final char = runes[i];
// Check for escape sequences
if (char == 0x5C && i + 1 < runes.length) {
// '\\' character in UTF-16
final nextChar = runes[i + 1];
if (nextChar == 0x72) {
// 'r' character in UTF-16
// Handle '\r' escape sequence
stdout.write('\r'); // Move the cursor back to the beginning of the line
stdout.write(
' ' * stdout.terminalColumns); // Erase the entire line with spaces
stdout.write(
'\r'); // Move the cursor back to the start of the line again after clearing
i++; // Skip the 'r' character
continue;
}
final charStr = String.fromCharCode(char);
if (charStr == '\r') {
// 'r' character in UTF-16
// Handle '\r' escape sequence
stdout.write('\r'); // Move the cursor back to the beginning of the line
stdout.write(' '.padLeft(
stdout.terminalColumns - 1)); // Erase the entire line with spaces
stdout.write(
'\r'); // Move the cursor back to the start of the line again after clearing
continue;
}
// Write the character as-is
stdout.write(String.fromCharCode(char));
}
await stdout.flush();
}
String trunc(String input) {
if (input.length > stdout.terminalColumns - 32) {
return "${input.substring(0, (stdout.terminalColumns / 2).round())}...";
} else
return input;
}

View file

@ -1,6 +1,6 @@
name: libac_dart
description: "Aria's Creations code library"
version: 1.2.082424+0006
version: 1.2.082424+0701
homepage: "https://zontreck.com"

View file

@ -1,3 +1,5 @@
import 'dart:io';
import 'package:libac_dart/utils/IOTools.dart';
import 'package:test/expect.dart';
import 'package:test/scaffolding.dart';
@ -9,7 +11,7 @@ Future<void> main() async {
});
test("Test directory size checking", () async {
expect(await getDirectorySize("test"), 9046);
expect(await getDirectorySize("test"), 9981);
});
test("Test file info methods", () async {
@ -29,4 +31,19 @@ Future<void> main() async {
expect(list[0].size > ascList[0].size, true);
expect(list[list.length - 1].size == ascList[0].size, true);
});
test("Test prnt", () async {
try {
var i = stdout.terminalColumns;
} catch (E) {
expect(1, 1);
return;
}
await prnt("> Processing");
await prnt("> Process..\r");
await prnt("> Please stand by...\r");
await prnt("\rComplete!\n");
expect(1, 1);
});
}