@@ -14,28 +14,31 @@ import (
14
14
"os"
15
15
"runtime"
16
16
"sort"
17
+ "strconv"
18
+ "strings"
17
19
"sync"
18
20
"time"
19
21
20
- "pkg.re/essentialkaos/ek.v11/fmtc"
21
- "pkg.re/essentialkaos/ek.v11/fmtutil"
22
- "pkg.re/essentialkaos/ek.v11/fsutil"
23
- "pkg.re/essentialkaos/ek.v11/options"
24
- "pkg.re/essentialkaos/ek.v11/signal"
25
- "pkg.re/essentialkaos/ek.v11/strutil"
26
- "pkg.re/essentialkaos/ek.v11/usage"
27
- "pkg.re/essentialkaos/ek.v11/usage/completion/bash"
28
- "pkg.re/essentialkaos/ek.v11/usage/completion/fish"
29
- "pkg.re/essentialkaos/ek.v11/usage/completion/zsh"
30
- "pkg.re/essentialkaos/ek.v11/usage/update"
22
+ "pkg.re/essentialkaos/ek.v12/fmtc"
23
+ "pkg.re/essentialkaos/ek.v12/fmtutil"
24
+ "pkg.re/essentialkaos/ek.v12/fsutil"
25
+ "pkg.re/essentialkaos/ek.v12/options"
26
+ "pkg.re/essentialkaos/ek.v12/signal"
27
+ "pkg.re/essentialkaos/ek.v12/strutil"
28
+ "pkg.re/essentialkaos/ek.v12/usage"
29
+ "pkg.re/essentialkaos/ek.v12/usage/completion/bash"
30
+ "pkg.re/essentialkaos/ek.v12/usage/completion/fish"
31
+ "pkg.re/essentialkaos/ek.v12/usage/completion/zsh"
32
+ "pkg.re/essentialkaos/ek.v12/usage/man"
33
+ "pkg.re/essentialkaos/ek.v12/usage/update"
31
34
)
32
35
33
36
// ////////////////////////////////////////////////////////////////////////////////// //
34
37
35
38
// Application basic info
36
39
const (
37
40
APP = "uc"
38
- VER = "0 .0.2 "
41
+ VER = "1 .0.0 "
39
42
DESC = "Tool for counting unique lines"
40
43
)
41
44
@@ -48,7 +51,8 @@ const (
48
51
OPT_HELP = "h:help"
49
52
OPT_VER = "v:version"
50
53
51
- OPT_COMPLETION = "completion"
54
+ OPT_COMPLETION = "completion"
55
+ OPT_GENERATE_MAN = "generate-man"
52
56
)
53
57
54
58
// ////////////////////////////////////////////////////////////////////////////////// //
@@ -92,14 +96,15 @@ func (s linesSlice) Less(i, j int) bool {
92
96
93
97
// optMap is map with options
94
98
var optMap = options.Map {
95
- OPT_MAX_LINES : {Type : options .INT },
99
+ OPT_MAX_LINES : {Type : options .MIXED },
96
100
OPT_DISTRIBUTION : {Type : options .BOOL },
97
101
OPT_NO_PROGRESS : {Type : options .BOOL },
98
102
OPT_NO_COLOR : {Type : options .BOOL },
99
103
OPT_HELP : {Type : options .BOOL , Alias : "u:usage" },
100
104
OPT_VER : {Type : options .BOOL , Alias : "ver" },
101
105
102
- OPT_COMPLETION : {},
106
+ OPT_COMPLETION : {},
107
+ OPT_GENERATE_MAN : {Type : options .BOOL },
103
108
}
104
109
105
110
// stats contains info about data
@@ -127,7 +132,12 @@ func main() {
127
132
}
128
133
129
134
if options .Has (OPT_COMPLETION ) {
130
- genCompletion ()
135
+ os .Exit (genCompletion ())
136
+ }
137
+
138
+ if options .Has (OPT_GENERATE_MAN ) {
139
+ genMan ()
140
+ os .Exit (0 )
131
141
}
132
142
133
143
configureUI ()
@@ -195,7 +205,12 @@ func processData(input string) {
195
205
func readData (s * bufio.Scanner ) {
196
206
ct := crc64 .MakeTable (crc64 .ECMA )
197
207
dist := options .GetB (OPT_DISTRIBUTION )
198
- maxLines := options .GetI (OPT_MAX_LINES )
208
+ maxLines , err := parseMaxLines (options .GetS (OPT_MAX_LINES ))
209
+
210
+ if err != nil {
211
+ printError (err .Error ())
212
+ os .Exit (1 )
213
+ }
199
214
200
215
if dist {
201
216
stats .Samples = make (map [uint64 ]string )
@@ -299,6 +314,30 @@ func printDistribution() {
299
314
}
300
315
}
301
316
317
+ // parseMaxLines parses max line option
318
+ func parseMaxLines (maxLines string ) (int , error ) {
319
+ maxLines = strings .ToUpper (maxLines )
320
+
321
+ mp := 1
322
+
323
+ switch {
324
+ case strings .HasSuffix (maxLines , "K" ):
325
+ maxLines = strutil .Exclude (maxLines , "K" )
326
+ mp = 1000
327
+ case strings .HasSuffix (maxLines , "M" ):
328
+ mp = 1000 * 1000
329
+ maxLines = strutil .Exclude (maxLines , "M" )
330
+ }
331
+
332
+ num , err := strconv .Atoi (maxLines )
333
+
334
+ if err != nil {
335
+ return 0 , err
336
+ }
337
+
338
+ return num * mp , nil
339
+ }
340
+
302
341
// signalHandler is signal handler
303
342
func signalHandler () {
304
343
printResults ()
@@ -312,11 +351,42 @@ func printError(f string, a ...interface{}) {
312
351
313
352
// ////////////////////////////////////////////////////////////////////////////////// //
314
353
315
- // showUsage print usage info
354
+ // showUsage prints usage info
316
355
func showUsage () {
317
356
genUsage ().Render ()
318
357
}
319
358
359
+ // showAbout prints info about version
360
+ func showAbout () {
361
+ genAbout ().Render ()
362
+ }
363
+
364
+ // genCompletion generates completion for different shells
365
+ func genCompletion () int {
366
+ switch options .GetS (OPT_COMPLETION ) {
367
+ case "bash" :
368
+ fmt .Printf (bash .Generate (genUsage (), APP ))
369
+ case "fish" :
370
+ fmt .Printf (fish .Generate (genUsage (), APP ))
371
+ case "zsh" :
372
+ fmt .Printf (zsh .Generate (genUsage (), optMap , APP ))
373
+ default :
374
+ return 1
375
+ }
376
+
377
+ return 0
378
+ }
379
+
380
+ // genMan generates man page
381
+ func genMan () {
382
+ fmt .Println (
383
+ man .Generate (
384
+ genUsage (),
385
+ genAbout (),
386
+ ),
387
+ )
388
+ }
389
+
320
390
// genUsage generates usage info
321
391
func genUsage () * usage.Info {
322
392
info := usage .NewInfo (APP , "file" )
@@ -331,6 +401,7 @@ func genUsage() *usage.Info {
331
401
332
402
info .AddExample ("file.txt" , "Count unique lines in file.txt" )
333
403
info .AddExample ("-d file.txt" , "Show distribution for file.txt" )
404
+ info .AddExample ("-d -m 5k file.txt" , "Show distribution for file.txt for 5,000 uniq lines max" )
334
405
info .AddRawExample (
335
406
"cat file.txt | " + APP + " -" ,
336
407
"Count unique lines in stdin data" ,
@@ -339,33 +410,16 @@ func genUsage() *usage.Info {
339
410
return info
340
411
}
341
412
342
- // genCompletion generates completion for different shells
343
- func genCompletion () {
344
- switch options .GetS (OPT_COMPLETION ) {
345
- case "bash" :
346
- fmt .Printf (bash .Generate (genUsage (), APP ))
347
- case "fish" :
348
- fmt .Printf (fish .Generate (genUsage (), APP ))
349
- case "zsh" :
350
- fmt .Printf (zsh .Generate (genUsage (), optMap , APP ))
351
- default :
352
- os .Exit (1 )
353
- }
354
-
355
- os .Exit (0 )
356
- }
357
-
358
- // showAbout print info about version
359
- func showAbout () {
360
- about := & usage.About {
413
+ // genAbout generates info about version
414
+ func genAbout () * usage.About {
415
+ return & usage.About {
361
416
App : APP ,
362
417
Version : VER ,
363
418
Desc : DESC ,
364
419
Year : 2009 ,
365
- Owner : "Essential Kaos " ,
420
+ Owner : "ESSENTIAL KAOS " ,
366
421
License : "Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>" ,
422
+ BugTracker : "https://github.com/essentialkaos/uc" ,
367
423
UpdateChecker : usage.UpdateChecker {"essentialkaos/uc" , update .GitHubChecker },
368
424
}
369
-
370
- about .Render ()
371
425
}
0 commit comments