Mono.CSharpShell.PrettyPrint C# (CSharp) Method

PrettyPrint() static private method

static private PrettyPrint ( TextWriter output, object result ) : void
output System.IO.TextWriter
result object
return void
		internal static void PrettyPrint (TextWriter output, object result)
		{
			if (result == null){
				p (output, "null");
				return;
			}
			
			if (result is Array){
				Array a = (Array) result;
				
				p (output, "{ ");
				int top = a.GetUpperBound (0);
				for (int i = a.GetLowerBound (0); i <= top; i++){
					PrettyPrint (output, a.GetValue (i));
					if (i != top)
						p (output, ", ");
				}
				p (output, " }");
			} else if (result is bool){
				if ((bool) result)
					p (output, "true");
				else
					p (output, "false");
			} else if (result is string){
				p (output, String.Format ("\"{0}\"", EscapeString ((string)result)));
			} else if (result is IDictionary){
				IDictionary dict = (IDictionary) result;
				int top = dict.Count, count = 0;
				
				p (output, "{");
				foreach (DictionaryEntry entry in dict){
					count++;
					p (output, "{ ");
					PrettyPrint (output, entry.Key);
					p (output, ", ");
					PrettyPrint (output, entry.Value);
					if (count != top)
						p (output, " }, ");
					else
						p (output, " }");
				}
				p (output, "}");
			} else if (result is IEnumerable) {
				int i = 0;
				p (output, "{ ");
				foreach (object item in (IEnumerable) result) {
					if (i++ != 0)
						p (output, ", ");

					PrettyPrint (output, item);
				}
				p (output, " }");
			} else if (result is char) {
				EscapeChar (output, (char) result);
			} else {
				p (output, result.ToString ());
			}
		}

Usage Example

Beispiel #1
0
        public void RunRepl(NetworkStream s)
        {
            string input = null;

            while (!InteractiveBase.QuitRequested)
            {
                try {
                    string       error_string;
                    StringWriter error_output = new StringWriter();
//					Report.Stderr = error_output;

                    string line = s.GetString();

                    bool   result_set;
                    object result;

                    if (input == null)
                    {
                        input = line;
                    }
                    else
                    {
                        input = input + "\n" + line;
                    }

                    try {
                        input = evaluator.Evaluate(input, out result, out result_set);
                    } catch (Exception e) {
                        s.WriteByte((byte)AgentStatus.ERROR);
                        s.WriteString(e.ToString());
                        s.WriteByte((byte)AgentStatus.RESULT_NOT_SET);
                        continue;
                    }

                    if (input != null)
                    {
                        s.WriteByte((byte)AgentStatus.PARTIAL_INPUT);
                        continue;
                    }

                    // Send warnings and errors back
                    error_string = error_output.ToString();
                    if (error_string.Length != 0)
                    {
                        s.WriteByte((byte)AgentStatus.ERROR);
                        s.WriteString(error_output.ToString());
                    }

                    if (result_set)
                    {
                        s.WriteByte((byte)AgentStatus.RESULT_SET);
                        StringWriter sr = new StringWriter();
                        CSharpShell.PrettyPrint(sr, result);
                        s.WriteString(sr.ToString());
                    }
                    else
                    {
                        s.WriteByte((byte)AgentStatus.RESULT_NOT_SET);
                    }
                } catch (IOException) {
                    break;
                } catch (Exception e) {
                    Console.WriteLine(e);
                }
            }
        }