|
@@ -3,15 +3,51 @@ import java.io.IOException;
|
3
|
3
|
import java.io.InputStream;
|
4
|
4
|
import java.io.InputStreamReader;
|
5
|
5
|
import java.util.ArrayList;
|
|
6
|
+import java.util.LinkedHashMap;
|
6
|
7
|
import java.util.List;
|
|
8
|
+import java.util.Map;
|
|
9
|
+
|
|
10
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
11
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
7
|
12
|
|
8
|
13
|
public class SimpleShell {
|
9
|
14
|
|
10
|
15
|
|
11
|
|
- public static void prettyPrint(String output) {
|
12
|
|
- // yep, make an effort to format things nicely, eh?
|
13
|
|
- System.out.println(output);
|
|
16
|
+ public static void prettyPrintId(String output) {
|
|
17
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
18
|
+ String result = "";
|
|
19
|
+ Id[] users = null;
|
|
20
|
+ try {
|
|
21
|
+ users= objectMapper.readValue(output, Id[].class);
|
|
22
|
+ } catch (IOException e) {
|
|
23
|
+ e.printStackTrace();
|
|
24
|
+ }
|
|
25
|
+ for(Id user: users){
|
|
26
|
+ result += "name: " + user.getName() + ", github: " + user.getGithub() + "\n";
|
|
27
|
+ }
|
|
28
|
+ System.out.println(result);
|
14
|
29
|
}
|
|
30
|
+
|
|
31
|
+ public static void prettyPrintMessage(String output){
|
|
32
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
33
|
+ String result = "";
|
|
34
|
+ Message[] messages = null;
|
|
35
|
+ try {
|
|
36
|
+ messages = objectMapper.readValue(output, Message[].class);
|
|
37
|
+ } catch (IOException e) {
|
|
38
|
+ e.printStackTrace();
|
|
39
|
+ }
|
|
40
|
+ for(Message mess: messages){
|
|
41
|
+ if(mess.getToid().equals("")) {
|
|
42
|
+ result += "from: " + mess.getFromid() + ", message: " + mess.getMessage() + "\n";
|
|
43
|
+ }
|
|
44
|
+ if(!mess.getToid().equals("")) {
|
|
45
|
+ result += "from: " + mess.getFromid() + ", to: " + mess.getToid() + ", message: " + mess.getMessage() + "\n";
|
|
46
|
+ }
|
|
47
|
+ }
|
|
48
|
+ System.out.println(result);
|
|
49
|
+ }
|
|
50
|
+
|
15
|
51
|
public static void main(String[] args) throws java.io.IOException {
|
16
|
52
|
|
17
|
53
|
YouAreEll webber = new YouAreEll();
|
|
@@ -19,86 +55,144 @@ public class SimpleShell {
|
19
|
55
|
BufferedReader console = new BufferedReader
|
20
|
56
|
(new InputStreamReader(System.in));
|
21
|
57
|
|
22
|
|
- ProcessBuilder pb = new ProcessBuilder();
|
23
|
|
- List<String> history = new ArrayList<String>();
|
|
58
|
+ ProcessBuilder processBuilder = new ProcessBuilder();
|
|
59
|
+ List<String> userInputHistory = new ArrayList<>();
|
24
|
60
|
int index = 0;
|
|
61
|
+
|
25
|
62
|
//we break out with <ctrl c>
|
26
|
63
|
while (true) {
|
27
|
|
- //read what the user enters
|
28
|
|
- System.out.println("cmd? ");
|
|
64
|
+ System.out.println("To receive all registered github ids, register a new id, or change the associated name, type 'ids'.\n" +
|
|
65
|
+ "To get messages, type 'messages'. \nTo send a message, type 'send'.\n" +
|
|
66
|
+ "To receive a history of previous commands, type 'history'.\n" +
|
|
67
|
+ "To exit, type exit. ");
|
29
|
68
|
commandLine = console.readLine();
|
30
|
|
-
|
31
|
|
- //input parsed into array of strings(command and arguments)
|
32
|
|
- String[] commands = commandLine.split(" ");
|
33
|
|
- List<String> list = new ArrayList<String>();
|
34
|
|
-
|
35
|
|
- //if the user entered a return, just loop again
|
36
|
|
- if (commandLine.equals(""))
|
37
|
|
- continue;
|
|
69
|
+ if (commandLine.equals("")) continue;
|
38
|
70
|
if (commandLine.equals("exit")) {
|
39
|
|
- System.out.println("bye!");
|
|
71
|
+ System.out.println("Goodbye");
|
40
|
72
|
break;
|
41
|
73
|
}
|
42
|
74
|
|
43
|
|
- //loop through to see if parsing worked
|
44
|
|
- for (int i = 0; i < commands.length; i++) {
|
45
|
|
- //System.out.println(commands[i]); //***check to see if parsing/split worked***
|
46
|
|
- list.add(commands[i]);
|
47
|
|
-
|
48
|
|
- }
|
49
|
|
- System.out.print(list); //***check to see if list was added correctly***
|
50
|
|
- history.addAll(list);
|
51
|
75
|
try {
|
52
|
|
- //display history of shell with index
|
53
|
|
- if (list.get(list.size() - 1).equals("history")) {
|
54
|
|
- for (String s : history)
|
55
|
|
- System.out.println((index++) + " " + s);
|
56
|
|
- continue;
|
|
76
|
+ String specificCommand = "";
|
|
77
|
+ ArrayList<String> commandList = new ArrayList<>();
|
|
78
|
+ commandList.add(commandLine);
|
|
79
|
+ MessageController mesCont = new MessageController();
|
|
80
|
+ IdController idCont = new IdController();
|
|
81
|
+
|
|
82
|
+ if (commandLine.equalsIgnoreCase("ids")) {
|
|
83
|
+ String results = "";
|
|
84
|
+ System.out.println("To retrieve all registered github ids, press enter\n" +
|
|
85
|
+ "To register a new id or change the name associated, type your name");
|
|
86
|
+ specificCommand = console.readLine();
|
|
87
|
+ if(specificCommand.equals("")) {
|
|
88
|
+ results = idCont.get_ids(commandList);
|
|
89
|
+ } else {
|
|
90
|
+ commandList.add(specificCommand);
|
|
91
|
+ System.out.println("Please enter your github id");
|
|
92
|
+ specificCommand = console.readLine();
|
|
93
|
+ commandList.add(specificCommand);
|
|
94
|
+ results = idCont.saveId(commandList);
|
|
95
|
+ }
|
|
96
|
+ SimpleShell.prettyPrintId(results);
|
|
97
|
+ System.out.println("To enter another command, press enter. To exit, type exit.");
|
|
98
|
+ commandLine = console.readLine();
|
|
99
|
+ if (commandLine.equals("")) continue;
|
|
100
|
+ if (commandLine.equals("exit")) {
|
|
101
|
+ System.out.println("Goodbye");
|
|
102
|
+ break;
|
|
103
|
+ }
|
57
|
104
|
}
|
58
|
105
|
|
59
|
|
- // Specific Commands.
|
|
106
|
+ if (commandLine.equals("messages")) {
|
|
107
|
+ String results = "";
|
|
108
|
+ System.out.println("To retrieve the last 20 messages posted on the timeline, press enter\n" +
|
|
109
|
+ "To retrieve the last twenty messages sent to you enter your github id");
|
|
110
|
+ specificCommand = console.readLine();
|
|
111
|
+ if(specificCommand.equals("\n") || specificCommand.equals("")) {
|
|
112
|
+ results = mesCont.get_messages(commandList);
|
|
113
|
+ } else {
|
|
114
|
+ commandList.add(specificCommand);
|
|
115
|
+ results = mesCont.get_my_messages(commandList);
|
|
116
|
+ }
|
|
117
|
+ SimpleShell.prettyPrintMessage(results);
|
|
118
|
+ System.out.println("To enter another command, press enter. To exit, type exit.");
|
|
119
|
+ commandLine = console.readLine();
|
|
120
|
+ if (commandLine.equals("")) continue;
|
|
121
|
+ if (commandLine.equals("exit")) {
|
|
122
|
+ System.out.println("Goodbye");
|
|
123
|
+ break;
|
|
124
|
+ }
|
|
125
|
+ }
|
60
|
126
|
|
61
|
|
- // ids
|
62
|
|
- if (list.contains("ids")) {
|
63
|
|
- String results = webber.get_ids();
|
64
|
|
- SimpleShell.prettyPrint(results);
|
65
|
|
- continue;
|
|
127
|
+
|
|
128
|
+ if(commandLine.equalsIgnoreCase("send")){
|
|
129
|
+ String results = "";
|
|
130
|
+ System.out.println("Please enter your message");
|
|
131
|
+ specificCommand = console.readLine();
|
|
132
|
+ if(!specificCommand.equals("")) {
|
|
133
|
+ commandList.add(specificCommand);
|
|
134
|
+ }
|
|
135
|
+ System.out.println(("Enter your github id"));
|
|
136
|
+ specificCommand = console.readLine();
|
|
137
|
+ if(!specificCommand.equals("")) {
|
|
138
|
+ commandList.add(specificCommand);
|
|
139
|
+ results = mesCont.post_world(commandList);
|
|
140
|
+ }
|
|
141
|
+ System.out.println(("If you wish to send your message to another github user, enter their github id." +
|
|
142
|
+ "If you wish to send a general message to the timeline, press enter"));
|
|
143
|
+ if (specificCommand.equals("")) {
|
|
144
|
+ results = mesCont.post_world(commandList);
|
|
145
|
+ }
|
|
146
|
+ if(!specificCommand.equals("")) {
|
|
147
|
+ commandList.add(specificCommand);
|
|
148
|
+ results = mesCont.post_friend(commandList);
|
|
149
|
+ }
|
|
150
|
+ SimpleShell.prettyPrintMessage(results);
|
|
151
|
+ System.out.println("To enter another command, press enter. To exit, type exit.");
|
|
152
|
+ commandLine = console.readLine();
|
|
153
|
+ if (commandLine.equals("")) continue;
|
|
154
|
+ if (commandLine.equals("exit")) {
|
|
155
|
+ System.out.println("Goodbye");
|
|
156
|
+ break;
|
|
157
|
+ }
|
66
|
158
|
}
|
67
|
159
|
|
68
|
|
- // messages
|
69
|
|
- if (list.contains("messages")) {
|
70
|
|
- String results = webber.get_messages();
|
71
|
|
- SimpleShell.prettyPrint(results);
|
|
160
|
+
|
|
161
|
+ //print history, moved it here so full commands will be in history
|
|
162
|
+ userInputHistory.addAll(commandList);
|
|
163
|
+ if (commandLine.equalsIgnoreCase("history")) {
|
|
164
|
+ for (String s : userInputHistory)
|
|
165
|
+ System.out.println((index++) + " " + s);
|
72
|
166
|
continue;
|
73
|
167
|
}
|
74
|
|
- // you need to add a bunch more.
|
75
|
168
|
|
76
|
|
- //!! command returns the last command in history
|
77
|
|
- if (list.get(list.size() - 1).equals("!!")) {
|
78
|
|
- pb.command(history.get(history.size() - 2));
|
79
|
169
|
|
80
|
|
- }//!<integer value i> command
|
81
|
|
- else if (list.get(list.size() - 1).charAt(0) == '!') {
|
82
|
|
- int b = Character.getNumericValue(list.get(list.size() - 1).charAt(1));
|
83
|
|
- if (b <= history.size())//check if integer entered isn't bigger than history size
|
84
|
|
- pb.command(history.get(b));
|
|
170
|
+ //the !! command returns the last command in userInputHistory
|
|
171
|
+ if (commandList.get(commandList.size() - 1).equals("!!")) {
|
|
172
|
+ processBuilder.command(userInputHistory.get(userInputHistory.size() - 2));
|
|
173
|
+ }
|
|
174
|
+ // !<integer value i> command
|
|
175
|
+ else if (commandList.get(commandList.size() - 1).charAt(0) == '!') {
|
|
176
|
+ int b = Character.getNumericValue(commandList.get(commandList.size() - 1).charAt(1));
|
|
177
|
+ if (b <= userInputHistory.size())
|
|
178
|
+ processBuilder.command(userInputHistory.get(b));
|
85
|
179
|
} else {
|
86
|
|
- pb.command(list);
|
|
180
|
+ processBuilder.command(commandList);
|
87
|
181
|
}
|
88
|
182
|
|
89
|
183
|
// wait, wait, what curiousness is this?
|
90
|
|
- Process process = pb.start();
|
|
184
|
+ Process process = processBuilder.start();
|
91
|
185
|
|
92
|
186
|
//obtain the input stream
|
93
|
187
|
InputStream is = process.getInputStream();
|
94
|
188
|
InputStreamReader isr = new InputStreamReader(is);
|
95
|
|
- BufferedReader br = new BufferedReader(isr);
|
|
189
|
+ BufferedReader reader = new BufferedReader(isr);
|
96
|
190
|
|
97
|
191
|
//read output of the process
|
98
|
192
|
String line;
|
99
|
|
- while ((line = br.readLine()) != null)
|
|
193
|
+ while ((line = reader.readLine()) != null)
|
100
|
194
|
System.out.println(line);
|
101
|
|
- br.close();
|
|
195
|
+ reader.close();
|
102
|
196
|
|
103
|
197
|
|
104
|
198
|
}
|