-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
100 lines (78 loc) · 1.59 KB
/
input.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2020 Blues Inc. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
package main
import (
"bufio"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
)
func inputHandler() {
// Spawn our signal handler
go signalHandler()
// Create a scanner to watch stdin
scanner := bufio.NewScanner(os.Stdin)
var message string
for {
scanner.Scan()
message = scanner.Text()
args := strings.Split(message, " ")
argsLC := strings.Split(strings.ToLower(message), " ")
arg0 := ""
arg0LC := ""
if len(args) > 0 {
arg0 = args[0]
arg0LC = argsLC[0]
}
arg1 := ""
arg1LC := ""
if len(args) > 1 {
arg1 = args[1]
arg1LC = argsLC[1]
}
_ = arg1
arg2 := ""
arg2LC := ""
if len(args) > 2 {
arg2 = args[2]
arg2LC = argsLC[2]
}
_ = arg2
messageAfterFirstWord := ""
if len(args) > 1 {
messageAfterFirstWord = strings.Join(args[1:], " ")
}
if false {
unused := arg0 + arg1LC + arg2LC + messageAfterFirstWord
fmt.Printf("%s", unused)
}
switch arg0LC {
case "":
case "q":
os.Exit(0)
default:
fmt.Printf("Unrecognized: '%s'\n", message)
}
// Prompt after performing command
fmt.Print("\n> ")
}
}
// Our app's signal handler
func signalHandler() {
ch := make(chan os.Signal, 2)
signal.Notify(ch, syscall.SIGTERM)
signal.Notify(ch, syscall.SIGINT)
signal.Notify(ch, syscall.SIGSEGV)
for {
switch <-ch {
case syscall.SIGINT:
fmt.Printf("*** Exiting because of SIGNAL \n")
os.Exit(0)
case syscall.SIGTERM:
return
}
}
}