|
1 | 1 | // Set terminal height dynamically, without modifying webrepl.js |
2 | 2 | (function() { |
3 | | - var maxReadyAttempts = 90; |
| 3 | + var maxReadyAttempts = 60; |
4 | 4 | var readyAttempts = 0; |
5 | | - var resizeScheduled = false; |
6 | 5 | var resizeDebounce = null; |
7 | 6 |
|
8 | 7 | function get_term_container() { |
9 | | - return document.getElementById('term'); |
| 8 | + return document.getElementById("term"); |
10 | 9 | } |
11 | 10 |
|
12 | | - // Wrapper keeps a fixed height; rows are derived from its rendered height. |
13 | 11 | function get_term_wrapper() { |
14 | | - return document.getElementById('term-wrapper') || get_term_container(); |
| 12 | + return document.getElementById("term-wrapper") || get_term_container(); |
15 | 13 | } |
16 | 14 |
|
17 | | - // Compute rows once on load/resize using wrapper height and terminal padding. |
18 | | - function calculate_dynamic_rows() { |
| 15 | + function log_debug(message, data) { |
| 16 | + if (data) { |
| 17 | + console.debug("[webrepl_tweaks] " + message, data); |
| 18 | + return; |
| 19 | + } |
| 20 | + console.debug("[webrepl_tweaks] " + message); |
| 21 | + } |
| 22 | + |
| 23 | + function get_cell_height(term) { |
| 24 | + var core = term && term._core && term._core._renderService; |
| 25 | + var dimensions = core && core.dimensions; |
| 26 | + var cellHeight = dimensions && dimensions.actualCellHeight; |
| 27 | + if (cellHeight) { |
| 28 | + return cellHeight; |
| 29 | + } |
| 30 | + return 16; |
| 31 | + } |
| 32 | + |
| 33 | + function calculate_dynamic_rows(term) { |
19 | 34 | var wrapper = get_term_wrapper(); |
20 | 35 | var termContainer = get_term_container(); |
21 | 36 | if (!wrapper || !termContainer) { |
| 37 | + log_debug("missing wrapper or term container", { |
| 38 | + wrapper: !!wrapper, |
| 39 | + termContainer: !!termContainer |
| 40 | + }); |
22 | 41 | return null; |
23 | 42 | } |
24 | 43 | var rect = wrapper.getBoundingClientRect(); |
|
27 | 46 | var paddingTop = parseFloat(style.paddingTop) || 0; |
28 | 47 | var paddingBottom = parseFloat(style.paddingBottom) || 0; |
29 | 48 | var contentHeight = height - paddingTop - paddingBottom; |
30 | | - return Math.max(24, Math.min(80, (contentHeight - 20) / 12)) | 0; |
| 49 | + var cellHeight = get_cell_height(term); |
| 50 | + var rows = Math.floor(contentHeight / cellHeight) - 2; |
| 51 | + log_debug("row calculation", { |
| 52 | + wrapperHeight: height, |
| 53 | + paddingTop: paddingTop, |
| 54 | + paddingBottom: paddingBottom, |
| 55 | + contentHeight: contentHeight, |
| 56 | + cellHeight: cellHeight, |
| 57 | + calculatedRows: rows, |
| 58 | + safetyRows: 2 |
| 59 | + }); |
| 60 | + if (!rows || rows < 2) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + return rows; |
31 | 64 | } |
32 | 65 |
|
33 | 66 | function resize_term_rows() { |
34 | 67 | if (!window.term || !window.term.resize) { |
| 68 | + log_debug("term not ready"); |
35 | 69 | return false; |
36 | 70 | } |
37 | | - var rows = calculate_dynamic_rows(); |
| 71 | + var rows = calculate_dynamic_rows(window.term); |
38 | 72 | if (rows === null) { |
| 73 | + log_debug("skip resize (rows null)"); |
39 | 74 | return false; |
40 | 75 | } |
41 | 76 | var cols = window.term.cols || 80; |
42 | 77 | if (window.term.rows !== rows) { |
| 78 | + log_debug("resize term", { cols: cols, rows: rows }); |
43 | 79 | window.term.resize(cols, rows); |
| 80 | + } else { |
| 81 | + log_debug("rows unchanged", { rows: rows }); |
44 | 82 | } |
45 | 83 | return true; |
46 | 84 | } |
47 | 85 |
|
48 | | - function schedule_resize() { |
49 | | - if (resizeScheduled) { |
50 | | - return; |
51 | | - } |
52 | | - resizeScheduled = true; |
53 | | - window.requestAnimationFrame(function() { |
54 | | - resizeScheduled = false; |
55 | | - resize_term_rows(); |
56 | | - }); |
57 | | - } |
58 | | - |
59 | | - function schedule_resize_debounced() { |
| 86 | + function schedule_resize_debounced(reason) { |
60 | 87 | if (resizeDebounce) { |
61 | 88 | window.clearTimeout(resizeDebounce); |
62 | 89 | } |
63 | 90 | resizeDebounce = window.setTimeout(function() { |
64 | 91 | resizeDebounce = null; |
65 | | - schedule_resize(); |
66 | | - }, 80); |
| 92 | + log_debug("resize tick", { reason: reason || "unknown" }); |
| 93 | + resize_term_rows(); |
| 94 | + }, 100); |
67 | 95 | } |
68 | 96 |
|
69 | 97 | function wait_for_term_ready() { |
70 | 98 | if (window.term && window.term.resize) { |
71 | | - schedule_resize_debounced(); |
| 99 | + schedule_resize_debounced("term-ready"); |
72 | 100 | if (resize_term_rows()) { |
73 | 101 | return; |
74 | 102 | } |
75 | 103 | } |
76 | 104 | if (readyAttempts >= maxReadyAttempts) { |
| 105 | + log_debug("giving up waiting for term"); |
77 | 106 | return; |
78 | 107 | } |
79 | 108 | readyAttempts += 1; |
80 | 109 | window.requestAnimationFrame(wait_for_term_ready); |
81 | 110 | } |
82 | 111 |
|
83 | | - window.addEventListener('load', function() { |
| 112 | + window.addEventListener("load", function() { |
| 113 | + log_debug("window load"); |
84 | 114 | wait_for_term_ready(); |
85 | | - schedule_resize_debounced(); |
| 115 | + schedule_resize_debounced("load"); |
86 | 116 | }); |
87 | 117 |
|
88 | | - window.addEventListener('resize', function() { |
89 | | - schedule_resize_debounced(); |
| 118 | + window.addEventListener("resize", function() { |
| 119 | + schedule_resize_debounced("window-resize"); |
90 | 120 | }); |
91 | 121 | }).call(this); |
0 commit comments