-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
55 lines (52 loc) · 1.57 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Command ed implements ed, the standard unix text editor.
// Originally created by Ken Thompson in the 1970s.
//
// Usage:
//
// ed [-] [-s] [-p string] [file]
//
// ed is a line-oriented text editor that operates on a file one line at a time.
// It's designed for editing small to medium-sized files, and its simplicity makes
// it a great choice for quick edits or when you need to edit files in a script.
//
// Basic commands:
//
// s/old/new/g : Substitutes all occurences of old with new on the current line
// a : Append lines at [dot] until the entered line only consists of a "."
// c : Change lines selected by the range until the entered line only consists of a "."
// d : Delete the current line
// p : Print the current line
// ,p : Prints the entire buffer
// ,n : Prints the entire buffer but with line numbers
// q : Quit ed
//
// For more information, refer to the OpenBSD man page: https://man.openbsd.org/ed.1
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
)
var (
Prompt = flag.String("p", "", "user prompt")
Silent = flag.Bool("s", false, "suppress diagnostics")
)
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [-] [-s] [-p string] [file]\n", filepath.Base(os.Args[0]))
os.Exit(1)
}
flag.Parse()
opts := []Option{WithStdin(os.Stdin), WithPrompt(*Prompt)}
if flag.NArg() > 0 {
arg := flag.Args()[0]
if arg == "-" {
*Silent = true
} else {
opts = append(opts, WithFile(arg))
}
}
opts = append(opts, WithSilent(*Silent))
NewEditor(opts...).Run()
}