Ejemplo n.º 1
0
        private static string SearchDialog(List <string> list, string currentPathUsed, bool useFiles)
        {
            var userInput = TextDialogBox("Search", "Please enter a keyword or file extension (Max: 100 Characters)",
                                          Blue, 100, false);

            if (string.IsNullOrWhiteSpace(userInput))
            {
                ButtonDialogBox("Error", "Search needs to be above 0 characters in length", Red, MessageBoxButtons.Ok);
                return(currentPathUsed);
            }

            var matchedResults = new List <string>();

            for (var i = 0;
                 i < list.Count;
                 i++)
            {
                var match = Regex.Match(list[i], userInput);
                if (match.Success)
                {
                    matchedResults.Add(list[i]);
                }
            }

            if (matchedResults.Any())
            {
                Dialog.ButtonDialogBox("Success", $"{matchedResults.Count} File(s)/Folder(s) found!", Blue,
                                       MessageBoxButtons.Ok);
                Console.BackgroundColor = Black;
                var        startingIndex              = 0;
                var        currentIndex               = 0;
                var        selectedForeground         = Green;
                var        deSelectedBackground       = Black;
                var        deSelectedForegroundFile   = Gray;
                var        deSelectedForegroundFolder = White;
                var        pageCount = 1;
                ConsoleKey key;
                while (true)
                {
                    Console.ForegroundColor = White;
                    Console.BackgroundColor = Black;
                    Console.Clear();
                    var tableLimit   = Console.WindowHeight - 4;
                    var inputAreas   = TableMaker.MakeTable(SectionCount.Two, 75);
                    var highestValue = matchedResults.Count - startingIndex < tableLimit
                        ? matchedResults.Count - startingIndex
                        : tableLimit;
                    TableMaker.TableTitle("Search");
                    TableMaker.MakeNavBar(
                        "[Enter - Open Selected Files / Open Folder | Escape - Go Back | E - Enter Directory of highlighted item]");
                    for (var i = 0; i < highestValue; i++)
                    {
                        if (i + startingIndex == currentIndex)
                        {
                            Console.ForegroundColor = selectedForeground;
                        }
                        else
                        {
                            if (File.Exists(matchedResults[i + startingIndex]))
                            {
                                Console.ForegroundColor = deSelectedForegroundFile;
                            }
                            else
                            {
                                Console.ForegroundColor = deSelectedForegroundFolder;
                            }
                        }

                        Console.SetCursorPosition(inputAreas[0], 2 + i);
                        if (matchedResults[i].Length > inputAreas[1] - 3)
                        {
                            Console.Write(matchedResults[i + startingIndex].Substring(0, inputAreas[1] - 3));
                        }
                        else
                        {
                            Console.Write(matchedResults[i + startingIndex]);
                        }

                        Console.CursorLeft = inputAreas[1];
                        Console.Write(Directory.GetLastAccessTime(matchedResults[i + startingIndex]));
                    }

                    Console.SetCursorPosition(inputAreas[0], 2 + tableLimit);
                    Console.ForegroundColor = Cyan;
                    Console.Write($"Found {matchedResults.Count} File(s)/Folder(s)");
                    Console.CursorLeft = inputAreas[1];
                    Console.Write($"Page: {pageCount}");
                    Console.SetCursorPosition(inputAreas[0], 1);
                    Console.Write("Name");
                    Console.CursorLeft = inputAreas[1];
                    Console.Write("Last Access Time");


                    var useMenu = true;
                    while (useMenu)
                    {
                        key = Console.ReadKey(true).Key;

                        switch (key)
                        {
                        case ConsoleKey.DownArrow:
                            if (currentIndex == startingIndex + tableLimit - 1 &&
                                currentIndex != list.Count - 1)
                            {
                                startingIndex = StartingIndex(tableLimit, startingIndex, Direction.Down);
                                pageCount++;
                                useMenu = false;
                                currentIndex++;
                                continue;
                            }

                            currentIndex = SelectionChange(deSelectedBackground, deSelectedForegroundFolder,
                                                           deSelectedForegroundFile, currentIndex, matchedResults, Direction.Down, inputAreas,
                                                           2, highestValue, startingIndex, false, new List <int>());
                            continue;

                        case ConsoleKey.UpArrow:
                            if (currentIndex == startingIndex && currentIndex != 0)
                            {
                                startingIndex = StartingIndex(tableLimit, startingIndex, Direction.Up);
                                pageCount--;
                                useMenu = false;
                                currentIndex--;
                                continue;
                            }

                            currentIndex = currentIndex = SelectionChange(deSelectedBackground,
                                                                          deSelectedForegroundFolder,
                                                                          deSelectedForegroundFile, currentIndex, matchedResults, Direction.Up, inputAreas,
                                                                          2, highestValue, startingIndex, false, new List <int>());
                            continue;

                        case ConsoleKey.Enter:
                            try
                            {
                                if (useFiles)
                                {
                                    if (File.Exists(matchedResults[currentIndex]))
                                    {
                                        return(matchedResults[currentIndex]);
                                    }
                                }

                                if (Directory.Exists(matchedResults[currentIndex]))
                                {
                                    return(matchedResults[currentIndex]);
                                }
                                else
                                {
                                    ButtonDialogBox("Error", "An Error has occurred", Red,
                                                    MessageBoxButtons.Ok);
                                    useMenu = false;
                                    continue;
                                }
                            }
                            catch (Exception e)
                            {
                                ButtonDialogBox("Error", e.Message, Red, MessageBoxButtons.Ok);
                                useMenu = false;
                                continue;
                            }

                        case ConsoleKey.Escape:
                            return(currentPathUsed);

                        case ConsoleKey.E:
                            var upOne = Path.GetFullPath(Path.Combine(list[currentIndex], "..\\"));
                            if (Directory.Exists(upOne))
                            {
                                return(upOne);
                            }

                            Console.Write($"Error {upOne}");
                            break;
                        }
                    }
                }
            }
            else
            {
                ButtonDialogBox("Error", "No Results Found", Red, MessageBoxButtons.Ok);
            }

            Console.ResetColor();
            return(currentPathUsed);
        }
