Skip to content

Commit 858837f

Browse files
committed
improved prompt example
1 parent 7d915dc commit 858837f

File tree

1 file changed

+1
-129
lines changed

1 file changed

+1
-129
lines changed

examples/prompt/main.go

Lines changed: 1 addition & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,24 @@ package main
22

33
import (
44
"context"
5-
"errors"
65
"flag"
76
"fmt"
87
"io"
9-
"os"
10-
"sync"
11-
"syscall"
128

139
logz "github.com/hedzr/logg/slog"
1410

15-
"github.com/hedzr/is"
1611
"github.com/hedzr/is/exec"
1712
"github.com/hedzr/is/term"
1813
"github.com/hedzr/is/term/color"
19-
20-
origterm "golang.org/x/term"
2114
)
2215

23-
var legacy bool
24-
25-
func init() {
26-
flag.BoolVar(&legacy, "legacy", false, "legacy mode")
27-
}
28-
2916
func main() {
3017
flag.Parse()
3118

3219
ctx := context.Background()
3320

3421
var err error
35-
if legacy {
36-
err = promptMode(ctx)
37-
} else {
38-
err = improvedPromptMode(ctx)
39-
}
22+
err = improvedPromptMode(ctx)
4023
if err != nil {
4124
logz.Error("app error", "err", err)
4225
}
@@ -134,117 +117,6 @@ func runSession(ctx context.Context, a []string, term io.Writer) (err error) {
134117
return
135118
}
136119

137-
func promptMode(ctx context.Context) (err error) {
138-
// if !terminal.IsTerminal(0) || !terminal.IsTerminal(1) {
139-
// return fmt.Errorf("stdin/stdout should be terminal")
140-
// }
141-
142-
var oldState *origterm.State
143-
oldState, err = term.MakeRaw(0)
144-
if err != nil {
145-
if !errors.Is(err, syscall.ENOTTY) {
146-
return err
147-
}
148-
}
149-
defer func() {
150-
if e := recover(); e != nil {
151-
if err == nil {
152-
if e1, ok := e.(error); ok {
153-
err = e1
154-
} else {
155-
err = fmt.Errorf("%v", e)
156-
}
157-
} else {
158-
err = fmt.Errorf("%v | %v", e, err)
159-
}
160-
}
161-
162-
if e1 := term.Restore(0, oldState); e1 != nil {
163-
if err == nil {
164-
err = e1
165-
} else {
166-
err = fmt.Errorf("%v | %v", e1, err)
167-
}
168-
}
169-
}()
170-
screen := struct {
171-
io.Reader
172-
io.Writer
173-
}{os.Stdin, os.Stdout}
174-
term := origterm.NewTerminal(screen, promptString)
175-
term.SetPrompt(string(term.Escape.Red) + promptString + string(term.Escape.Reset))
176-
177-
rePrefix := string(term.Escape.Cyan) + replyPrefix + string(term.Escape.Reset)
178-
179-
exitChan := make(chan struct{}, 3)
180-
defer func() { close(exitChan) }()
181-
182-
catcher := is.Signals().Catch()
183-
catcher.
184-
WithVerboseFn(func(msg string, args ...any) {
185-
// logz.WithSkip(2).Println(fmt.Sprintf("[verbose] %s\n", fmt.Sprintf(msg, args...)))
186-
// // server.Verbose(fmt.Sprintf("[verbose] %s", fmt.Sprintf(msg, args...)))
187-
}).
188-
WithOnSignalCaught(func(ctx context.Context, sig os.Signal, wg *sync.WaitGroup) {
189-
println()
190-
logz.Debug("signal caught", "sig", sig)
191-
// if err := server.Shutdown(); err != nil {
192-
// logger.Error("server shutdown error", "err", err)
193-
// }
194-
// cancel()
195-
exitChan <- struct{}{}
196-
}).
197-
WaitFor(ctx, func(ctx context.Context, closer func()) {
198-
// server.WithOnShutdown(func(err error, ss net.Server) { wgShutdown.Done() })
199-
// err := server.ListenAndServe(ctx, nil)
200-
// if err != nil {
201-
// server.Fatal("server serve failed", "err", err)
202-
// }
203-
204-
defer func() {
205-
_, _ = fmt.Fprintln(term, byeString)
206-
// stopChan <- syscall.SIGINT
207-
// wgShutdown.Done()
208-
closer()
209-
// _, _ = fmt.Println("end")
210-
}()
211-
212-
var line string
213-
for {
214-
line, err = term.ReadLine()
215-
if err == io.EOF {
216-
return
217-
}
218-
if err != nil {
219-
return
220-
}
221-
if line == "" {
222-
continue
223-
}
224-
if line == quitCmd || line == exitCmd || line == "q" {
225-
break
226-
}
227-
_, _ = fmt.Fprintln(term, rePrefix, line)
228-
err = interpretCommand(ctx, line, term)
229-
if err != nil {
230-
return
231-
}
232-
select {
233-
case <-exitChan:
234-
return
235-
default:
236-
}
237-
}
238-
})
239-
return
240-
}
241-
242-
// func interpretCommandOld(line string, term *origterm.Terminal) (err error) {
243-
// a := exec.SplitCommandString(line)
244-
// _, _ = fmt.Fprintln(term, a)
245-
// return
246-
// }
247-
248120
const (
249121
promptString = "(cmdr): "
250122
replyPrefix = "Human says: "

0 commit comments

Comments
 (0)