Skip to content

Commit 6c3473c

Browse files
committed
A quick pass at code cleanup
Signed-off-by: Hans Kokx <[email protected]>
1 parent 2bc06aa commit 6c3473c

8 files changed

+177
-64
lines changed

bin/dexa.dart

+27-40
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import 'package:intl/intl.dart';
77
import 'package:mime/mime.dart';
88
import 'package:path/path.dart';
99

10+
import 'functions/argument_parser.dart';
11+
1012
part 'constants/ansi.dart';
1113
part 'constants/file_sizes.dart';
1214
part 'constants/filetype_icons.dart';
@@ -20,40 +22,19 @@ part 'features/modification_date.dart';
2022
part 'features/permissions.dart';
2123
part 'functions/gather_file_sizes.dart';
2224
part 'functions/get_mime_type.dart';
23-
part 'functions/get_path.dart';
2425
part 'functions/handle_error.dart';
2526
part 'functions/list_directory_contents.dart';
2627

2728
void main(List<String> arguments) async {
2829
exitCode = 0; // presume success
29-
final parser = ArgParser();
30-
late Directory directory;
31-
32-
// Set parser flags
33-
parser.addFlag("all", negatable: false, abbr: 'a');
34-
parser.addFlag("human-readable", negatable: false, abbr: 'h');
35-
parser.addFlag("long", negatable: false, abbr: 'l');
36-
parser.addFlag("headers", negatable: false, abbr: 'H');
37-
// parser.addFlag("recursive", negatable: false, abbr: 'R');
38-
parser.addFlag("icons", negatable: false, abbr: 'I');
39-
40-
final ArgResults argResults = parser.parse(arguments);
41-
42-
// Read parser flags
43-
final Map<String, bool> args = {
44-
'longFileListing': (argResults["long"] == true) ? true : false,
45-
'humanReadableFileSize':
46-
(argResults["human-readable"] == true) ? true : false,
47-
'showHeaders': (argResults["headers"] == true) ? true : false,
48-
// 'listRecursively': (argResults["recursive"] == true) ? true : false,
49-
'showFileTypeIcon': (argResults["icons"] == true) ? true : false,
50-
'listAllFiles': (argResults["all"] == true)
51-
? true
52-
: false, // https://github.com/dart-lang/sdk/issues/40303
53-
};
30+
final ArgParser parser = ArgParser();
31+
32+
final (Directory directory, UserArguments args) = parseArguments(
33+
parser,
34+
arguments: arguments,
35+
);
5436

5537
// List all files and directories in the given path, then sort with dirctories on top
56-
directory = await getPath(argResults);
5738
final List<FileSystemEntity> fileList = [];
5839
final List<FileSystemEntity> files =
5940
await listDirectoryContents(directory, type: FileSystemEntityType.file);
@@ -66,46 +47,52 @@ void main(List<String> arguments) async {
6647
fileList.addAll(files);
6748

6849
// * Main logic starts here
69-
late int? maxFileSizeLengthInDigits;
70-
if (args['showHeaders']! && args['longFileListing']!) {
50+
String output = '';
51+
int maxFileSizeLengthInDigits = 0;
52+
53+
if (args.showHeaders && args.longFileListing) {
7154
maxFileSizeLengthInDigits = await gatherDigitsOfMaxFileSize(fileList);
72-
displayHeaders(args: args, fileSizeDigits: maxFileSizeLengthInDigits);
55+
output += getHeaders(
56+
showFileTypeIcon: args.showFileTypeIcon,
57+
fileSizeDigits: maxFileSizeLengthInDigits,
58+
);
7359
}
7460

7561
for (final FileSystemEntity element in fileList) {
76-
String output = '';
77-
7862
final String currentFile =
7963
element.uri.toFilePath(windows: Platform.isWindows);
8064

8165
try {
8266
final FileStat fileStat = await FileStat.stat(currentFile);
8367

84-
if (args['longFileListing']!) {
85-
if (args['showHeaders']!) {
68+
if (args.longFileListing) {
69+
if (args.showHeaders) {
8670
output += fileType(fileStat);
8771
output += filePermissions(fileStat);
8872

8973
output += fileSize(
9074
fileStat,
91-
fileSizeDigits: maxFileSizeLengthInDigits ?? 0,
92-
args: args,
75+
fileSizeDigits: maxFileSizeLengthInDigits,
76+
humanReadableFileSize: args.humanReadableFileSize,
9377
);
9478

9579
output += fileOwner(fileStat);
9680
output += fileModificationDate(fileStat);
9781
}
9882
}
9983

100-
if (args['showFileTypeIcon']!) {
84+
if (args.showFileTypeIcon) {
10185
final String fileToProcess = directory.path + currentFile;
10286
final FileSystemEntityType type = fileStat.type;
103-
output +=
104-
showFileIcon(fileToProcess, type, headers: args['showHeaders']!);
87+
output += showFileIcon(
88+
fileToProcess,
89+
type,
90+
showHeaders: args.showHeaders,
91+
);
10592
}
10693
output += fileName(element, fileStat, currentFile);
10794

108-
if (args['longFileListing']!) {
95+
if (args.longFileListing) {
10996
output += "\n";
11097
} else {
11198
output += " ";

bin/features/file_icon.dart

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
part of '../dexa.dart';
22

3-
String showFileIcon(String file, FileSystemEntityType type, {bool? headers}) {
3+
String showFileIcon(
4+
String file,
5+
FileSystemEntityType type, {
6+
bool showHeaders = false,
7+
}) {
48
String output = '';
59
String? mimeType;
610
String? icon;
@@ -34,7 +38,7 @@ String showFileIcon(String file, FileSystemEntityType type, {bool? headers}) {
3438
if (stdout.supportsAnsiEscapes) {
3539
output += color!;
3640
}
37-
if (headers == true) {
41+
if (showHeaders) {
3842
output += icon!.padRight(5);
3943
} else {
4044
output += '$icon ';

bin/features/file_size.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ part of '../dexa.dart';
33
String fileSize(
44
FileStat fileStat, {
55
required int fileSizeDigits,
6-
required Map<String, bool> args,
6+
bool humanReadableFileSize = false,
77
}) {
88
late String output;
9-
if (args['humanReadableFileSize']!) {
9+
if (humanReadableFileSize) {
1010
output = fileSizeHumanReadable(fileStat);
1111
} else {
1212
final String nhrfs = nonHumanReadableFileSize(fileStat);

bin/features/headers.dart

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
part of '../dexa.dart';
22

3-
void displayHeaders({
4-
required Map<String, bool> args,
3+
String getHeaders({
4+
bool showFileTypeIcon = false,
55
required int fileSizeDigits,
66
}) {
7+
final String padding = stdout.supportsAnsiEscapes ? " " : '';
78
String header = "";
8-
String padding = '';
9-
10-
if (stdout.supportsAnsiEscapes) {
11-
padding = ' ';
12-
}
139

1410
header += "${"Permissions".underline()} ";
1511
header += "${"Size".underline()}${" " * (fileSizeDigits - 5)} $padding";
1612
// header += "User".underline() + " ";
1713
header += "${"Date Modified".underline()} $padding";
18-
if (args['showFileTypeIcon']!) {
14+
if (showFileTypeIcon) {
1915
header += " ${"Icon".underline()}$padding";
2016
}
17+
2118
header += "${"Name".underline()} $padding";
2219

2320
header += "\n";
24-
stdout.write(header);
21+
22+
return header;
2523
}

bin/functions/argument_parser.dart

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import 'dart:io';
2+
3+
import 'package:args/args.dart';
4+
5+
/// Parses arguments passed into the program.
6+
(Directory, UserArguments) parseArguments(
7+
ArgParser parser, {
8+
List<String>? arguments,
9+
}) {
10+
Argument.values.map(
11+
(e) => parser.addFlag(e.name, negatable: false, abbr: e.abbr),
12+
);
13+
14+
final List<String> args = [if (arguments != null) ...arguments];
15+
16+
final ArgResults argResults = parser.parse(args);
17+
18+
final UserArguments parsedArguments = UserArguments(
19+
longFileListing: (argResults["long"] == true) ? true : false,
20+
humanReadableFileSize:
21+
(argResults["human-readable"] == true) ? true : false,
22+
showHeaders: (argResults["headers"] == true) ? true : false,
23+
showFileTypeIcon: (argResults["icons"] == true) ? true : false,
24+
listAllFiles: (argResults["all"] == true)
25+
? true
26+
: false, // https://github.com/dart-lang/sdk/issues/40303
27+
);
28+
29+
final Directory directory = Directory.fromUri(
30+
Uri.directory(argResults.rest.first),
31+
);
32+
33+
return (directory, parsedArguments);
34+
}
35+
36+
enum Argument {
37+
all("all", "a"),
38+
humanReadable("human-readable", "h"),
39+
long("long", "l"),
40+
headers("headers", "H"),
41+
icons("icons", "I");
42+
43+
final String name;
44+
final String abbr;
45+
46+
const Argument(this.name, this.abbr);
47+
}
48+
49+
class UserArguments {
50+
final bool longFileListing;
51+
final bool humanReadableFileSize;
52+
final bool showHeaders;
53+
final bool showFileTypeIcon;
54+
final bool listAllFiles;
55+
56+
const UserArguments({
57+
this.longFileListing = false,
58+
this.humanReadableFileSize = false,
59+
this.showHeaders = false,
60+
this.showFileTypeIcon = false,
61+
this.listAllFiles = false,
62+
});
63+
}

bin/functions/get_path.dart

-8
This file was deleted.

bin/functions/list_directory_contents.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ Future<List<FileSystemEntity>> listDirectoryContents(
1010
final Stream<FileSystemEntity> lister = dir.list(recursive: false);
1111

1212
lister.listen(
13-
(file) async {
14-
final FileStat currentFileStat = await FileStat.stat(file.path);
15-
if (currentFileStat.type == type) {
13+
(FileSystemEntity file) async {
14+
final FileStat fileStatistics = await FileStat.stat(file.path);
15+
if (fileStatistics.type == type) {
1616
files.add(file);
1717
}
1818
},

pubspec.lock

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Generated by pub
2+
# See https://dart.dev/tools/pub/glossary#lockfile
3+
packages:
4+
args:
5+
dependency: "direct main"
6+
description:
7+
name: args
8+
sha256: eef6c46b622e0494a36c5a12d10d77fb4e855501a91c1b9ef9339326e58f0596
9+
url: "https://pub.dev"
10+
source: hosted
11+
version: "2.4.2"
12+
clock:
13+
dependency: transitive
14+
description:
15+
name: clock
16+
sha256: cb6d7f03e1de671e34607e909a7213e31d7752be4fb66a86d29fe1eb14bfb5cf
17+
url: "https://pub.dev"
18+
source: hosted
19+
version: "1.1.1"
20+
flutter_lints:
21+
dependency: "direct dev"
22+
description:
23+
name: flutter_lints
24+
sha256: aeb0b80a8b3709709c9cc496cdc027c5b3216796bc0af0ce1007eaf24464fd4c
25+
url: "https://pub.dev"
26+
source: hosted
27+
version: "2.0.1"
28+
intl:
29+
dependency: "direct main"
30+
description:
31+
name: intl
32+
sha256: "3bc132a9dbce73a7e4a21a17d06e1878839ffbf975568bc875c60537824b0c4d"
33+
url: "https://pub.dev"
34+
source: hosted
35+
version: "0.18.1"
36+
lints:
37+
dependency: transitive
38+
description:
39+
name: lints
40+
sha256: "0a217c6c989d21039f1498c3ed9f3ed71b354e69873f13a8dfc3c9fe76f1b452"
41+
url: "https://pub.dev"
42+
source: hosted
43+
version: "2.1.1"
44+
meta:
45+
dependency: transitive
46+
description:
47+
name: meta
48+
sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3"
49+
url: "https://pub.dev"
50+
source: hosted
51+
version: "1.9.1"
52+
mime:
53+
dependency: "direct main"
54+
description:
55+
name: mime
56+
sha256: e4ff8e8564c03f255408decd16e7899da1733852a9110a58fe6d1b817684a63e
57+
url: "https://pub.dev"
58+
source: hosted
59+
version: "1.0.4"
60+
path:
61+
dependency: "direct main"
62+
description:
63+
name: path
64+
sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917"
65+
url: "https://pub.dev"
66+
source: hosted
67+
version: "1.8.3"
68+
sdks:
69+
dart: ">=3.0.0 <4.0.0"

0 commit comments

Comments
 (0)