Ejemplo n.º 2
0
        private static string FileLister3(string currentPath, bool useFiles, string fileFilter)
        {
            fileFilter = fileFilter.Replace("*", "");
            Console.BackgroundColor = Black;
            Console.Clear();
            Console.Title = "File Explorer";
            var currentlyHighlighted = new List <int>();
            const ConsoleColor selectedBackground         = ConsoleColor.Black;
            const ConsoleColor selectedForeground         = ConsoleColor.Green;
            const ConsoleColor deSelectedBackground       = ConsoleColor.Black;
            const ConsoleColor deSelectedForegroundFolder = ConsoleColor.White;
            const ConsoleColor deSelectedForegroundFile   = ConsoleColor.Gray;
            var        cut = new List <string>();
            int        highestIndex;
            int        tableLimit;
            var        pageCount     = 1;
            var        startingIndex = 0;
            var        currentIndex  = 0;
            ConsoleKey key;

            while (true)
            {
                //try
                //{
                //Starts the draw time stopwatch
                var watch = Stopwatch.StartNew();
                //Checks if a directory has been set, and if the directory exists.
                if (!Directory.Exists(currentPath))
                {
                    //Resets the console for drawing UI
                    Console.ForegroundColor = deSelectedForegroundFolder;
                    Console.Clear();
                    Console.SetCursorPosition(0, 0);
                    Console.Write("Current Version: 0.3");
                    tableLimit = Console.WindowHeight - 4;
                    //Gives us the console position values for writing data.
                    List <int> tableInputAreas = TableMaker.MakeTable(SectionCount.Three, 50, 15);
                    //Makes the table and writes the title.
                    TableMaker.TableTitle("File Explorer");
                    TableMaker.MakeNavBar(
                        "[Enter - Open Selected Files / Open Folder | Escape - Go Up One Folder]");
                    //Gets the drives that are attached to the system and that are ready.
                    var driveInfo   = DriveInfo.GetDrives();
                    var sendOffList = new List <string>();
                    //Checks if the console is large enough to hold all data, choosing the appropriate display manor to use.
                    highestIndex = driveInfo.Count() < tableLimit?driveInfo.Count() : tableLimit;

                    for (int i = 0; i < highestIndex; i++)
                    {
                        //Checks if the said option is the currently selected option from the user, the 'currentIndex' value defaults to 0.
                        if (i == currentIndex)
                        {
                            Console.ForegroundColor = selectedForeground;
                            Console.BackgroundColor = selectedBackground;
                        }
                        else
                        {
                            Console.BackgroundColor = deSelectedBackground;
                            Console.ForegroundColor = deSelectedForegroundFolder;
                        }
                        //Writes the data into the specified locations that were returned when the table was drawn.
                        Console.SetCursorPosition(tableInputAreas[0], 2 + i);
                        Console.Write(driveInfo[i].Name);
                        //Seeing as these are just drives, it doesn't need to check the last modified dates. And defaults to "---" value.
                        Console.SetCursorPosition(tableInputAreas[1], 2 + i);
                        Console.Write("---");
                        Console.SetCursorPosition(tableInputAreas[2], 2 + i);
                        Console.Write(Directory.GetLastWriteTime(driveInfo[i].Name));
                        sendOffList.Add(driveInfo[i].Name);
                    }
                    //The entire user interface has been drawn, so it stops the timer and displays the time taken to draw.
                    watch.Stop();
                    var text = $"Draw Time: {watch.ElapsedMilliseconds}ms";
                    Console.SetCursorPosition((Console.WindowWidth / 2) - (text.Length / 2), 0);
                    Console.Write(text);
                    var selectionPrompt = true;
                    //Just a while loop. Simple.
                    while (selectionPrompt)
                    {
                        //Reads the user's input.
                        key = Console.ReadKey(true).Key;
                        //Runs that input through a switch to decide what to do with it.
                        switch (key)
                        {
                        case ConsoleKey.Enter:
                            if (!Directory.Exists(driveInfo[currentIndex].Name))
                            {
                                Dialog.ButtonDialogBox("Error", "An Error has Occurred", DarkRed,
                                                       MessageBoxButtons.Ok);
                                selectionPrompt = false;
                                continue;
                            }

                            currentPath     = driveInfo[currentIndex].Name;
                            currentIndex    = 0;
                            selectionPrompt = false;
                            continue;

                        case ConsoleKey.UpArrow:
                            currentIndex = SelectionChange(Black, White, Gray, currentIndex, sendOffList,
                                                           Direction.Up, tableInputAreas, 2,
                                                           highestIndex, 0, false, new List <int>());
                            continue;

                        case ConsoleKey.DownArrow:
                            currentIndex = SelectionChange(Black, White, Gray, currentIndex, sendOffList,
                                                           Direction.Down, tableInputAreas, 2,
                                                           highestIndex, 0, false, new List <int>());
                            continue;

                        case ConsoleKey.Escape:
                            return(null);
                        }
                    }
                }
                else
                {
                    Console.Clear();
                    Console.ResetColor();
                    Console.SetCursorPosition(0, 0);
                    Console.Write("Current Version: 0.3");
                    List <int> tableInputAreas = TableMaker.MakeTable(SectionCount.Four, 50, 20, 20);
                    TableMaker.TableTitle("File Explorer");
                    var addition = useFiles ? "" : "| Space - Select Folder";
                    TableMaker.MakeNavBar(
                        $"[Enter - Open Selected Files / Open Folder | Escape - Go Up One Folder | M - Other Commands {addition}]");
                    tableLimit = Console.WindowHeight - 4;

                    if (_listCurrentPath != currentPath)
                    {
                        var filesInDirectory       = Directory.GetFiles(currentPath);
                        var directoriesInDirectory = Directory.GetDirectories(currentPath);
                        _list.Clear();
                        currentlyHighlighted.Clear();
                        for (var i = 0; i < directoriesInDirectory.Length; i++)
                        {
                            _list.Add(directoriesInDirectory[i]);
                        }

                        if (useFiles)
                        {
                            for (var i = 0; i < filesInDirectory.Length; i++)
                            {
                                if (filesInDirectory[i].Contains(fileFilter))
                                {
                                    _list.Add(filesInDirectory[i]);
                                }
                            }
                        }

                        _listCurrentPath = currentPath;
                    }

                    highestIndex = _list.Count() - startingIndex < tableLimit
                        ? (_list.Count - startingIndex)
                        : tableLimit;
                    if (_list.Any())
                    {
                        var selectedCheck = currentIndex - startingIndex;
                        for (var i = 0; i < highestIndex; i++)
                        {
                            var index = i + startingIndex;
                            var fileLastWriteDateAndTime = Directory.GetLastWriteTime(_list[index]);
                            if (i == selectedCheck)
                            {
                                Console.ForegroundColor = selectedForeground;
                                Console.BackgroundColor = selectedBackground;
                            }
                            else
                            {
                                Console.BackgroundColor = deSelectedBackground;
                                if (File.Exists(_list[index]))
                                {
                                    Console.ForegroundColor = deSelectedForegroundFile;
                                }
                                else
                                {
                                    Console.ForegroundColor = deSelectedForegroundFolder;
                                }
                            }

                            Console.SetCursorPosition(tableInputAreas[0], 2 + i);
                            if (_list[i + startingIndex].Length > tableInputAreas[1] - 3)
                            {
                                Console.Write(_list[i + startingIndex].Substring(0, tableInputAreas[1] - 3));
                            }
                            else
                            {
                                Console.Write(_list[i + startingIndex]);
                            }

                            Console.CursorLeft = tableInputAreas[1];
                            if (File.Exists(_list[i + startingIndex]))
                            {
                                Console.Write(new FileInfo(_list[i + startingIndex]).Length);
                            }
                            else
                            {
                                Console.Write("---");
                            }

                            Console.CursorLeft = tableInputAreas[2];
                            Console.Write(fileLastWriteDateAndTime);
                            CursorLeft = tableInputAreas[3];
                            if (File.Exists(_list[i + startingIndex]))
                            {
                                Console.Write(Path.GetExtension(_list[i + startingIndex]));
                            }
                            else
                            {
                                Write("\\");
                            }
                        }

                        Console.ForegroundColor = Cyan;
                        Console.SetCursorPosition(tableInputAreas[0], 1);
                        Console.Write(currentPath);
                        Console.CursorLeft = tableInputAreas[1];
                        Console.Write("Size");
                        Console.CursorLeft = tableInputAreas[2];
                        Console.Write("Last Write Time");
                        Console.CursorLeft = tableInputAreas[3];
                        Write("Type");
                        Console.ForegroundColor = White;
                        Console.SetCursorPosition(tableInputAreas[2], Console.WindowHeight - 2);
                        Console.Write($"Page: {pageCount}");
                    }
                    else
                    {
                        Console.SetCursorPosition(tableInputAreas[0], 2);
                        Console.Write("<Empty>");
                    }

                    watch.Stop();
                    var text = $"Draw Time: {watch.ElapsedMilliseconds}ms";
                    Console.SetCursorPosition((Console.WindowWidth / 2) - (text.Length / 2), 0);
                    Console.Write(text);
                    var useMenu = true;

                    while (useMenu)
                    {
                        key = Console.ReadKey(true).Key;
                        switch (key)
                        {
                        case ConsoleKey.Enter:
                            if (useFiles)
                            {
                                if (File.Exists(_list[currentIndex]))
                                {
                                    try
                                    {
                                        return(_list[currentIndex]);
                                    }
                                    catch (Exception e)
                                    {
                                        ButtonDialogBox("Error", e.Message, Red, MessageBoxButtons.Ok);
                                        useMenu = false;
                                        continue;
                                    }
                                }
                            }

                            pageCount = 1;
                            currentlyHighlighted.Clear();
                            currentPath   = _list[currentIndex];
                            startingIndex = 0;
                            currentIndex  = 0;
                            useMenu       = false;
                            continue;


                        case ConsoleKey.UpArrow:
                            if (currentIndex == startingIndex && currentIndex != 0)
                            {
                                startingIndex = StartingIndex(tableLimit, startingIndex, Direction.Up);
                                pageCount--;
                                useMenu = false;
                                currentIndex--;
                                continue;
                            }

                            currentIndex = SelectionChange(Black, White, Gray, currentIndex,
                                                           _list,
                                                           Direction.Up, tableInputAreas,
                                                           2,
                                                           highestIndex, startingIndex, true, currentlyHighlighted);
                            continue;

                        case ConsoleKey.DownArrow:
                            if (currentIndex == startingIndex + tableLimit - 1 &&
                                currentIndex != _list.Count - 1)
                            {
                                startingIndex = StartingIndex(tableLimit, startingIndex, Direction.Down);
                                pageCount++;
                                useMenu = false;
                                currentIndex++;
                                continue;
                            }

                            currentIndex = SelectionChange(Black, White, Gray, currentIndex,
                                                           _list,
                                                           Direction.Down,
                                                           tableInputAreas, 2,
                                                           highestIndex, startingIndex, true, currentlyHighlighted);
                            continue;

                        case ConsoleKey.Escape:
                            var up = Path.GetFullPath(Path.Combine(currentPath, "..\\"));
                            pageCount     = 1;
                            startingIndex = 0;
                            currentIndex  = 0;
                            if (up == currentPath)
                            {
                                currentPath = null;
                                useMenu     = false;
                                currentlyHighlighted.Clear();
                                continue;
                            }

                            currentlyHighlighted.Clear();
                            currentPath = up;
                            useMenu     = false;
                            continue;

                        case ConsoleKey.M:
                            ButtonDialogBox("Other Commands",
                                            "C - Copy Highlighted Files / Folders\nP - Paste Copied Files / Folders\nD - Delete Highlighted Files / Folders\nS - Search For a File in Directory\nX - Highlight File or Folder\nPageDown - Skip One Page Down\nPageUp - Skip One Page Up\n1 - Settings",
                                            Blue,
                                            MessageBoxButtons.Ok);
                            useMenu = false;
                            continue;

                        case ConsoleKey.PageDown:
                            if (startingIndex + tableLimit > _list.Count)
                            {
                                currentIndex = _list.Count - 1;
                                useMenu      = false;
                                continue;
                            }

                            startingIndex = StartingIndex(tableLimit, startingIndex, Direction.Down);
                            currentIndex  = startingIndex;
                            useMenu       = false;
                            pageCount++;
                            continue;

                        case ConsoleKey.PageUp:
                            if (startingIndex == 0)
                            {
                                currentIndex = 0;
                                useMenu      = false;
                                continue;
                            }

                            startingIndex = StartingIndex(tableLimit, startingIndex, Direction.Up);
                            currentIndex  = startingIndex;
                            useMenu       = false;
                            pageCount--;
                            continue;

                        case ConsoleKey.S:
                            var searchDialog = SearchDialog(_list, currentPath, useFiles);
                            if (useFiles)
                            {
                                if (File.Exists(searchDialog))
                                {
                                    return(searchDialog);
                                }
                            }

                            if (Directory.Exists(searchDialog))
                            {
                                currentPath = searchDialog;
                            }
                            Console.ForegroundColor = White;
                            Console.BackgroundColor = Black;
                            pageCount     = 1;
                            startingIndex = 0;
                            currentIndex  = 0;
                            useMenu       = false;
                            continue;

                        case ConsoleKey.D:

                            DialogResult result = ButtonDialogBox("Are you Sure?",
                                                                  $"Are you really sure you want to delete all currently highlighted items?",
                                                                  Red,
                                                                  MessageBoxButtons.YesNo);
                            Console.ResetColor();
                            if (result != DialogResult.Yes)
                            {
                                useMenu = false;
                                continue;
                            }

                            try
                            {
                                if (File.Exists(_list[currentIndex]))
                                {
                                    File.Delete(_list[currentIndex]);
                                }
                                else
                                {
                                    if (Directory.Exists(_list[currentIndex]))
                                    {
                                        Directory.Delete(_list[currentIndex], true);
                                    }
                                }

                                for (var i = 0; i < currentlyHighlighted.Count; i++)
                                {
                                    if (File.Exists(_list[currentlyHighlighted[i]]))
                                    {
                                        File.Delete(_list[currentlyHighlighted[i]]);
                                    }
                                    else
                                    {
                                        if (Directory.Exists(_list[currentlyHighlighted[i]]))
                                        {
                                            Directory.Delete(_list[currentlyHighlighted[i]], true);
                                        }
                                    }
                                }

                                useMenu       = false;
                                currentIndex  = 0;
                                startingIndex = 0;
                                pageCount     = 1;
                                continue;
                            }
                            catch (Exception e)
                            {
                                ButtonDialogBox("Error", e.Message, Red, MessageBoxButtons.Ok);
                                useMenu = false;
                                continue;
                            }

                        case ConsoleKey.X:
                            var alreadySelected = false;
                            var sameIndex       = 0;
                            for (var i = 0; i < currentlyHighlighted.Count; i++)
                            {
                                if (currentIndex == currentlyHighlighted[i])
                                {
                                    alreadySelected = true;
                                    sameIndex       = currentlyHighlighted[i];
                                }
                            }

                            if (alreadySelected)
                            {
                                currentlyHighlighted.Remove(sameIndex);
                            }
                            else
                            {
                                currentlyHighlighted.Add(currentIndex);
                            }

                            continue;

                        case ConsoleKey.C:
                            cut = FileMoving.Copy(currentlyHighlighted, _list, currentIndex, currentPath);
                            break;

                        case ConsoleKey.P:
                            FileMoving.Paste(cut, currentPath);
                            useMenu = false;
                            continue;

                        case ConsoleKey.Spacebar:
                            if (useFiles)
                            {
                                continue;
                            }
                            if (Directory.Exists(_list[currentIndex]))
                            {
                                return(_list[currentIndex]);
                            }
                            break;
                        }
                    }
                }
            }
        }