@@ -7,6 +7,8 @@ import 'package:intl/intl.dart';
7
7
import 'package:mime/mime.dart' ;
8
8
import 'package:path/path.dart' ;
9
9
10
+ import 'functions/argument_parser.dart' ;
11
+
10
12
part 'constants/ansi.dart' ;
11
13
part 'constants/file_sizes.dart' ;
12
14
part 'constants/filetype_icons.dart' ;
@@ -20,40 +22,19 @@ part 'features/modification_date.dart';
20
22
part 'features/permissions.dart' ;
21
23
part 'functions/gather_file_sizes.dart' ;
22
24
part 'functions/get_mime_type.dart' ;
23
- part 'functions/get_path.dart' ;
24
25
part 'functions/handle_error.dart' ;
25
26
part 'functions/list_directory_contents.dart' ;
26
27
27
28
void main (List <String > arguments) async {
28
29
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
+ );
54
36
55
37
// List all files and directories in the given path, then sort with dirctories on top
56
- directory = await getPath (argResults);
57
38
final List <FileSystemEntity > fileList = [];
58
39
final List <FileSystemEntity > files =
59
40
await listDirectoryContents (directory, type: FileSystemEntityType .file);
@@ -66,46 +47,52 @@ void main(List<String> arguments) async {
66
47
fileList.addAll (files);
67
48
68
49
// * 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) {
71
54
maxFileSizeLengthInDigits = await gatherDigitsOfMaxFileSize (fileList);
72
- displayHeaders (args: args, fileSizeDigits: maxFileSizeLengthInDigits);
55
+ output += getHeaders (
56
+ showFileTypeIcon: args.showFileTypeIcon,
57
+ fileSizeDigits: maxFileSizeLengthInDigits,
58
+ );
73
59
}
74
60
75
61
for (final FileSystemEntity element in fileList) {
76
- String output = '' ;
77
-
78
62
final String currentFile =
79
63
element.uri.toFilePath (windows: Platform .isWindows);
80
64
81
65
try {
82
66
final FileStat fileStat = await FileStat .stat (currentFile);
83
67
84
- if (args[ ' longFileListing' ] ! ) {
85
- if (args[ ' showHeaders' ] ! ) {
68
+ if (args. longFileListing) {
69
+ if (args. showHeaders) {
86
70
output += fileType (fileStat);
87
71
output += filePermissions (fileStat);
88
72
89
73
output += fileSize (
90
74
fileStat,
91
- fileSizeDigits: maxFileSizeLengthInDigits ?? 0 ,
92
- args : args,
75
+ fileSizeDigits: maxFileSizeLengthInDigits,
76
+ humanReadableFileSize : args.humanReadableFileSize ,
93
77
);
94
78
95
79
output += fileOwner (fileStat);
96
80
output += fileModificationDate (fileStat);
97
81
}
98
82
}
99
83
100
- if (args[ ' showFileTypeIcon' ] ! ) {
84
+ if (args. showFileTypeIcon) {
101
85
final String fileToProcess = directory.path + currentFile;
102
86
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
+ );
105
92
}
106
93
output += fileName (element, fileStat, currentFile);
107
94
108
- if (args[ ' longFileListing' ] ! ) {
95
+ if (args. longFileListing) {
109
96
output += "\n " ;
110
97
} else {
111
98
output += " " ;
0 commit comments