/// <summary> /// Initialize the Dialog /// </summary> /// <param name="m">Message to show</param> /// <param name="hdr">Text for column header of list box</param> /// <param name="c">List of objects to put in the list box</param> /// <param name="renderer">Function to generate text for each option</param> /// <param name="comparer">Optional function to sort the rows</param> public ConsoleChoiceDialog(string m, string hdr, List <ChoiceT> c, Func <ChoiceT, string> renderer, Comparison <ChoiceT> comparer = null) : base() { int l = GetLeft(), r = GetRight(); int w = r - l + 1; // Resize the window to fit the content List <string> msgLines = Formatting.WordWrap(m, w - 4); int h = 2 + msgLines.Count + 1 + 1 + c.Count + 2; int t = (Console.WindowHeight - h) / 2; int b = t + h - 1; SetDimensions(l, t, r, b); // Wrapped message at top ConsoleTextBox tb = new ConsoleTextBox( l + 2, t + 2, r - 2, t + 2 + msgLines.Count - 1, false, TextAlign.Left, () => ConsoleTheme.Current.PopupBg, () => ConsoleTheme.Current.PopupFg ); AddObject(tb); tb.AddLine(m); // ConsoleListBox<ChoiceT> of choices at bottom choices = new ConsoleListBox <ChoiceT>( l + 2, t + 2 + msgLines.Count + 1, r - 2, b - 2, c, new List <ConsoleListBoxColumn <ChoiceT> >() { new ConsoleListBoxColumn <ChoiceT>() { Header = hdr, Width = w - 6, Renderer = renderer, Comparer = comparer } }, 0, 0, ListSortDirection.Ascending ); choices.AddTip("Enter", "Accept"); choices.AddBinding(Keys.Enter, (object sender) => { return(false); }); choices.AddTip("Esc", "Cancel"); choices.AddBinding(Keys.Escape, (object sender) => { cancelled = true; return(false); }); AddObject(choices); }
/// <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); }); }