Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ func main() {
}
}

// ==== ADDED BLOCK: check if stdin is a terminal and bail if yes ====
fi, err := os.Stdin.Stat()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to stat stdin: %v\n", err)
os.Exit(1)
}

if (fi.Mode() & os.ModeCharDevice) != 0 {
fmt.Fprintln(os.Stderr, "anew: expects input via pipe or redirection. Example:")
fmt.Fprintln(os.Stderr, " cat newthings.txt | anew things.txt")
os.Exit(1)
}
// ==== END ADDED BLOCK ====

// read the lines, append and output them if they're new
sc := bufio.NewScanner(os.Stdin)

Expand All @@ -76,3 +90,4 @@ func main() {
}
}
}