sort-cli: magic constants
This commit is contained in:
@ -2,6 +2,8 @@ package main
|
|||||||
|
|
||||||
import "flag"
|
import "flag"
|
||||||
|
|
||||||
|
const stdin = "-"
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Key int
|
Key int
|
||||||
Numeric bool
|
Numeric bool
|
||||||
@ -21,6 +23,6 @@ func (c *Config) ParseFlags() {
|
|||||||
c.Sources = flag.Args()
|
c.Sources = flag.Args()
|
||||||
|
|
||||||
if len(c.Sources) == 0 {
|
if len(c.Sources) == 0 {
|
||||||
c.Sources = []string{"-"}
|
c.Sources = []string{stdin}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// NL New line constant
|
||||||
|
const NL = "\n"
|
||||||
|
|
||||||
type content [][]byte
|
type content [][]byte
|
||||||
|
|
||||||
func (c *content) Sort(reverse bool) {
|
func (c *content) Sort(reverse bool) {
|
||||||
@ -42,14 +45,14 @@ func (c *content) String() string {
|
|||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
n += len(line)
|
n += len(line)
|
||||||
}
|
}
|
||||||
n += len(lines) * len("\n")
|
n += len(lines) * len(NL)
|
||||||
|
|
||||||
var sb strings.Builder
|
var sb strings.Builder
|
||||||
sb.Grow(n)
|
sb.Grow(n)
|
||||||
|
|
||||||
for i, line := range lines {
|
for i, line := range lines {
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
sb.WriteString("\n")
|
sb.WriteString(NL)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, rn := range line {
|
for _, rn := range line {
|
||||||
|
@ -12,7 +12,7 @@ func open(sources []string) io.Reader {
|
|||||||
for _, source := range sources {
|
for _, source := range sources {
|
||||||
var r io.Reader
|
var r io.Reader
|
||||||
|
|
||||||
if source == "-" {
|
if source == stdin {
|
||||||
r = os.Stdin
|
r = os.Stdin
|
||||||
} else {
|
} else {
|
||||||
if _, err := os.Stat(source); err != nil {
|
if _, err := os.Stat(source); err != nil {
|
||||||
|
Reference in New Issue
Block a user