Skip to content

Commit

Permalink
version up, signal CTRL-C to abort, read very long lines
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisVilches committed Dec 6, 2024
1 parent 057605a commit 5db1d3e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .metadata/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.14
2.0.15
58 changes: 44 additions & 14 deletions cli/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand All @@ -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)
Expand All @@ -229,6 +266,7 @@ func mainCommand(opts options, args []string) error {
opts.useRelativeError,
opts.numbers,
chSize,
signalAborted,
)

fullResult, err := listenEntries(entries, opts)
Expand All @@ -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()
}
Expand Down
5 changes: 4 additions & 1 deletion cmp/process.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmp

import (
"sync/atomic"

"github.com/ChrisVilches/cpdiff/big"
)

Expand Down Expand Up @@ -81,6 +83,7 @@ func Process(
useRelativeErr bool,
useNumbers bool,
chSize int,
aborted *atomic.Bool,
) <-chan ComparisonEntry {
entries := make(chan ComparisonEntry, chSize)

Expand All @@ -89,7 +92,7 @@ func Process(
line1, ok1 := <-lhsCh
line2, ok2 := <-rhsCh

if !ok1 && !ok2 {
if (!ok1 && !ok2) || aborted.Load() {
break
}

Expand Down

0 comments on commit 5db1d3e

Please sign in to comment.