settings-list.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import { fuzzyFilter } from "../fuzzy.ts";
  2. import { getKeybindings } from "../keybindings.ts";
  3. import type { Component } from "../tui.ts";
  4. import { truncateToWidth, visibleWidth, wrapTextWithAnsi } from "../utils.ts";
  5. import { Input } from "./input.ts";
  6. export interface SettingItem {
  7. /** Unique identifier for this setting */
  8. id: string;
  9. /** Display label (left side) */
  10. label: string;
  11. /** Optional description shown when selected */
  12. description?: string;
  13. /** Current value to display (right side) */
  14. currentValue: string;
  15. /** If provided, Enter/Space cycles through these values */
  16. values?: string[];
  17. /** If provided, Enter opens this submenu. Receives current value and done callback. */
  18. submenu?: (currentValue: string, done: (selectedValue?: string) => void) => Component;
  19. }
  20. export interface SettingsListTheme {
  21. label: (text: string, selected: boolean) => string;
  22. value: (text: string, selected: boolean) => string;
  23. description: (text: string) => string;
  24. cursor: string;
  25. hint: (text: string) => string;
  26. }
  27. export interface SettingsListOptions {
  28. enableSearch?: boolean;
  29. }
  30. export class SettingsList implements Component {
  31. private items: SettingItem[];
  32. private filteredItems: SettingItem[];
  33. private theme: SettingsListTheme;
  34. private selectedIndex = 0;
  35. private maxVisible: number;
  36. private onChange: (id: string, newValue: string) => void;
  37. private onCancel: () => void;
  38. private searchInput?: Input;
  39. private searchEnabled: boolean;
  40. // Submenu state
  41. private submenuComponent: Component | null = null;
  42. private submenuItemIndex: number | null = null;
  43. constructor(
  44. items: SettingItem[],
  45. maxVisible: number,
  46. theme: SettingsListTheme,
  47. onChange: (id: string, newValue: string) => void,
  48. onCancel: () => void,
  49. options: SettingsListOptions = {},
  50. ) {
  51. this.items = items;
  52. this.filteredItems = items;
  53. this.maxVisible = maxVisible;
  54. this.theme = theme;
  55. this.onChange = onChange;
  56. this.onCancel = onCancel;
  57. this.searchEnabled = options.enableSearch ?? false;
  58. if (this.searchEnabled) {
  59. this.searchInput = new Input();
  60. }
  61. }
  62. /** Update an item's currentValue */
  63. updateValue(id: string, newValue: string): void {
  64. const item = this.items.find((i) => i.id === id);
  65. if (item) {
  66. item.currentValue = newValue;
  67. }
  68. }
  69. invalidate(): void {
  70. this.submenuComponent?.invalidate?.();
  71. }
  72. render(width: number): string[] {
  73. // If submenu is active, render it instead
  74. if (this.submenuComponent) {
  75. return this.submenuComponent.render(width);
  76. }
  77. return this.renderMainList(width);
  78. }
  79. private renderMainList(width: number): string[] {
  80. const lines: string[] = [];
  81. if (this.searchEnabled && this.searchInput) {
  82. lines.push(...this.searchInput.render(width));
  83. lines.push("");
  84. }
  85. if (this.items.length === 0) {
  86. lines.push(this.theme.hint(" No settings available"));
  87. if (this.searchEnabled) {
  88. this.addHintLine(lines, width);
  89. }
  90. return lines;
  91. }
  92. const displayItems = this.searchEnabled ? this.filteredItems : this.items;
  93. if (displayItems.length === 0) {
  94. lines.push(truncateToWidth(this.theme.hint(" No matching settings"), width));
  95. this.addHintLine(lines, width);
  96. return lines;
  97. }
  98. // Calculate visible range with scrolling
  99. const startIndex = Math.max(
  100. 0,
  101. Math.min(this.selectedIndex - Math.floor(this.maxVisible / 2), displayItems.length - this.maxVisible),
  102. );
  103. const endIndex = Math.min(startIndex + this.maxVisible, displayItems.length);
  104. // Calculate max label width for alignment
  105. const maxLabelWidth = Math.min(30, Math.max(...this.items.map((item) => visibleWidth(item.label))));
  106. // Render visible items
  107. for (let i = startIndex; i < endIndex; i++) {
  108. const item = displayItems[i];
  109. if (!item) continue;
  110. const isSelected = i === this.selectedIndex;
  111. const prefix = isSelected ? this.theme.cursor : " ";
  112. const prefixWidth = visibleWidth(prefix);
  113. // Pad label to align values
  114. const labelPadded = item.label + " ".repeat(Math.max(0, maxLabelWidth - visibleWidth(item.label)));
  115. const labelText = this.theme.label(labelPadded, isSelected);
  116. // Calculate space for value
  117. const separator = " ";
  118. const usedWidth = prefixWidth + maxLabelWidth + visibleWidth(separator);
  119. const valueMaxWidth = width - usedWidth - 2;
  120. const valueText = this.theme.value(truncateToWidth(item.currentValue, valueMaxWidth, ""), isSelected);
  121. lines.push(truncateToWidth(prefix + labelText + separator + valueText, width));
  122. }
  123. // Add scroll indicator if needed
  124. if (startIndex > 0 || endIndex < displayItems.length) {
  125. const scrollText = ` (${this.selectedIndex + 1}/${displayItems.length})`;
  126. lines.push(this.theme.hint(truncateToWidth(scrollText, width - 2, "")));
  127. }
  128. // Add description for selected item
  129. const selectedItem = displayItems[this.selectedIndex];
  130. if (selectedItem?.description) {
  131. lines.push("");
  132. const wrappedDesc = wrapTextWithAnsi(selectedItem.description, width - 4);
  133. for (const line of wrappedDesc) {
  134. lines.push(this.theme.description(` ${line}`));
  135. }
  136. }
  137. // Add hint
  138. this.addHintLine(lines, width);
  139. return lines;
  140. }
  141. handleInput(data: string): void {
  142. // If submenu is active, delegate all input to it
  143. // The submenu's onCancel (triggered by escape) will call done() which closes it
  144. if (this.submenuComponent) {
  145. this.submenuComponent.handleInput?.(data);
  146. return;
  147. }
  148. // Main list input handling
  149. const kb = getKeybindings();
  150. const displayItems = this.searchEnabled ? this.filteredItems : this.items;
  151. if (kb.matches(data, "tui.select.up")) {
  152. if (displayItems.length === 0) return;
  153. this.selectedIndex = this.selectedIndex === 0 ? displayItems.length - 1 : this.selectedIndex - 1;
  154. } else if (kb.matches(data, "tui.select.down")) {
  155. if (displayItems.length === 0) return;
  156. this.selectedIndex = this.selectedIndex === displayItems.length - 1 ? 0 : this.selectedIndex + 1;
  157. } else if (kb.matches(data, "tui.select.confirm") || data === " ") {
  158. this.activateItem();
  159. } else if (kb.matches(data, "tui.select.cancel")) {
  160. this.onCancel();
  161. } else if (this.searchEnabled && this.searchInput) {
  162. const sanitized = data.replace(/ /g, "");
  163. if (!sanitized) {
  164. return;
  165. }
  166. this.searchInput.handleInput(sanitized);
  167. this.applyFilter(this.searchInput.getValue());
  168. }
  169. }
  170. private activateItem(): void {
  171. const item = this.searchEnabled ? this.filteredItems[this.selectedIndex] : this.items[this.selectedIndex];
  172. if (!item) return;
  173. if (item.submenu) {
  174. // Open submenu, passing current value so it can pre-select correctly
  175. this.submenuItemIndex = this.selectedIndex;
  176. this.submenuComponent = item.submenu(item.currentValue, (selectedValue?: string) => {
  177. if (selectedValue !== undefined) {
  178. item.currentValue = selectedValue;
  179. this.onChange(item.id, selectedValue);
  180. }
  181. this.closeSubmenu();
  182. });
  183. } else if (item.values && item.values.length > 0) {
  184. // Cycle through values
  185. const currentIndex = item.values.indexOf(item.currentValue);
  186. const nextIndex = (currentIndex + 1) % item.values.length;
  187. const newValue = item.values[nextIndex];
  188. item.currentValue = newValue;
  189. this.onChange(item.id, newValue);
  190. }
  191. }
  192. private closeSubmenu(): void {
  193. this.submenuComponent = null;
  194. // Restore selection to the item that opened the submenu
  195. if (this.submenuItemIndex !== null) {
  196. this.selectedIndex = this.submenuItemIndex;
  197. this.submenuItemIndex = null;
  198. }
  199. }
  200. private applyFilter(query: string): void {
  201. this.filteredItems = fuzzyFilter(this.items, query, (item) => item.label);
  202. this.selectedIndex = 0;
  203. }
  204. private addHintLine(lines: string[], width: number): void {
  205. lines.push("");
  206. lines.push(
  207. truncateToWidth(
  208. this.theme.hint(
  209. this.searchEnabled
  210. ? " Type to search · Enter/Space to change · Esc to cancel"
  211. : " Enter/Space to change · Esc to cancel",
  212. ),
  213. width,
  214. ),
  215. );
  216. }
  217. }