Inheritance: PowerArgs.Cli.ConsolePanel
Exemplo n.º 1
0
 private void Push(string route, Page p)
 {
     stack.Push(new KeyValuePair<string, Page>(route, p));
     FirePropertyChanged(nameof(CurrentPage));
     FirePropertyChanged(nameof(CurrentPath));
 }
Exemplo n.º 2
0
        private bool TryResolveRoute(ref string path, out Page page)
        {
            if (path.EndsWith("/")) path = path.Substring(0, path.Length - 1);

            if(path == "")
            {
                if(defaultRoute.HasValue)
                {
                    path = defaultRoute.Value.Key;
                    page = defaultRoute.Value.Value();
                    page.RouteVariables = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>());
                    page.Path = defaultRoute.Value.Key;
                    return true;
                }
                else
                {
                    page = null;
                    return false;
                }
            }

            var pathSegments = path.Split('/');
            foreach (var route in routes.Keys)
            {
                var variablesCandidate = new Dictionary<string, string>();
                var routeSegments = route.Split('/');

                if(pathSegments.Length == routeSegments.Length)
                {
                    // the path and route has the same # of segments
                }
                else if(routeSegments.Length > pathSegments.Length)
                {
                    // path can't match route because the path is too short, skip it
                    continue;
                }
                else if(pathSegments.Length > routeSegments.Length && route.Contains("{*}"))
                {
                    // this route supports dynamic routing so it's ok to keep checking if the path is longer than the route
                }
                else
                {
                    continue;
                }

                bool candidateIsGood = true;
                for(int i = 0; i < routeSegments.Length; i++)
                {
                    var routeSegment = routeSegments[i];
                    var pathSegment = pathSegments[i];

                    if(routeSegment.StartsWith("{") && routeSegment.EndsWith("}"))
                    {
                        var variableName = routeSegment.Substring(1, routeSegment.Length - 2);

                        if (variableName == "*")
                        {
                            var restOfPathValue = "";
                            for(int j = i; j < pathSegments.Length; j++)
                            {
                                restOfPathValue += pathSegments[j]+"/";
                            }
                            restOfPathValue = restOfPathValue.Substring(0, restOfPathValue.Length - 1);
                            variablesCandidate.Add(variableName, restOfPathValue);
                            break;
                        }
                        else
                        {
                            variablesCandidate.Add(variableName, pathSegment);
                        }
                    }
                    else if(routeSegment.Equals(pathSegment, StringComparison.InvariantCultureIgnoreCase) == false)
                    {
                        candidateIsGood = false;
                        break;
                    }
                }

                if(candidateIsGood)
                {
                    page = routes[route]();
                    page.RouteVariables = new ReadOnlyDictionary<string, string>(variablesCandidate);
                    page.Path = path;
                    return true;
                }
            }

            page = null;
            return false;
        }
        private Page CreateExplorerPage()
        {
            var explorerPage = new Page();

            explorerPage.Loaded += () =>
            {
                var path = explorerPage.RouteVariables.ContainsKey("*") ? explorerPage.RouteVariables["*"] : "";

                if(path.EndsWith(":"))
                {
                    path += "/";
                }

                List<object> items;
                if (path == "")
                {
                    items = System.Environment.GetLogicalDrives().Select(d => new Drive() { Letter = d } as object).ToList();
                }
                else
                {
                    items = new List<object>();
                    try
                    {
                        items.AddRange(Directory.GetDirectories(path).Select(d => new FileRecord(d)));
                        items.AddRange(Directory.GetFiles(path).Select(d => new FileRecord(d)));
                    }
                    catch (UnauthorizedAccessException) { }
                }

                var grid = explorerPage.Add(new Grid(items));
                var filter = explorerPage.Add(new TextBox() { Y = 1 });

                grid.Width = explorerPage.Width;
                grid.Height = explorerPage.Height - 2;
                grid.Y = 2;

                filter.Width = explorerPage.Width;
                grid.FilterTextBox = filter;

                explorerPage.Width = explorerPage.Application.LayoutRoot.Width;
                explorerPage.Height = explorerPage.Application.LayoutRoot.Height;
                grid.Width = explorerPage.Width;
                grid.Height = explorerPage.Height - 2;
                filter.Width = explorerPage.Width;

                var pathColumn = grid.VisibleColumns.Where(c => c.ColumnDisplayName.ToString() == "Path").SingleOrDefault();
                if (pathColumn != null)
                {
                    grid.VisibleColumns.Remove(pathColumn);
                }

                grid.SelectedItemActivated += () =>
                {
                    if (grid.SelectedItem is Drive)
                    {
                        explorerPage.PageStack.Navigate((grid.SelectedItem as Drive).Letter.Replace('\\', '/'));
                    }
                    else if(Directory.Exists((grid.SelectedItem as FileRecord).Path.Replace('\\', '/')))
                    {
                        explorerPage.PageStack.Navigate((grid.SelectedItem as FileRecord).Path.Replace('\\', '/'));
                    }
                    else
                    {
                        Process.Start((grid.SelectedItem as FileRecord).Path.Replace('\\', '/'));
                    }
                };

                grid.KeyInputReceived.SubscribeForLifetime((keyInfo) =>
                {
                    if(keyInfo.Key == ConsoleKey.Delete && grid.SelectedItem != null)
                    {
                        if(grid.SelectedItem is FileRecord)
                        {
                            var deletePath = (grid.SelectedItem as FileRecord).Path;
                            if(File.Exists(deletePath))
                            {
                                Dialog.ShowMessage("Are you sure you want to delete the file ".ToConsoleString() + Path.GetFileName(deletePath).ToConsoleString(ConsoleColor.Yellow) + "?", (response) =>
                                {
                                    if(response != null && response.DisplayText == "Yes")
                                    {
                                        File.Delete(deletePath);
                                        explorerPage.PageStack.Refresh();
                                    }
                                }, true, 12, new DialogButton() { DisplayText = "Yes" }, new DialogButton() { DisplayText = "No" });
                            }
                        }
                    }
                }, grid.LifetimeManager);

                grid.TryFocus();
            };
            return explorerPage;
        }