Skip to content

Commit 3c40e12

Browse files
committed
add --block-size option
1 parent 361da8d commit 3c40e12

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,16 @@ examples:
3737
$ echo 1637421447 | tf
3838
$ tf -g log.txt | head
3939
40-
-b, --block use block buffering (default: line buffering)
41-
-d, --date default format with '2006-01-02 15:04:05'
42-
-f, --format string golang Time.Format string (default: '15:04:05')
43-
-g, --global global match
44-
-h, --help show help```
40+
The time formatting uses Golang Time.Format layouts:
41+
https://pkg.go.dev/time#Time.Format
42+
43+
options:
44+
-b, --block use block buffering (default: line buffering)
45+
-z, --block-size uint32 block buffer size (default 4096)
46+
-d, --date default format with date: '2006-01-02 15:04:05'
47+
-f, --format string golang Time.Format string (default: '15:04:05')
48+
-g, --global global match
49+
-h, --help show help
4550
```
4651

4752
----

cmd/tf/main.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,23 @@ examples:
2828
$ echo 1637421447 | tf
2929
$ tf -g log.txt | head
3030
31+
The time formatting uses Golang Time.Format layouts:
32+
https://pkg.go.dev/time#Time.Format
33+
34+
options:
3135
`
3236

3337
////////////////////////////////////////////////////////////////////////////////////
3438
// Globals
3539

40+
const DEFAULT_FORMAT = "15:04:05"
41+
const DEFAULT_FORMAT_WITH_DATE = "2006-01-02 15:04:05"
42+
43+
const DEFAULT_BLOCK_BUFFER_SIZE = 4096
44+
3645
var g_outputFormat string = ""
3746
var g_blockBuffering bool = false
47+
var g_blockBufferSize uint32 = DEFAULT_BLOCK_BUFFER_SIZE
3848
var g_globalMatch bool = false
3949

4050
////////////////////////////////////////////////////////////////////////////////////
@@ -43,7 +53,7 @@ func processReader(reader io.Reader) error {
4353
if g_blockBuffering {
4454
// block buffering with bufio Reader
4555
reader := bufio.NewReader(reader)
46-
buf := make([]byte, BLOCK_BUFFER_SIZE)
56+
buf := make([]byte, g_blockBufferSize)
4757
for {
4858
n, err := reader.Read(buf)
4959
buf = buf[:n]
@@ -63,7 +73,6 @@ func processReader(reader io.Reader) error {
6373
// line buffering with bufio.Scanner
6474
scanner := bufio.NewScanner(reader)
6575
for scanner.Scan() {
66-
// convert time
6776
str, _ := tf.ConvertTimes(scanner.Text(), g_outputFormat, g_globalMatch)
6877
fmt.Println(str)
6978
}
@@ -78,19 +87,15 @@ func processReader(reader io.Reader) error {
7887
/////////////////////////////////////////////////////////////////////////////////////
7988
// Main Program
8089

81-
const DEFAULT_FORMAT = "15:04:05"
82-
const DEFAULT_FORMAT_WITH_DATE = "2006-01-02 15:04:05"
83-
84-
const BLOCK_BUFFER_SIZE = 4096
85-
8690
func main() {
8791
var useDate bool
8892
var showHelp bool
8993

90-
pflag.StringVarP(&g_outputFormat, "format", "f", "", "golang Time.Format string (default: '15:04:05')")
94+
pflag.StringVarP(&g_outputFormat, "format", "f", "", fmt.Sprintf("golang Time.Format string (default: '%s')", DEFAULT_FORMAT))
9195
pflag.BoolVarP(&g_globalMatch, "global", "g", false, "global match")
9296
pflag.BoolVarP(&g_blockBuffering, "block", "b", false, "use block buffering (default: line buffering)")
93-
pflag.BoolVarP(&useDate, "date", "d", false, "default format with '2006-01-02 15:04:05'")
97+
pflag.Uint32VarP(&g_blockBufferSize, "block-size", "z", DEFAULT_BLOCK_BUFFER_SIZE, "block buffer size")
98+
pflag.BoolVarP(&useDate, "date", "d", false, fmt.Sprintf("default format with date: '%s'", DEFAULT_FORMAT_WITH_DATE))
9499
pflag.BoolVarP(&showHelp, "help", "h", false, "show help")
95100
pflag.Parse()
96101

0 commit comments

Comments
 (0)