System.TermInfoDriver.Read C# (CSharp) Method

Read() public method

public Read ( [ dest, int index, int count ) : int
dest [
index int
count int
return int
		public int Read ([In, Out] char [] dest, int index, int count)
		{
			bool fresh, echo = false;
			StringBuilder sbuf;
			ConsoleKeyInfo key;
			int BoL = 0;  // Beginning-of-Line marker (can't backspace beyond this)
			object o;
			char c;

			sbuf = new StringBuilder ();

			// consume buffered keys first (do not echo, these have already been echo'd)
			while (true) {
				if ((o = GetKeyFromBuffer (true)) == null)
					break;

				key = (ConsoleKeyInfo) o;
				c = key.KeyChar;

				if (key.Key != ConsoleKey.Backspace) {
					if (key.Key == ConsoleKey.Enter)
						BoL = sbuf.Length;

					sbuf.Append (c);
				} else if (sbuf.Length > BoL) {
					sbuf.Length--;
				}
			}

			// continue reading until Enter is hit
			rl_startx = cursorLeft;
			rl_starty = cursorTop;

			do {
				key = ReadKeyInternal (out fresh);
				echo = echo || fresh;
				c = key.KeyChar;

				if (key.Key != ConsoleKey.Backspace) {
					if (key.Key == ConsoleKey.Enter)
						BoL = sbuf.Length;

					sbuf.Append (c);
				} else if (sbuf.Length > BoL) {
					sbuf.Length--;
				} else {
					continue;
				}

				// echo fresh keys back to the console
				if (echo)
					Echo (key);
			} while (key.Key != ConsoleKey.Enter);

			EchoFlush ();

			rl_startx = -1;
			rl_starty = -1;

			// copy up to count chars into dest
			int nread = 0;
			while (count > 0 && nread < sbuf.Length) {
				dest[index + nread] = sbuf[nread];
				nread++;
				count--;
			}

			// put the rest back into our key buffer
			for (int i = nread; i < sbuf.Length; i++)
				AddToBuffer (sbuf[i]);

			return nread;
		}