~jpgleeson/spawsh

f49645e43411ea5649670c54c5c0b9c13f24b497 — Jack Gleeson 7 months ago 2a2e9b4
TUI interface for interactive mode.
1 files changed, 71 insertions(+), 44 deletions(-)

M Program.cs
M Program.cs => Program.cs +71 -44
@@ 13,6 13,7 @@ namespace spawsh
        static string page = "/";
        static string[] LineBuffer;
        static string[] linksInPage;
        static string[] DisplayBuffer;
        static bool inInteractive = false;
        static int selectedLinkIndex = -1;
        static string currentPage;


@@ 85,6 86,9 @@ namespace spawsh
                buildRequest(server + page);
                LineBuffer = fetchPage();
                linksInPage = buildLinkSet(LineBuffer);
                buildDisplayBuffer();

                currentOffset = 0;

                while (inInteractive)
                {


@@ 122,42 126,18 @@ namespace spawsh
            Console.Write("\x1b[3J");
            Console.Write("\x1b[H");

            for (int i = 0; i < Console.WindowWidth - LineBuffer[0].Length; i++)
            {
                Console.Write(" ");    
            }
            //Console.WriteLine(LineBuffer[0]);

            for (int i = 0; i < Console.WindowWidth; i++)
            for (int i = 0; i < Console.WindowWidth - (server.Length + page.Length); i++)
            {
                Console.Write("-");
            }
            Console.Write(server + page);
            Console.Write('\n');

            displayWindow(currentOffset);

            for (int i = 1; i < LineBuffer.Length; i++)
            {
                if (LineBuffer[i] != null)
                {
                    if (LineBuffer[i].Length < Console.WindowWidth)
                    {
                        //Console.WriteLine(LineBuffer[i]);
                    }
                    else if (LineBuffer[i].Substring(0, 2) == "=>")
                    {
                        //Console.WriteLine(LineBuffer[i]);
                    }
                    else
                    {
                        //lineWrapString(LineBuffer[i]);
                    }
                }
            }

            if (selectedLinkIndex != -1)
            {
                Console.WriteLine(linksInPage[selectedLinkIndex]);
                Console.Write(linksInPage[selectedLinkIndex]);
            }

            ConsoleKeyInfo keyRead = Console.ReadKey();


@@ 190,6 170,8 @@ namespace spawsh
                    currentOffset = 0;

                    LineBuffer = fetchedPage;

                    buildDisplayBuffer();
                    
                    if (LineBuffer[0] == "No such host is known." || LineBuffer[0] == "Resource temporarily unavailable")
                    {


@@ 222,9 204,9 @@ namespace spawsh
            else if (keyRead.Key == ConsoleKey.DownArrow)
            {
                currentOffset++;
                if (currentOffset > (windowLineCount - currentOffset))
                if (currentOffset > (DisplayBuffer.Length - windowLineCount))
                {
                    currentOffset = windowLineCount - (currentOffset + 1);
                    currentOffset = DisplayBuffer.Length - windowLineCount;
                }
            }
            else if (keyRead.Key == ConsoleKey.UpArrow)


@@ 279,7 261,6 @@ namespace spawsh
                    sslStream.Close();
                }
            }

            
            client.Close();



@@ 462,7 443,7 @@ namespace spawsh

            if (responseCode == "20")
            {
                // Console.WriteLine(responseBody);
                //This is good and normal. Don't do anything.
            }
            else if (responseCode[0] == '3')
            {


@@ 508,20 489,58 @@ namespace spawsh

        static void displayWindow(int currentOffset)
        {
            for (int i = currentOffset; i < currentOffset + windowLineCount; i++)
            for (int i = currentOffset + 1; i < currentOffset + windowLineCount - 2; i++)
            {
                if (i < LineBuffer.Length)
                if (i < DisplayBuffer.Length)
                {
                    if (LineBuffer[i] != null)
                    if (DisplayBuffer[i] != null)
                    {
                        Console.WriteLine(LineBuffer[i]);
                        Console.WriteLine(DisplayBuffer[i]);
                    }
                }
            }
            Console.Write("-=-=-=-=- =>");
        }

        static void lineWrapString(string input)
        static void buildDisplayBuffer() {
            string[] tempBuffer = new string[1024];
            int currentPointer = 0;

            foreach (string singleLine in LineBuffer) {
                if (singleLine.Length < Console.WindowWidth)
                    {
                        tempBuffer[currentPointer] = singleLine;
                        currentPointer++;
                    }
                    else if (singleLine.Substring(0, 2) == "=>")
                    {
                        //Console.WriteLine(LineBuffer[i]);
                        //out put the link description if possiblr
                    }
                    else
                    {
                        string[] returnLines = lineWrapString(singleLine);
                        int incrementRecurse = 0;
                        for (int i = 0; i < returnLines.Length; i++) {
                            if (returnLines[i] != null) {
                                tempBuffer[currentPointer + i] = returnLines[i];
                                incrementRecurse++;
                            }
                        }
                        currentPointer += incrementRecurse;
                    }
            }

            DisplayBuffer = new string[currentPointer];
            for (int i = 0; i < currentPointer; i++) {
                DisplayBuffer[i] = tempBuffer[i];
            }
        }

        static string[] lineWrapString(string input)
        {
            int linesToAdd = 0;
            string[] returnLines = new string[256];

            if (input.Length > Console.WindowWidth)
            {


@@ 540,23 559,31 @@ namespace spawsh
                if (lastNiceBreak != -1)
                {
                    outputString = input.Substring(0, lastNiceBreak);
                    Console.WriteLine(outputString);
                    returnLines[linesToAdd] = outputString;
                    linesToAdd++;
                    string remainingString = input.Substring(lastNiceBreak + 1);
                    //call this on the remaining string
                    lineWrapString(remainingString);
                    string[] recursedLines =  lineWrapString(remainingString);

                    for (int f = 0; f < recursedLines.Length; f++)
                    {
                        if (recursedLines[f] == null) {
                            break;
                        }
                        returnLines[linesToAdd] = recursedLines[f];
                        linesToAdd++;
                    }
                }
                else
                {
                    Console.WriteLine(input);
                    returnLines[linesToAdd] = outputString;
                    linesToAdd++;
                }
                

                
            }
            else
            {
                Console.WriteLine(input);
                returnLines[linesToAdd] = input;
            }
            return returnLines;
        }

    }