From 6b80d53da80d0e9ea49db73a575ce75acca0334c Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Fri, 11 Jan 2019 13:10:11 +0100 Subject: [PATCH] feat: support reading sequences from stdin --- main.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index b7a40e1..81d6fca 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,9 @@ package main // import "moul.io/patfind" import ( + "bufio" "fmt" + "log" "math" "os" "strings" @@ -14,8 +16,25 @@ import ( func main() { data := [][]string{} - for _, arg := range os.Args[1:] { - sequence := score.New(arg) + sequences := []score.Sequence{} + + if len(os.Args) < 2 { // stdin + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + if err := scanner.Err(); err != nil { + log.Printf("error: failed to read line from stdin: %v", err) + break + } else { + sequences = append(sequences, score.New(scanner.Text())) + } + } + } else { + for _, arg := range os.Args[1:] { + sequences = append(sequences, score.New(arg)) + } + } + + for _, sequence := range sequences { score, comments := sequence.Score() shift := math.Pow(10, float64(5)) roundedScore := math.Floor(score*shift+.5) / shift @@ -26,7 +45,7 @@ func main() { table.SetHeader([]string{"Sequence", "Score", "Comments"}) table.SetBorder(true) table.SetRowLine(true) - table.SetColWidth(50) + table.SetColWidth(100) table.AppendBulk(data) table.Render() }