54 lines
791 B
Go
54 lines
791 B
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
type Item struct {
|
|
Text string
|
|
Selected bool
|
|
}
|
|
|
|
type Items = *[]*Item
|
|
|
|
func NewItem(text string) *Item {
|
|
return &Item{
|
|
Text: text,
|
|
Selected: false,
|
|
}
|
|
}
|
|
|
|
func min(a, b int) int {
|
|
if a < b {
|
|
return a
|
|
}
|
|
return b
|
|
}
|
|
|
|
func ReadFromStdin() (Items, error) {
|
|
info, err := os.Stdin.Stat()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if info.Mode()&os.ModeCharDevice != 0 {
|
|
return nil, fmt.Errorf("The command is intended to work with pipes. For example `git log | filter | xarg | ...`")
|
|
}
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
var output []*Item
|
|
|
|
for {
|
|
input, _, err := reader.ReadLine()
|
|
if err != nil && err == io.EOF {
|
|
break
|
|
}
|
|
item := NewItem(string(input))
|
|
output = append(output, item)
|
|
}
|
|
return &output, nil
|
|
}
|