Skip to content

Commit

Permalink
Support copy to clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
lotusirous committed Oct 29, 2022
1 parent b196755 commit 45710f9
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 23 deletions.
17 changes: 16 additions & 1 deletion file_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import "testing"
import (
"path/filepath"
"testing"
)

func TestByteCountSI(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -31,5 +34,17 @@ func TestByteCountSI(t *testing.T) {
}
})
}
}

func TestReadFileInfo(t *testing.T) {
testFile := "file.go"
abs, _, err := ReadFileInfo(testFile)
if err != nil {
t.Fatalf("cannot read file info: %v", err)
}

want, _ := filepath.Abs(testFile)
if abs != want {
t.Fatalf("invalid abs file, want: %v - got: %v", want, abs)
}
}
26 changes: 18 additions & 8 deletions fp.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
Expand All @@ -11,6 +12,8 @@ import (
"io"
"log"
"os"

"github.com/atotto/clipboard"
)

// DigestGroup hashes a file for a given path
Expand Down Expand Up @@ -49,7 +52,7 @@ func usage() {

var (
flagVerbose = flag.Bool("v", false, `print the file hash in (md5, sha1, sha256)`)
flagClip = flag.Bool("clip", false, "copy the path to the clipboard")
flagClip = flag.Bool("c", false, "copy the path to the clipboard")
)

func main() {
Expand All @@ -68,14 +71,15 @@ func main() {
log.Fatal(err)
}

rr := NewRowRender(os.Stdout)
if fi.IsDir() {
rr.AddRow("DIR", abs)
} else {
rr.AddRow("FILE", abs)
}
rr := NewRowRender()
rr.AddRow("", abs)

if *flagVerbose && !fi.IsDir() {
if fi.IsDir() {
rr.AddRow("DIR", abs)
} else {
rr.AddRow("FILE", abs)
}
rr.AddRow("SIZE", ByteCountSI(fi.Size()))
hg := make(map[string]hash.Hash)
hg["MD5"] = md5.New()
Expand All @@ -87,7 +91,13 @@ func main() {
log.Fatal("unable to digest group: ", err)
}
rr.AddRowMap(values)
rr.Write(os.Stdout)
}

rr.RenderTo(os.Stdout)
if *flagClip {
buf := new(bytes.Buffer)
rr.Write(buf)
clipboard.WriteAll(buf.String())
return
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/lotusirous/fp

go 1.17

require github.com/atotto/clipboard v0.1.4
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
31 changes: 19 additions & 12 deletions printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,26 @@ type RowRenderer interface {
AddRow(field, value string)
// AddRowMap appends the row by map input.
AddRowMap(values map[string]string)
// RenderTo the rows.
RenderTo(w io.Writer)
// Write the rows.
Write(w io.Writer) (n int, err error)
}

// NewRowRender creates the printer.
func NewRowRender(w io.Writer) RowRenderer {
return &pw{w, make([][]string, 0)}
func NewRowRender() RowRenderer {
return &pw{make([]row, 0)}
}

type row struct {
header string
value string
}
type pw struct {
w io.Writer
rows [][]string
rows []row
}

// AddRow adds a row to writer.
func (p *pw) AddRow(field, value string) {
r := []string{field, value}
r := row{header: field, value: value}
p.rows = append(p.rows, r)
}

Expand All @@ -42,15 +45,19 @@ func (p *pw) AddRowMap(values map[string]string) {
sort.Strings(keys)

for _, name := range keys {
r := []string{name, values[name]}
r := row{header: name, value: values[name]}
p.rows = append(p.rows, r)
}

}

// Render the rows to output
func (p *pw) RenderTo(w io.Writer) {
for _, l := range p.rows {
fmt.Fprintf(p.w, "%s: %s\n", strings.ToUpper(l[0]), l[1])
func (p *pw) Write(w io.Writer) (int, error) {
for _, row := range p.rows {
if row.header == "" {
fmt.Fprintf(w, "%s\n", row.value)
} else {
fmt.Fprintf(w, "%s: %s\n", strings.ToUpper(row.header), row.value)
}
}
return 0, nil
}
4 changes: 2 additions & 2 deletions printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (

func TestRowPrinter(t *testing.T) {
buf := new(bytes.Buffer)
pw := &pw{buf, make([][]string, 0)}
pw := &pw{make([]row, 0)}
pw.AddRow("FILE", "bar")
pw.RenderTo(buf)
pw.Write(buf)

got := buf.String()
want := "FILE: bar\n"
Expand Down

0 comments on commit 45710f9

Please sign in to comment.