From 5db1d3e22c3f402b04add534d11d6ab3ef02a938 Mon Sep 17 00:00:00 2001 From: Chris Vilches Date: Fri, 6 Dec 2024 21:15:28 +0900 Subject: [PATCH] version up, signal CTRL-C to abort, read very long lines --- .metadata/version | 2 +- cli/command.go | 58 +++++++++++++++++++++++++++++++++++------------ cmp/process.go | 5 +++- 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/.metadata/version b/.metadata/version index 3d45b5c..b8061b5 100644 --- a/.metadata/version +++ b/.metadata/version @@ -1 +1 @@ -2.0.14 +2.0.15 diff --git a/cli/command.go b/cli/command.go index 12a3575..5c45c0e 100644 --- a/cli/command.go +++ b/cli/command.go @@ -4,10 +4,13 @@ import ( "bufio" "errors" "fmt" - "github.com/ChrisVilches/cpdiff/cmp" "os" + "os/signal" "strings" + "sync/atomic" "time" + + "github.com/ChrisVilches/cpdiff/cmp" ) const ( @@ -116,12 +119,30 @@ func showComparisonEntry( return true, nil } -func readLinesToChannel(buf *bufio.Scanner, opts options) <-chan string { +func readLinesToChannel(buf *bufio.Reader, opts options) <-chan string { ch := make(chan string, chSize) + // TODO: I have to improve the error validation here. + // In the previous version I had something like this: + // (since I changed to bufio.NewReader, this error handling + // had to be removed) + // + // lhs := bufio.NewScanner(files[0]) + // rhs := bufio.NewScanner(files[1]) + // ... + // if err := lhs.Err(); err != nil { + // return err + // } + // if err := rhs.Err(); err != nil { + // return err + // } + go func() { - for buf.Scan() { - line := buf.Text() + for { + line, err := buf.ReadString('\n') + if err != nil { + break + } if opts.trim { line = strings.TrimSpace(line) @@ -204,6 +225,20 @@ func listenEntries( return &res, nil } +func listenSignal() *atomic.Bool { + res := atomic.Bool{} + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + go func() { + for s := range c { + if s.String() == "interrupt" { + res.Store(true) + } + } + }() + return &res +} + func mainCommand(opts options, args []string) error { startTime := time.Now() @@ -216,8 +251,10 @@ func mainCommand(opts options, args []string) error { defer files[0].Close() defer files[1].Close() - lhs := bufio.NewScanner(files[0]) - rhs := bufio.NewScanner(files[1]) + lhs := bufio.NewReader(files[0]) + rhs := bufio.NewReader(files[1]) + + signalAborted := listenSignal() lhsCh := readLinesToChannel(lhs, opts) rhsCh := readLinesToChannel(rhs, opts) @@ -229,6 +266,7 @@ func mainCommand(opts options, args []string) error { opts.useRelativeError, opts.numbers, chSize, + signalAborted, ) fullResult, err := listenEntries(entries, opts) @@ -237,14 +275,6 @@ func mainCommand(opts options, args []string) error { return err } - if err := lhs.Err(); err != nil { - return err - } - - if err := rhs.Err(); err != nil { - return err - } - if fullResult.printedLines > 0 { fmt.Println() } diff --git a/cmp/process.go b/cmp/process.go index 98931f3..8f1a236 100644 --- a/cmp/process.go +++ b/cmp/process.go @@ -1,6 +1,8 @@ package cmp import ( + "sync/atomic" + "github.com/ChrisVilches/cpdiff/big" ) @@ -81,6 +83,7 @@ func Process( useRelativeErr bool, useNumbers bool, chSize int, + aborted *atomic.Bool, ) <-chan ComparisonEntry { entries := make(chan ComparisonEntry, chSize) @@ -89,7 +92,7 @@ func Process( line1, ok1 := <-lhsCh line2, ok2 := <-rhsCh - if !ok1 && !ok2 { + if (!ok1 && !ok2) || aborted.Load() { break }