Initial QSfera import

This commit is contained in:
Курнат Андрей
2026-06-07 10:20:04 +03:00
commit 2315f25754
16485 changed files with 4826827 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
# Stdio
`stdio` provide some standard IO util functions.
## Install
```bash
go get github.com/gookit/goutil/x/stdio
```
## Go docs
- [Go docs](https://pkg.go.dev/github.com/gookit/goutil/x/stdio)
## Usage
Please see tests.
## Testings
```shell
go test -v ./stdio/...
```
Test limit by regexp:
```shell
go test -v -run ^TestSetByKeys ./stdio/...
```
+35
View File
@@ -0,0 +1,35 @@
package stdio
import "io"
// Flusher interface
type Flusher interface {
Flush() error
}
// Syncer interface
type Syncer interface {
Sync() error
}
// FlushWriter is the interface satisfied by logging destinations.
type FlushWriter interface {
Flusher
// Writer the output writer
io.Writer
}
// FlushCloseWriter is the interface satisfied by logging destinations.
type FlushCloseWriter interface {
Flusher
// WriteCloser the output writer
io.WriteCloser
}
// SyncCloseWriter is the interface satisfied by logging destinations.
// such as os.File
type SyncCloseWriter interface {
Syncer
// WriteCloser the output writer
io.WriteCloser
}
+31
View File
@@ -0,0 +1,31 @@
package stdio
import (
"fmt"
"io"
"strings"
)
// Fprint to writer, will ignore error
func Fprint(w io.Writer, a ...any) {
_, _ = fmt.Fprint(w, a...)
}
// Fprintf to writer, will ignore error
func Fprintf(w io.Writer, tpl string, vs ...any) {
_, _ = fmt.Fprintf(w, tpl, vs...)
}
// Fprintln to writer, will ignore error
func Fprintln(w io.Writer, a ...any) {
_, _ = fmt.Fprintln(w, a...)
}
// WriteStringTo a writer, will ignore error
func WriteStringTo(w io.Writer, ss ...string) {
if len(ss) == 1 {
_, _ = io.WriteString(w, ss[0])
} else if len(ss) > 1 {
_, _ = io.WriteString(w, strings.Join(ss, ""))
}
}
+94
View File
@@ -0,0 +1,94 @@
// Package stdio provide some standard IO util functions.
package stdio
import (
"bufio"
"bytes"
"io"
"os"
"strings"
)
// DiscardReader anything from the reader
func DiscardReader(src io.Reader) {
_, _ = io.Copy(io.Discard, src)
}
// ReadString read contents from io.Reader, return empty string on error
func ReadString(r io.Reader) string {
bs, err := io.ReadAll(r)
if err != nil {
return ""
}
return string(bs)
}
// MustReadReader read contents from io.Reader, will panic on error
func MustReadReader(r io.Reader) []byte {
bs, err := io.ReadAll(r)
if err != nil {
panic(err)
}
return bs
}
// NewIOReader instance by input: string, bytes, io.Reader
func NewIOReader(in any) io.Reader {
switch typIn := in.(type) {
case []byte:
return bytes.NewReader(typIn)
case string:
return strings.NewReader(typIn)
case io.Reader:
return typIn
}
panic("invalid input type for create reader")
}
// NewScanner instance by input data or reader
func NewScanner(in any) *bufio.Scanner {
switch typIn := in.(type) {
case io.Reader:
return bufio.NewScanner(typIn)
case []byte:
return bufio.NewScanner(bytes.NewReader(typIn))
case string:
return bufio.NewScanner(strings.NewReader(typIn))
case *bufio.Scanner:
return typIn
default:
panic("invalid input type for create scanner")
}
}
// SafeClose close io.Closer, ignore error
func SafeClose(c io.Closer) {
_ = c.Close()
}
// WriteByte to stdout, will ignore error
func WriteByte(b byte) {
_, _ = os.Stdout.Write([]byte{b})
}
// WriteBytes to stdout, will ignore error
func WriteBytes(bs []byte) {
_, _ = os.Stdout.Write(bs)
}
// WritelnBytes to stdout, will ignore error
func WritelnBytes(bs []byte) {
_, _ = os.Stdout.Write(bs)
_, _ = os.Stdout.Write([]byte("\n"))
}
// WriteString to stdout. will ignore error
func WriteString(s string) {
_, _ = os.Stdout.WriteString(s)
}
// Writeln string to stdout. will ignore error
func Writeln(s string) {
_, _ = os.Stdout.WriteString(s)
_, _ = os.Stdout.Write([]byte("\n"))
}
+53
View File
@@ -0,0 +1,53 @@
package stdio
import (
"fmt"
"io"
)
// WriteWrapper warp io.Writer support more operate methods.
type WriteWrapper struct {
Out io.Writer
}
// WrapW instance
func WrapW(w io.Writer) *WriteWrapper {
return &WriteWrapper{Out: w}
}
// NewWriteWrapper instance
func NewWriteWrapper(w io.Writer) *WriteWrapper {
return &WriteWrapper{Out: w}
}
// Write bytes data
func (w *WriteWrapper) Write(p []byte) (n int, err error) {
return w.Out.Write(p)
}
// Writef data to output
func (w *WriteWrapper) Writef(tpl string, vs ...any) (n int, err error) {
return fmt.Fprintf(w.Out, tpl, vs...)
}
// WriteByte data
func (w *WriteWrapper) WriteByte(c byte) error {
_, err := w.Out.Write([]byte{c})
return err
}
// WriteString data
func (w *WriteWrapper) WriteString(s string) (n int, err error) {
if sw, ok := w.Out.(io.StringWriter); ok {
return sw.WriteString(s)
}
return w.Out.Write([]byte(s))
}
// String get write data string
func (w *WriteWrapper) String() string {
if sw, ok := w.Out.(fmt.Stringer); ok {
return sw.String()
}
return ""
}