コード例 #1
0
 private void pathFieldChanged(ConsoleField sender, string newValue)
 {
     // Validate and update path
     if (Directory.Exists(newValue))
     {
         try {
             curDir = new DirectoryInfo(newValue);
             fileList.SetData(getFileList());
         } catch { }
     }
 }
コード例 #2
0
        /// <summary>
        /// Initialize the popup.
        /// </summary>
        /// <param name="title">String to be shown in the top center of the popup</param>
        /// <param name="startPath">Path of directory to start in</param>
        /// <param name="filPat">Glob-style wildcard string for matching files to show</param>
        /// <param name="toggleHeader">Header for the column with checkmarks for selected files</param>
        public ConsoleFileMultiSelectDialog(string title, string startPath, string filPat, string toggleHeader)
            : base()
        {
            CenterHeader = () => title;
            curDir       = new DirectoryInfo(startPath);
            filePattern  = filPat;

            int w = (Console.WindowWidth > idealW + 2 * hPad)
                ? idealW
                : Console.WindowWidth - 2 * hPad;
            int left  = (Console.WindowWidth - w) / 2;
            int right = -left;

            SetDimensions(left, top, right, bottom);

            AddObject(new ConsoleLabel(
                          left + 2, top + 2, left + 2 + labelW - 1,
                          () => $"Directory:",
                          () => ConsoleTheme.Current.PopupBg,
                          () => ConsoleTheme.Current.PopupFg
                          ));

            pathField = new ConsoleField(
                left + 2 + labelW, top + 2, right - 2,
                curDir.FullName
                );
            pathField.OnChange += pathFieldChanged;
            AddObject(pathField);

            AddObject(new ConsoleLabel(
                          left + 2, bottom - 1, right - 2,
                          () => $"{chosenFiles.Count} selected, {Formatting.FmtSize(totalChosenSize())}",
                          () => ConsoleTheme.Current.PopupBg,
                          () => ConsoleTheme.Current.PopupFg
                          ));

            // ListBox showing zip files in current dir
            fileList = new ConsoleListBox <FileSystemInfo>(
                left + 2, top + 4, right - 2, bottom - 2,
                getFileList(),
                new List <ConsoleListBoxColumn <FileSystemInfo> >()
            {
                new ConsoleListBoxColumn <FileSystemInfo>()
                {
                    Header   = toggleHeader,
                    Width    = 8,
                    Renderer = getRowSymbol
                }, new ConsoleListBoxColumn <FileSystemInfo>()
                {
                    Header   = "Name",
                    Width    = 36,
                    Renderer = getRowName,
                    Comparer = compareNames
                }, new ConsoleListBoxColumn <FileSystemInfo>()
                {
                    Header = "Size",
                    // Longest: "1023.1 KB"
                    Width    = 9,
                    Renderer = (FileSystemInfo fi) => getLength(fi),
                    Comparer = (a, b) => {
                        FileInfo fa = a as FileInfo, fb = b as FileInfo;
                        return(fa == null
                                ? (fb == null ? 0 : -1)
                                : (fb == null ? 1 : fa.Length.CompareTo(fb.Length)));
                    }
                }, new ConsoleListBoxColumn <FileSystemInfo>()
                {
                    Header   = "Accessed",
                    Width    = 10,
                    Renderer = (FileSystemInfo fi) => fi.LastWriteTime.ToString("yyyy-MM-dd"),
                    Comparer = (a, b) => a.LastWriteTime.CompareTo(b.LastWriteTime)
                }
            },
                1, 1, ListSortDirection.Ascending
                );
            AddObject(fileList);

            AddTip("Esc", "Cancel");
            AddBinding(Keys.Escape, (object sender) => {
                chosenFiles.Clear();
                return(false);
            });

            AddTip("F10", "Sort");
            AddBinding(Keys.F10, (object sender) => {
                fileList.SortMenu().Run(right - 2, top + 2);
                DrawBackground();
                return(true);
            });

            AddTip("Enter", "Change directory", () => fileList.Selection != null && isDir(fileList.Selection));
            AddTip("Enter", "Select", () => fileList.Selection != null && !isDir(fileList.Selection));
            AddBinding(Keys.Enter, (object sender) => selectRow());
            AddBinding(Keys.Space, (object sender) => selectRow());

            AddTip("Ctrl+A", "Select all");
            AddBinding(Keys.CtrlA, (object sender) => {
                foreach (FileSystemInfo fi in contents)
                {
                    if (!isDir(fi))
                    {
                        FileInfo file = fi as FileInfo;
                        if (file != null)
                        {
                            chosenFiles.Add(file);
                        }
                    }
                }
                return(true);
            });

            AddTip("Ctrl+D", "Deselect all", () => chosenFiles.Count > 0);
            AddBinding(Keys.CtrlD, (object sender) => {
                if (chosenFiles.Count > 0)
                {
                    chosenFiles.Clear();
                }
                return(true);
            });

            AddTip("F9", "Import", () => chosenFiles.Count > 0);
            AddBinding(Keys.F9, (object sender) => {
                return(false);
            });
        }