Giter VIP home page Giter VIP logo

cancelreader's Introduction

CancelReader

Latest Release Go Doc Software License Build Status Go ReportCard

A cancelable reader for Go

This package is based on the fantastic work of Erik Geiser in Charm's Bubble Tea framework.

Usage

NewReader returns a reader with a Cancel function. If the input reader is a File, the cancel function can be used to interrupt a blocking Read call. In this case, the cancel function returns true if the call was canceled successfully. If the input reader is not a File, the cancel function does nothing and always returns false.

r, err := cancelreader.NewReader(file)
if err != nil {
    // handle error
    ...
}

// cancel after five seconds
go func() {
    time.Sleep(5 * time.Second)
    r.Cancel()
}()

// keep reading
for {
    var buf [1024]byte
    _, err := r.Read(buf[:])

    if errors.Is(err, cancelreader.ErrCanceled) {
        fmt.Println("canceled!")
        break
    }
    if err != nil {
        // handle other errors
        ...
    }

    // handle data
    ...
}

Implementations

  • The Linux implementation is based on the epoll mechanism
  • The BSD and macOS implementation is based on the kqueue mechanism
  • The generic Unix implementation is based on the posix select syscall

Caution

The Windows implementation is based on WaitForMultipleObject with overlapping reads from CONIN$. At this point it only supports canceling reads from os.Stdin.

cancelreader's People

Contributors

caarlos0 avatar dependabot[bot] avatar hinshun avatar muesli avatar tklauser avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cancelreader's Issues

`CTRL+C` on windows is trapped by this library

According to the Windows documentation:

The SetConsoleMode function can disable the ENABLE_PROCESSED_INPUT input mode for a console's input buffer, so CTRL+C is reported as keyboard input rather than as a signal.

In the windows implementation, we do:

var newMode uint32
newMode &^= windows.ENABLE_ECHO_INPUT
newMode &^= windows.ENABLE_LINE_INPUT
newMode &^= windows.ENABLE_MOUSE_INPUT
newMode &^= windows.ENABLE_WINDOW_INPUT

newMode &^= windows.ENABLE_PROCESSED_INPUT // This reports CTRL+C as an input rather than as a signal.

newMode |= windows.ENABLE_EXTENDED_FLAGS
newMode |= windows.ENABLE_INSERT_MODE
newMode |= windows.ENABLE_QUICK_EDIT_MODE

I believe it should be changed to newMode |= windows.ENABLE_PROCESSED_INPUT so we keep the signal propagation.

cancelreader not support chinese on windows?

hello, i found this problem when using bubbletea.

test code:

func main() {
	test(os.Stdin)

	reader, _ := cancelreader.NewReader(os.Stdin)
	test(reader)
}

func test(reader io.Reader) {
	var buf [256]byte
	n, err := reader.Read(buf[:])
	if err != nil {
		fmt.Println(err)
	}

	r, _ := utf8.DecodeRune(buf[:n])
	fmt.Println("decode error ? ", r == utf8.RuneError)
	fmt.Printf("%s \n", string(r))
	fmt.Println(buf[:n])

	fmt.Println("=============")
}

input:
output:

$ go run .
好
decode error ?  false
好
[229 165 189 13 10]
=============
decode error ?  true
�
[186 195]
=============

go version go1.19 windows/amd64 faild
go version go1.18 windows/amd64 falid

but it was successful on go version go1.18.3 linux/amd64

Thank you!

This package was exactly what I needed to solve a tricky issue in my code.

Redirected stdin not handled properly on Windows

On Windows, NewReader(os.Stdin) will replace stdin with a handle to the console, even when stdin was previously redirected from a pipe.

This can be fixed by returning newFallbackCancelReader(reader) when stdin is not a console.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.