Skip to content

Commit

Permalink
🎨 chore: resolve some code style error and update some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 28, 2023
1 parent cac36d3 commit 868b402
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 20 deletions.
2 changes: 1 addition & 1 deletion envutil/envutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestSetEnvs(t *testing.T) {
assert.Eq(t, val, Getenv(key))
}
assert.Panics(t, func() {
SetEnvs("name")
SetEnvs("name", "one", "two")
})

UnsetEnvs(keys...)
Expand Down
10 changes: 5 additions & 5 deletions stdio/stdio.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,28 @@ func NewScanner(in any) *bufio.Scanner {
}
}

// WriteByte to stdout
// WriteByte to stdout, will ignore error
func WriteByte(b byte) {
_, _ = os.Stdout.Write([]byte{b})
}

// WriteBytes to stdout
// WriteBytes to stdout, will ignore error
func WriteBytes(bs []byte) {
_, _ = os.Stdout.Write(bs)
}

// WritelnBytes to stdout
// WritelnBytes to stdout, will ignore error
func WritelnBytes(bs []byte) {
_, _ = os.Stdout.Write(bs)
_, _ = os.Stdout.Write([]byte("\n"))
}

// WriteString to stdout
// WriteString to stdout, will ignore error
func WriteString(s string) {
_, _ = os.Stdout.WriteString(s)
}

// Writeln string to stdout
// Writeln string to stdout, will ignore error
func Writeln(s string) {
_, _ = os.Stdout.WriteString(s)
_, _ = os.Stdout.Write([]byte("\n"))
Expand Down
4 changes: 2 additions & 2 deletions structs/smap.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package structs

// SMap simple map[string]string struct.
// SMap simple map[string]string struct. TODO
type SMap struct {
data map[string]string
data map[string]string //lint:ignore U1000 for unused
}
4 changes: 2 additions & 2 deletions structs/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type Wrapper struct {
FieldTagName string

// caches for field rv and name and tag name TODO
fieldNames []string
fvCacheMap map[string]reflect.Value
fieldNames []string //lint:ignore U1000 for unused
fvCacheMap map[string]reflect.Value //lint:ignore U1000 for unused
}

// Wrap create a struct wrapper
Expand Down
19 changes: 13 additions & 6 deletions sysutil/clipboard/clipboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ func (c *Clipboard) Reset() error {
// ---------------------------------------- write ----------------------------------------
//

// Write bytes data to clipboard
// Write bytes data to buffer. should call Flush() to write to clipboard
func (c *Clipboard) Write(p []byte) (int, error) {
return c.WriteString(string(p))
}

// WriteString data to clipboard
// WriteString data to buffer. should call Flush() to write to clipboard
func (c *Clipboard) WriteString(s string) (int, error) {
// if c.addSlashes {
// s = strutil.AddSlashes(s)
Expand All @@ -104,8 +104,6 @@ func (c *Clipboard) Flush() error {

// WriteFromFile contents to clipboard
func (c *Clipboard) WriteFromFile(filepath string) error {
// eg:
// Mac: pbcopy < tempfile.txt
file, err := fsutil.OpenReadFile(filepath)
if err != nil {
return err
Expand Down Expand Up @@ -134,7 +132,7 @@ func (c *Clipboard) WriteFrom(r io.Reader) error {
// ---------------------------------------- read ----------------------------------------
//

// Read contents from clipboard
// Read bytes contents from clipboard
func (c *Clipboard) Read() ([]byte, error) {
buf, err := c.ReadToBuffer()
if err != nil {
Expand All @@ -143,7 +141,7 @@ func (c *Clipboard) Read() ([]byte, error) {
return buf.Bytes(), nil
}

// ReadToBuffer contents from clipboard
// ReadToBuffer read clipboard contents to new buffer.
func (c *Clipboard) ReadToBuffer() (*bytes.Buffer, error) {
var buf bytes.Buffer
if err := c.ReadTo(&buf); err != nil {
Expand All @@ -152,6 +150,15 @@ func (c *Clipboard) ReadToBuffer() (*bytes.Buffer, error) {
return &buf, nil
}

// SafeString read contents as string from clipboard, will return empty string on error
func (c *Clipboard) SafeString() string {
s, err := c.ReadString()
if err != nil {
return ""
}
return s
}

// ReadString contents as string from clipboard
func (c *Clipboard) ReadString() (string, error) {
bts, err := c.Read()
Expand Down
13 changes: 13 additions & 0 deletions sysutil/clipboard/clipboard_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package clipboard_test

import (
"fmt"
"testing"

"github.com/gookit/goutil/fsutil"
Expand All @@ -17,6 +18,10 @@ func TestClipboard_WriteFromFile(t *testing.T) {
return
}

cb.WithVerbose(true)
assert.True(t, cb.Writeable())
assert.True(t, cb.Readable())

srcFile := "testdata/testcb.txt"
srcStr := string(fsutil.MustReadFile(srcFile))
assert.NotEmpty(t, srcStr)
Expand All @@ -38,4 +43,12 @@ func TestClipboard_WriteFromFile(t *testing.T) {

dstStr := string(fsutil.MustReadFile(dstFile))
assert.Eq(t, srcStr, strutil.Trim(dstStr))

assert.NoErr(t, cb.Clean())

_, err = cb.Write([]byte("hello"))
assert.NoErr(t, err)
assert.NoErr(t, cb.Flush())
assert.Eq(t, "hello", cb.SafeString())
fmt.Println("...end...")
}
9 changes: 5 additions & 4 deletions sysutil/clipboard/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ const (
)

var (
writerOnLin = []string{"xclip", "xsel"}
)
// TODO select an valid driver on New()
writerOnLin = []string{"xclip", "xsel"} //lint:ignore U1000 for TODO

// std instance
var std = New()
// std instance
std = New()
)

// Std get
func Std() *Clipboard {
Expand Down

0 comments on commit 868b402

Please sign in to comment.