Exemplo n.º 1
0
 public Data.Node Load(Storage storage, Data.Node data)
 {
     if (data is Data.Collection)
         for (int i = 0; i < (data as Data.Collection).Nodes.Count; i++)
             (data as Data.Collection).Nodes[i] = this.Load(storage, (data as Data.Collection).Nodes[i]);
     else if (data is Data.Branch)
     {
         Kean.Collection.IDictionary<string, Data.Node> nodes = new Kean.Collection.Dictionary<string, Data.Node>((data as Data.Branch).Nodes.Count);
         foreach (Data.Node child in (data as Data.Branch).Nodes)
         {
             Data.Node n = nodes[child.Name];
             if (n.IsNull())
                 nodes[child.Name] = child;
             else if (n is Data.Collection)
                 (n as Data.Collection).Nodes.Add(child.UpdateLocators((string)child.Locator + "[" + (n as Data.Collection).Nodes.Count + "]"));
             else
             {
                 Data.Collection collection = new Data.Collection() { Name = child.Name, Locator = child.Locator, Region = child.Region }; // TODO: include all children in region
                 collection.Nodes.Add(n.UpdateLocators((string)n.Locator + "[0]"));
                 collection.Nodes.Add(child.UpdateLocators((string)child.Locator + "[1]"));
                 nodes[child.Name] = collection;
             }
         }
         (data as Data.Branch).Nodes.Clear();
         foreach (KeyValue<string, Data.Node> n in nodes)
             (data as Data.Branch).Nodes.Add(this.Load(storage, n.Value));
     }
     return data;
 }
Exemplo n.º 2
0
		public static void Run(string[] args)
		{
			ITerminal terminal = new ConsoleTerminal();
			//LineBuffer.Abstract editor = new LineBuffer.Simple(terminal);
			//LineBuffer.Abstract editor = new LineBuffer.Interactive(terminal);
			LineBuffer.Abstract editor = new LineBuffer.InteractiveWithHistory(terminal);
			editor.Prompt = ":>";
			Tuple<string, string>[] completionDescription = new Tuple<string, string>[]
			{
				Tuple.Create<string, string>("fish", "Fish are a paraphyletic group of organisms."),
				Tuple.Create<string, string>("firefox", "Mozilla Firefox is a free and open source web browser."),
				Tuple.Create<string, string>("foot", "The foot is an anatomical structure found in many vertebrates."),
			};
		   
			editor.Complete = text =>
			{
				string result = text;
				string[] parts = text.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
				int hits = 0;
				
				if (parts.Length > 0)
				{
					string last = parts[parts.Length - 1];
					string found = null;
					foreach (Tuple<string, string> word in completionDescription)
						if (word.Item1.StartsWith(last))
						{
							found = word.Item1;
							hits++;
						}
					if (hits == 1)
						result = text.Remove(text.Length - last.Length) + found;
				}
				return result;
			};
			Kean.Collection.IDictionary<string, Action> commands = new Kean.Collection.Dictionary<string, Action>();
			commands["play"] = () => Console.WriteLine("play it again");
			commands["beep"] = () => Console.Beep();
			commands["now"] = () => Console.WriteLine("now again");
			
			editor.Execute = text =>
			{
				string[] parts = text.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
				bool correct = true;
				foreach (string part in parts)
					correct &= commands.Contains(part);
				if (correct)
					foreach (string part in parts)
						commands[part]();
				return correct;
			};
			editor.Help = text =>
			{
				System.Text.StringBuilder result = new System.Text.StringBuilder();
				string[] parts = text.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
				if (parts.Length > 0)
				{
					string last = parts[parts.Length - 1];
					foreach (Tuple<string, string> word in completionDescription)
						if (word.Item1.StartsWith(last))
							result.AppendLine(word.Item1 + " " + word.Item2);
				}
				return result.ToString();   
			};
			/*
			System.Timers.Timer timer = new System.Timers.Timer(4000);
			timer.Elapsed += (object sender, System.Timers.ElapsedEventArgs elapsedArguments) =>
			{
				editor.WriteLine("Hello");
			};
			timer.Start();
			*/
			editor.Read();
		}