@shitiandmw/node-pty 1.1.0-agent.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (288) hide show
  1. package/LICENSE +69 -0
  2. package/README.md +170 -0
  3. package/binding.gyp +111 -0
  4. package/deps/.editorconfig +2 -0
  5. package/deps/winpty/.drone.yml +17 -0
  6. package/deps/winpty/.gitattributes +19 -0
  7. package/deps/winpty/LICENSE +21 -0
  8. package/deps/winpty/Makefile +166 -0
  9. package/deps/winpty/README.md +151 -0
  10. package/deps/winpty/RELEASES.md +280 -0
  11. package/deps/winpty/VERSION.txt +1 -0
  12. package/deps/winpty/configure +167 -0
  13. package/deps/winpty/misc/BufferResizeTests.cc +90 -0
  14. package/deps/winpty/misc/ChangeScreenBuffer.cc +53 -0
  15. package/deps/winpty/misc/ClearConsole.cc +72 -0
  16. package/deps/winpty/misc/ConinMode.cc +117 -0
  17. package/deps/winpty/misc/ConinMode.ps1 +116 -0
  18. package/deps/winpty/misc/ConoutMode.cc +113 -0
  19. package/deps/winpty/misc/DebugClient.py +42 -0
  20. package/deps/winpty/misc/DebugServer.py +63 -0
  21. package/deps/winpty/misc/DumpLines.py +5 -0
  22. package/deps/winpty/misc/EnableExtendedFlags.txt +46 -0
  23. package/deps/winpty/misc/Font-Report-June2016/CP437-Consolas.txt +528 -0
  24. package/deps/winpty/misc/Font-Report-June2016/CP437-Lucida.txt +633 -0
  25. package/deps/winpty/misc/Font-Report-June2016/CP932.txt +630 -0
  26. package/deps/winpty/misc/Font-Report-June2016/CP936.txt +630 -0
  27. package/deps/winpty/misc/Font-Report-June2016/CP949.txt +630 -0
  28. package/deps/winpty/misc/Font-Report-June2016/CP950.txt +630 -0
  29. package/deps/winpty/misc/Font-Report-June2016/MinimumWindowWidths.txt +16 -0
  30. package/deps/winpty/misc/Font-Report-June2016/Results.txt +4 -0
  31. package/deps/winpty/misc/Font-Report-June2016/Windows10SetFontBugginess.txt +144 -0
  32. package/deps/winpty/misc/FontSurvey.cc +100 -0
  33. package/deps/winpty/misc/FormatChar.h +21 -0
  34. package/deps/winpty/misc/FreezePerfTest.cc +62 -0
  35. package/deps/winpty/misc/GetCh.cc +20 -0
  36. package/deps/winpty/misc/GetConsolePos.cc +41 -0
  37. package/deps/winpty/misc/GetFont.cc +261 -0
  38. package/deps/winpty/misc/IdentifyConsoleWindow.ps1 +51 -0
  39. package/deps/winpty/misc/IsNewConsole.cc +87 -0
  40. package/deps/winpty/misc/MouseInputNotes.txt +90 -0
  41. package/deps/winpty/misc/MoveConsoleWindow.cc +34 -0
  42. package/deps/winpty/misc/Notes.txt +219 -0
  43. package/deps/winpty/misc/OSVersion.cc +27 -0
  44. package/deps/winpty/misc/ScreenBufferFreezeInactive.cc +101 -0
  45. package/deps/winpty/misc/ScreenBufferTest.cc +671 -0
  46. package/deps/winpty/misc/ScreenBufferTest2.cc +151 -0
  47. package/deps/winpty/misc/SelectAllTest.cc +45 -0
  48. package/deps/winpty/misc/SetBufferSize.cc +32 -0
  49. package/deps/winpty/misc/SetCursorPos.cc +10 -0
  50. package/deps/winpty/misc/SetFont.cc +145 -0
  51. package/deps/winpty/misc/SetWindowRect.cc +36 -0
  52. package/deps/winpty/misc/ShowArgv.cc +12 -0
  53. package/deps/winpty/misc/ShowConsoleInput.cc +40 -0
  54. package/deps/winpty/misc/Spew.py +5 -0
  55. package/deps/winpty/misc/TestUtil.cc +172 -0
  56. package/deps/winpty/misc/UnicodeDoubleWidthTest.cc +102 -0
  57. package/deps/winpty/misc/UnicodeWideTest1.cc +246 -0
  58. package/deps/winpty/misc/UnicodeWideTest2.cc +130 -0
  59. package/deps/winpty/misc/UnixEcho.cc +89 -0
  60. package/deps/winpty/misc/Utf16Echo.cc +46 -0
  61. package/deps/winpty/misc/VeryLargeRead.cc +122 -0
  62. package/deps/winpty/misc/VkEscapeTest.cc +56 -0
  63. package/deps/winpty/misc/Win10ResizeWhileFrozen.cc +52 -0
  64. package/deps/winpty/misc/Win10WrapTest1.cc +57 -0
  65. package/deps/winpty/misc/Win10WrapTest2.cc +30 -0
  66. package/deps/winpty/misc/Win32Echo1.cc +26 -0
  67. package/deps/winpty/misc/Win32Echo2.cc +19 -0
  68. package/deps/winpty/misc/Win32Test1.cc +46 -0
  69. package/deps/winpty/misc/Win32Test2.cc +70 -0
  70. package/deps/winpty/misc/Win32Test3.cc +78 -0
  71. package/deps/winpty/misc/Win32Write1.cc +44 -0
  72. package/deps/winpty/misc/WindowsBugCrashReader.cc +27 -0
  73. package/deps/winpty/misc/WriteConsole.cc +106 -0
  74. package/deps/winpty/misc/build32.sh +9 -0
  75. package/deps/winpty/misc/build64.sh +9 -0
  76. package/deps/winpty/misc/color-test.sh +212 -0
  77. package/deps/winpty/misc/font-notes.txt +300 -0
  78. package/deps/winpty/misc/winbug-15048.cc +201 -0
  79. package/deps/winpty/ship/build-pty4j-libpty.bat +36 -0
  80. package/deps/winpty/ship/common_ship.py +53 -0
  81. package/deps/winpty/ship/make_msvc_package.py +165 -0
  82. package/deps/winpty/ship/ship.py +108 -0
  83. package/deps/winpty/src/agent/Agent.cc +613 -0
  84. package/deps/winpty/src/agent/Agent.h +103 -0
  85. package/deps/winpty/src/agent/AgentCreateDesktop.cc +84 -0
  86. package/deps/winpty/src/agent/AgentCreateDesktop.h +28 -0
  87. package/deps/winpty/src/agent/ConsoleFont.cc +632 -0
  88. package/deps/winpty/src/agent/ConsoleFont.h +28 -0
  89. package/deps/winpty/src/agent/ConsoleInput.cc +852 -0
  90. package/deps/winpty/src/agent/ConsoleInput.h +109 -0
  91. package/deps/winpty/src/agent/ConsoleInputReencoding.cc +121 -0
  92. package/deps/winpty/src/agent/ConsoleInputReencoding.h +36 -0
  93. package/deps/winpty/src/agent/ConsoleLine.cc +152 -0
  94. package/deps/winpty/src/agent/ConsoleLine.h +41 -0
  95. package/deps/winpty/src/agent/Coord.h +87 -0
  96. package/deps/winpty/src/agent/DebugShowInput.cc +239 -0
  97. package/deps/winpty/src/agent/DebugShowInput.h +32 -0
  98. package/deps/winpty/src/agent/DefaultInputMap.cc +422 -0
  99. package/deps/winpty/src/agent/DefaultInputMap.h +28 -0
  100. package/deps/winpty/src/agent/DsrSender.h +30 -0
  101. package/deps/winpty/src/agent/EventLoop.cc +99 -0
  102. package/deps/winpty/src/agent/EventLoop.h +47 -0
  103. package/deps/winpty/src/agent/InputMap.cc +246 -0
  104. package/deps/winpty/src/agent/InputMap.h +114 -0
  105. package/deps/winpty/src/agent/LargeConsoleRead.cc +71 -0
  106. package/deps/winpty/src/agent/LargeConsoleRead.h +68 -0
  107. package/deps/winpty/src/agent/NamedPipe.cc +378 -0
  108. package/deps/winpty/src/agent/NamedPipe.h +125 -0
  109. package/deps/winpty/src/agent/Scraper.cc +699 -0
  110. package/deps/winpty/src/agent/Scraper.h +103 -0
  111. package/deps/winpty/src/agent/SimplePool.h +75 -0
  112. package/deps/winpty/src/agent/SmallRect.h +143 -0
  113. package/deps/winpty/src/agent/Terminal.cc +535 -0
  114. package/deps/winpty/src/agent/Terminal.h +69 -0
  115. package/deps/winpty/src/agent/UnicodeEncoding.h +157 -0
  116. package/deps/winpty/src/agent/UnicodeEncodingTest.cc +189 -0
  117. package/deps/winpty/src/agent/Win32Console.cc +107 -0
  118. package/deps/winpty/src/agent/Win32Console.h +67 -0
  119. package/deps/winpty/src/agent/Win32ConsoleBuffer.cc +193 -0
  120. package/deps/winpty/src/agent/Win32ConsoleBuffer.h +99 -0
  121. package/deps/winpty/src/agent/main.cc +114 -0
  122. package/deps/winpty/src/agent/subdir.mk +61 -0
  123. package/deps/winpty/src/configurations.gypi +60 -0
  124. package/deps/winpty/src/debugserver/DebugServer.cc +117 -0
  125. package/deps/winpty/src/debugserver/subdir.mk +41 -0
  126. package/deps/winpty/src/include/winpty.h +242 -0
  127. package/deps/winpty/src/include/winpty_constants.h +131 -0
  128. package/deps/winpty/src/libwinpty/AgentLocation.cc +75 -0
  129. package/deps/winpty/src/libwinpty/AgentLocation.h +28 -0
  130. package/deps/winpty/src/libwinpty/LibWinptyException.h +54 -0
  131. package/deps/winpty/src/libwinpty/WinptyInternal.h +72 -0
  132. package/deps/winpty/src/libwinpty/subdir.mk +46 -0
  133. package/deps/winpty/src/libwinpty/winpty.cc +970 -0
  134. package/deps/winpty/src/shared/AgentMsg.h +38 -0
  135. package/deps/winpty/src/shared/BackgroundDesktop.cc +122 -0
  136. package/deps/winpty/src/shared/BackgroundDesktop.h +73 -0
  137. package/deps/winpty/src/shared/Buffer.cc +103 -0
  138. package/deps/winpty/src/shared/Buffer.h +102 -0
  139. package/deps/winpty/src/shared/DebugClient.cc +187 -0
  140. package/deps/winpty/src/shared/DebugClient.h +38 -0
  141. package/deps/winpty/src/shared/GenRandom.cc +138 -0
  142. package/deps/winpty/src/shared/GenRandom.h +55 -0
  143. package/deps/winpty/src/shared/GetCommitHash.bat +13 -0
  144. package/deps/winpty/src/shared/Mutex.h +54 -0
  145. package/deps/winpty/src/shared/OsModule.h +63 -0
  146. package/deps/winpty/src/shared/OwnedHandle.cc +36 -0
  147. package/deps/winpty/src/shared/OwnedHandle.h +45 -0
  148. package/deps/winpty/src/shared/PrecompiledHeader.h +43 -0
  149. package/deps/winpty/src/shared/StringBuilder.h +227 -0
  150. package/deps/winpty/src/shared/StringBuilderTest.cc +114 -0
  151. package/deps/winpty/src/shared/StringUtil.cc +55 -0
  152. package/deps/winpty/src/shared/StringUtil.h +80 -0
  153. package/deps/winpty/src/shared/TimeMeasurement.h +63 -0
  154. package/deps/winpty/src/shared/UnixCtrlChars.h +45 -0
  155. package/deps/winpty/src/shared/UpdateGenVersion.bat +20 -0
  156. package/deps/winpty/src/shared/WindowsSecurity.cc +460 -0
  157. package/deps/winpty/src/shared/WindowsSecurity.h +104 -0
  158. package/deps/winpty/src/shared/WindowsVersion.cc +252 -0
  159. package/deps/winpty/src/shared/WindowsVersion.h +29 -0
  160. package/deps/winpty/src/shared/WinptyAssert.cc +55 -0
  161. package/deps/winpty/src/shared/WinptyAssert.h +64 -0
  162. package/deps/winpty/src/shared/WinptyException.cc +57 -0
  163. package/deps/winpty/src/shared/WinptyException.h +43 -0
  164. package/deps/winpty/src/shared/WinptyVersion.cc +42 -0
  165. package/deps/winpty/src/shared/WinptyVersion.h +27 -0
  166. package/deps/winpty/src/shared/winpty_snprintf.h +99 -0
  167. package/deps/winpty/src/subdir.mk +5 -0
  168. package/deps/winpty/src/tests/subdir.mk +28 -0
  169. package/deps/winpty/src/tests/trivial_test.cc +158 -0
  170. package/deps/winpty/src/unix-adapter/InputHandler.cc +114 -0
  171. package/deps/winpty/src/unix-adapter/InputHandler.h +56 -0
  172. package/deps/winpty/src/unix-adapter/OutputHandler.cc +80 -0
  173. package/deps/winpty/src/unix-adapter/OutputHandler.h +53 -0
  174. package/deps/winpty/src/unix-adapter/Util.cc +86 -0
  175. package/deps/winpty/src/unix-adapter/Util.h +31 -0
  176. package/deps/winpty/src/unix-adapter/WakeupFd.cc +70 -0
  177. package/deps/winpty/src/unix-adapter/WakeupFd.h +42 -0
  178. package/deps/winpty/src/unix-adapter/main.cc +729 -0
  179. package/deps/winpty/src/unix-adapter/subdir.mk +41 -0
  180. package/deps/winpty/src/winpty.gyp +234 -0
  181. package/deps/winpty/vcbuild.bat +83 -0
  182. package/lib/conpty_console_list_agent.js +16 -0
  183. package/lib/conpty_console_list_agent.js.map +1 -0
  184. package/lib/eventEmitter2.js +47 -0
  185. package/lib/eventEmitter2.js.map +1 -0
  186. package/lib/eventEmitter2.test.js +30 -0
  187. package/lib/eventEmitter2.test.js.map +1 -0
  188. package/lib/index.js +52 -0
  189. package/lib/index.js.map +1 -0
  190. package/lib/interfaces.js +7 -0
  191. package/lib/interfaces.js.map +1 -0
  192. package/lib/shared/conout.js +11 -0
  193. package/lib/shared/conout.js.map +1 -0
  194. package/lib/terminal.js +190 -0
  195. package/lib/terminal.js.map +1 -0
  196. package/lib/terminal.test.js +139 -0
  197. package/lib/terminal.test.js.map +1 -0
  198. package/lib/testUtils.test.js +28 -0
  199. package/lib/testUtils.test.js.map +1 -0
  200. package/lib/types.js +7 -0
  201. package/lib/types.js.map +1 -0
  202. package/lib/unixTerminal.js +346 -0
  203. package/lib/unixTerminal.js.map +1 -0
  204. package/lib/unixTerminal.test.js +351 -0
  205. package/lib/unixTerminal.test.js.map +1 -0
  206. package/lib/utils.js +39 -0
  207. package/lib/utils.js.map +1 -0
  208. package/lib/windowsConoutConnection.js +125 -0
  209. package/lib/windowsConoutConnection.js.map +1 -0
  210. package/lib/windowsPtyAgent.js +320 -0
  211. package/lib/windowsPtyAgent.js.map +1 -0
  212. package/lib/windowsPtyAgent.test.js +90 -0
  213. package/lib/windowsPtyAgent.test.js.map +1 -0
  214. package/lib/windowsTerminal.js +199 -0
  215. package/lib/windowsTerminal.js.map +1 -0
  216. package/lib/windowsTerminal.test.js +219 -0
  217. package/lib/windowsTerminal.test.js.map +1 -0
  218. package/lib/worker/conoutSocketWorker.js +22 -0
  219. package/lib/worker/conoutSocketWorker.js.map +1 -0
  220. package/package.json +68 -0
  221. package/prebuilds/darwin-arm64/pty.node +0 -0
  222. package/prebuilds/darwin-arm64/spawn-helper +0 -0
  223. package/prebuilds/darwin-x64/pty.node +0 -0
  224. package/prebuilds/darwin-x64/spawn-helper +0 -0
  225. package/prebuilds/win32-arm64/conpty/OpenConsole.exe +0 -0
  226. package/prebuilds/win32-arm64/conpty/conpty.dll +0 -0
  227. package/prebuilds/win32-arm64/conpty.node +0 -0
  228. package/prebuilds/win32-arm64/conpty.pdb +0 -0
  229. package/prebuilds/win32-arm64/conpty_console_list.node +0 -0
  230. package/prebuilds/win32-arm64/conpty_console_list.pdb +0 -0
  231. package/prebuilds/win32-arm64/pty.node +0 -0
  232. package/prebuilds/win32-arm64/pty.pdb +0 -0
  233. package/prebuilds/win32-arm64/winpty-agent.exe +0 -0
  234. package/prebuilds/win32-arm64/winpty-agent.pdb +0 -0
  235. package/prebuilds/win32-arm64/winpty.dll +0 -0
  236. package/prebuilds/win32-arm64/winpty.pdb +0 -0
  237. package/prebuilds/win32-x64/conpty/OpenConsole.exe +0 -0
  238. package/prebuilds/win32-x64/conpty/conpty.dll +0 -0
  239. package/prebuilds/win32-x64/conpty.node +0 -0
  240. package/prebuilds/win32-x64/conpty.pdb +0 -0
  241. package/prebuilds/win32-x64/conpty_console_list.node +0 -0
  242. package/prebuilds/win32-x64/conpty_console_list.pdb +0 -0
  243. package/prebuilds/win32-x64/pty.node +0 -0
  244. package/prebuilds/win32-x64/pty.pdb +0 -0
  245. package/prebuilds/win32-x64/winpty-agent.exe +0 -0
  246. package/prebuilds/win32-x64/winpty-agent.pdb +0 -0
  247. package/prebuilds/win32-x64/winpty.dll +0 -0
  248. package/prebuilds/win32-x64/winpty.pdb +0 -0
  249. package/scripts/gen-compile-commands.js +8 -0
  250. package/scripts/increment-version.js +54 -0
  251. package/scripts/post-install.js +99 -0
  252. package/scripts/prebuild.js +39 -0
  253. package/scripts/sync-prebuild.js +31 -0
  254. package/scripts/verify-darwin-fd-leak.js +63 -0
  255. package/src/conpty_console_list_agent.ts +15 -0
  256. package/src/eventEmitter2.test.ts +30 -0
  257. package/src/eventEmitter2.ts +48 -0
  258. package/src/index.ts +52 -0
  259. package/src/interfaces.ts +130 -0
  260. package/src/native.d.ts +54 -0
  261. package/src/shared/conout.ts +15 -0
  262. package/src/terminal.test.ts +119 -0
  263. package/src/terminal.ts +211 -0
  264. package/src/testUtils.test.ts +23 -0
  265. package/src/tsconfig.json +22 -0
  266. package/src/types.ts +15 -0
  267. package/src/unix/pty.cc +808 -0
  268. package/src/unix/spawn-helper.cc +23 -0
  269. package/src/unixTerminal.test.ts +367 -0
  270. package/src/unixTerminal.ts +388 -0
  271. package/src/utils.ts +29 -0
  272. package/src/win/conpty.cc +583 -0
  273. package/src/win/conpty.h +41 -0
  274. package/src/win/conpty_console_list.cc +44 -0
  275. package/src/win/path_util.cc +95 -0
  276. package/src/win/path_util.h +26 -0
  277. package/src/win/winpty.cc +333 -0
  278. package/src/windowsConoutConnection.ts +82 -0
  279. package/src/windowsPtyAgent.test.ts +94 -0
  280. package/src/windowsPtyAgent.ts +321 -0
  281. package/src/windowsTerminal.test.ts +229 -0
  282. package/src/windowsTerminal.ts +203 -0
  283. package/src/worker/conoutSocketWorker.ts +22 -0
  284. package/third_party/conpty/1.23.251008001/win10-arm64/OpenConsole.exe +0 -0
  285. package/third_party/conpty/1.23.251008001/win10-arm64/conpty.dll +0 -0
  286. package/third_party/conpty/1.23.251008001/win10-x64/OpenConsole.exe +0 -0
  287. package/third_party/conpty/1.23.251008001/win10-x64/conpty.dll +0 -0
  288. package/typings/node-pty.d.ts +211 -0
@@ -0,0 +1,699 @@
1
+ // Copyright (c) 2011-2016 Ryan Prichard
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ // of this software and associated documentation files (the "Software"), to
5
+ // deal in the Software without restriction, including without limitation the
6
+ // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ // sell copies of the Software, and to permit persons to whom the Software is
8
+ // furnished to do so, subject to the following conditions:
9
+ //
10
+ // The above copyright notice and this permission notice shall be included in
11
+ // all copies or substantial portions of the Software.
12
+ //
13
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18
+ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19
+ // IN THE SOFTWARE.
20
+
21
+ #include "Scraper.h"
22
+
23
+ #include <windows.h>
24
+
25
+ #include <stdint.h>
26
+
27
+ #include <algorithm>
28
+ #include <utility>
29
+
30
+ #include "../shared/WinptyAssert.h"
31
+ #include "../shared/winpty_snprintf.h"
32
+
33
+ #include "ConsoleFont.h"
34
+ #include "Win32Console.h"
35
+ #include "Win32ConsoleBuffer.h"
36
+
37
+ namespace {
38
+
39
+ template <typename T>
40
+ T constrained(T min, T val, T max) {
41
+ ASSERT(min <= max);
42
+ return std::min(std::max(min, val), max);
43
+ }
44
+
45
+ } // anonymous namespace
46
+
47
+ Scraper::Scraper(
48
+ Win32Console &console,
49
+ Win32ConsoleBuffer &buffer,
50
+ std::unique_ptr<Terminal> terminal,
51
+ Coord initialSize) :
52
+ m_console(console),
53
+ m_terminal(std::move(terminal)),
54
+ m_ptySize(initialSize)
55
+ {
56
+ m_consoleBuffer = &buffer;
57
+
58
+ resetConsoleTracking(Terminal::OmitClear, buffer.windowRect().top());
59
+
60
+ m_bufferData.resize(BUFFER_LINE_COUNT);
61
+
62
+ // Setup the initial screen buffer and window size.
63
+ //
64
+ // Use SetConsoleWindowInfo to shrink the console window as much as
65
+ // possible -- to a 1x1 cell at the top-left. This call always succeeds.
66
+ // Prior to the new Windows 10 console, it also actually resizes the GUI
67
+ // window to 1x1 cell. Nevertheless, even though the GUI window can
68
+ // therefore be narrower than its minimum, calling
69
+ // SetConsoleScreenBufferSize with a 1x1 size still fails.
70
+ //
71
+ // While the small font intends to support large buffers, a user could
72
+ // still hit a limit imposed by their monitor width, so cap the new window
73
+ // size to GetLargestConsoleWindowSize().
74
+ setSmallFont(buffer.conout(), initialSize.X, m_console.isNewW10());
75
+ buffer.moveWindow(SmallRect(0, 0, 1, 1));
76
+ buffer.resizeBufferRange(Coord(initialSize.X, BUFFER_LINE_COUNT));
77
+ const auto largest = GetLargestConsoleWindowSize(buffer.conout());
78
+ buffer.moveWindow(SmallRect(
79
+ 0, 0,
80
+ std::min(initialSize.X, largest.X),
81
+ std::min(initialSize.Y, largest.Y)));
82
+ buffer.setCursorPosition(Coord(0, 0));
83
+
84
+ // For the sake of the color translation heuristic, set the console color
85
+ // to LtGray-on-Black.
86
+ buffer.setTextAttribute(Win32ConsoleBuffer::kDefaultAttributes);
87
+ buffer.clearAllLines(m_consoleBuffer->bufferInfo());
88
+
89
+ m_consoleBuffer = nullptr;
90
+ }
91
+
92
+ Scraper::~Scraper()
93
+ {
94
+ }
95
+
96
+ // Whether or not the agent is frozen on entry, it will be frozen on exit.
97
+ void Scraper::resizeWindow(Win32ConsoleBuffer &buffer,
98
+ Coord newSize,
99
+ ConsoleScreenBufferInfo &finalInfoOut)
100
+ {
101
+ m_consoleBuffer = &buffer;
102
+ m_ptySize = newSize;
103
+ syncConsoleContentAndSize(true, finalInfoOut);
104
+ m_consoleBuffer = nullptr;
105
+ }
106
+
107
+ // This function may freeze the agent, but it will not unfreeze it.
108
+ void Scraper::scrapeBuffer(Win32ConsoleBuffer &buffer,
109
+ ConsoleScreenBufferInfo &finalInfoOut)
110
+ {
111
+ m_consoleBuffer = &buffer;
112
+ syncConsoleContentAndSize(false, finalInfoOut);
113
+ m_consoleBuffer = nullptr;
114
+ }
115
+
116
+ void Scraper::resetConsoleTracking(
117
+ Terminal::SendClearFlag sendClear, int64_t scrapedLineCount)
118
+ {
119
+ for (ConsoleLine &line : m_bufferData) {
120
+ line.reset();
121
+ }
122
+ m_syncRow = -1;
123
+ m_scrapedLineCount = scrapedLineCount;
124
+ m_scrolledCount = 0;
125
+ m_maxBufferedLine = -1;
126
+ m_dirtyWindowTop = -1;
127
+ m_dirtyLineCount = 0;
128
+ m_terminal->reset(sendClear, m_scrapedLineCount);
129
+ }
130
+
131
+ // Detect window movement. If the window moves down (presumably as a
132
+ // result of scrolling), then assume that all screen buffer lines down to
133
+ // the bottom of the window are dirty.
134
+ void Scraper::markEntireWindowDirty(const SmallRect &windowRect)
135
+ {
136
+ m_dirtyLineCount = std::max(m_dirtyLineCount,
137
+ windowRect.top() + windowRect.height());
138
+ }
139
+
140
+ // Scan the screen buffer and advance the dirty line count when we find
141
+ // non-empty lines.
142
+ void Scraper::scanForDirtyLines(const SmallRect &windowRect)
143
+ {
144
+ const int w = m_readBuffer.rect().width();
145
+ ASSERT(m_dirtyLineCount >= 1);
146
+ const CHAR_INFO *const prevLine =
147
+ m_readBuffer.lineData(m_dirtyLineCount - 1);
148
+ WORD prevLineAttr = prevLine[w - 1].Attributes;
149
+ const int stopLine = windowRect.top() + windowRect.height();
150
+
151
+ for (int line = m_dirtyLineCount; line < stopLine; ++line) {
152
+ const CHAR_INFO *lineData = m_readBuffer.lineData(line);
153
+ for (int col = 0; col < w; ++col) {
154
+ const WORD colAttr = lineData[col].Attributes;
155
+ if (lineData[col].Char.UnicodeChar != L' ' ||
156
+ colAttr != prevLineAttr) {
157
+ m_dirtyLineCount = line + 1;
158
+ break;
159
+ }
160
+ }
161
+ prevLineAttr = lineData[w - 1].Attributes;
162
+ }
163
+ }
164
+
165
+ // Clear lines in the line buffer. The `firstRow` parameter is in
166
+ // screen-buffer coordinates.
167
+ void Scraper::clearBufferLines(
168
+ const int firstRow,
169
+ const int count)
170
+ {
171
+ ASSERT(!m_directMode);
172
+ for (int row = firstRow; row < firstRow + count; ++row) {
173
+ const int64_t bufLine = row + m_scrolledCount;
174
+ m_maxBufferedLine = std::max(m_maxBufferedLine, bufLine);
175
+ m_bufferData[bufLine % BUFFER_LINE_COUNT].blank(
176
+ Win32ConsoleBuffer::kDefaultAttributes);
177
+ }
178
+ }
179
+
180
+ static bool cursorInWindow(const ConsoleScreenBufferInfo &info)
181
+ {
182
+ return info.dwCursorPosition.Y >= info.srWindow.Top &&
183
+ info.dwCursorPosition.Y <= info.srWindow.Bottom;
184
+ }
185
+
186
+ void Scraper::resizeImpl(const ConsoleScreenBufferInfo &origInfo)
187
+ {
188
+ ASSERT(m_console.frozen());
189
+ const int cols = m_ptySize.X;
190
+ const int rows = m_ptySize.Y;
191
+ Coord finalBufferSize;
192
+
193
+ {
194
+ //
195
+ // To accommodate Windows 10, erase all lines up to the top of the
196
+ // visible window. It's hard to tell whether this is strictly
197
+ // necessary. It ensures that the sync marker won't move downward,
198
+ // and it ensures that we won't repeat lines that have already scrolled
199
+ // up into the scrollback.
200
+ //
201
+ // It *is* possible for these blank lines to reappear in the visible
202
+ // window (e.g. if the window is made taller), but because we blanked
203
+ // the lines in the line buffer, we still don't output them again.
204
+ //
205
+ const Coord origBufferSize = origInfo.bufferSize();
206
+ const SmallRect origWindowRect = origInfo.windowRect();
207
+
208
+ if (m_directMode) {
209
+ for (ConsoleLine &line : m_bufferData) {
210
+ line.reset();
211
+ }
212
+ } else {
213
+ m_consoleBuffer->clearLines(0, origWindowRect.Top, origInfo);
214
+ clearBufferLines(0, origWindowRect.Top);
215
+ if (m_syncRow != -1) {
216
+ createSyncMarker(std::min(
217
+ m_syncRow,
218
+ BUFFER_LINE_COUNT - rows
219
+ - SYNC_MARKER_LEN
220
+ - SYNC_MARKER_MARGIN));
221
+ }
222
+ }
223
+
224
+ finalBufferSize = Coord(
225
+ cols,
226
+ // If there was previously no scrollback (e.g. a full-screen app
227
+ // in direct mode) and we're reducing the window height, then
228
+ // reduce the console buffer's height too.
229
+ (origWindowRect.height() == origBufferSize.Y)
230
+ ? rows
231
+ : std::max<int>(rows, origBufferSize.Y));
232
+
233
+ // Reset the console font size. We need to do this before shrinking
234
+ // the window, because we might need to make the font bigger to permit
235
+ // a smaller window width. Making the font smaller could expand the
236
+ // screen buffer, which would hang the conhost process in the
237
+ // Windows 10 (10240 build) if the console selection is in progress, so
238
+ // unfreeze it first.
239
+ m_console.setFrozen(false);
240
+ setSmallFont(m_consoleBuffer->conout(), cols, m_console.isNewW10());
241
+ }
242
+
243
+ // We try to make the font small enough so that the entire screen buffer
244
+ // fits on the monitor, but it can't be guaranteed.
245
+ const auto largest =
246
+ GetLargestConsoleWindowSize(m_consoleBuffer->conout());
247
+ const short visibleCols = std::min<short>(cols, largest.X);
248
+ const short visibleRows = std::min<short>(rows, largest.Y);
249
+
250
+ {
251
+ // Make the window small enough. We want the console frozen during
252
+ // this step so we don't accidentally move the window above the cursor.
253
+ m_console.setFrozen(true);
254
+ const auto info = m_consoleBuffer->bufferInfo();
255
+ const auto &bufferSize = info.dwSize;
256
+ const int tmpWindowWidth = std::min(bufferSize.X, visibleCols);
257
+ const int tmpWindowHeight = std::min(bufferSize.Y, visibleRows);
258
+ SmallRect tmpWindowRect(
259
+ 0,
260
+ std::min<int>(bufferSize.Y - tmpWindowHeight,
261
+ info.windowRect().Top),
262
+ tmpWindowWidth,
263
+ tmpWindowHeight);
264
+ if (cursorInWindow(info)) {
265
+ tmpWindowRect = tmpWindowRect.ensureLineIncluded(
266
+ info.cursorPosition().Y);
267
+ }
268
+ m_consoleBuffer->moveWindow(tmpWindowRect);
269
+ }
270
+
271
+ {
272
+ // Resize the buffer to the final desired size.
273
+ m_console.setFrozen(false);
274
+ m_consoleBuffer->resizeBufferRange(finalBufferSize);
275
+ }
276
+
277
+ {
278
+ // Expand the window to its full size.
279
+ m_console.setFrozen(true);
280
+ const ConsoleScreenBufferInfo info = m_consoleBuffer->bufferInfo();
281
+
282
+ SmallRect finalWindowRect(
283
+ 0,
284
+ std::min<int>(info.bufferSize().Y - visibleRows,
285
+ info.windowRect().Top),
286
+ visibleCols,
287
+ visibleRows);
288
+
289
+ //
290
+ // Once a line in the screen buffer is "dirty", it should stay visible
291
+ // in the console window, so that we continue to update its content in
292
+ // the terminal. This code is particularly (only?) necessary on
293
+ // Windows 10, where making the buffer wider can rewrap lines and move
294
+ // the console window upward.
295
+ //
296
+ if (!m_directMode && m_dirtyLineCount > finalWindowRect.Bottom + 1) {
297
+ // In theory, we avoid ensureLineIncluded, because, a massive
298
+ // amount of output could have occurred while the console was
299
+ // unfrozen, so that the *top* of the window is now below the
300
+ // dirtiest tracked line.
301
+ finalWindowRect = SmallRect(
302
+ 0, m_dirtyLineCount - visibleRows,
303
+ visibleCols, visibleRows);
304
+ }
305
+
306
+ // Highest priority constraint: ensure that the cursor remains visible.
307
+ if (cursorInWindow(info)) {
308
+ finalWindowRect = finalWindowRect.ensureLineIncluded(
309
+ info.cursorPosition().Y);
310
+ }
311
+
312
+ m_consoleBuffer->moveWindow(finalWindowRect);
313
+ m_dirtyWindowTop = finalWindowRect.Top;
314
+ }
315
+
316
+ ASSERT(m_console.frozen());
317
+ }
318
+
319
+ void Scraper::syncConsoleContentAndSize(
320
+ bool forceResize,
321
+ ConsoleScreenBufferInfo &finalInfoOut)
322
+ {
323
+ // We'll try to avoid freezing the console by reading large chunks (or
324
+ // all!) of the screen buffer without otherwise attempting to synchronize
325
+ // with the console application. We can only do this on Windows 10 and up
326
+ // because:
327
+ // - Prior to Windows 8, the size of a ReadConsoleOutputW call was limited
328
+ // by the ~32KB RPC buffer.
329
+ // - Prior to Windows 10, an out-of-range read region crashes the caller.
330
+ // (See misc/WindowsBugCrashReader.cc.)
331
+ //
332
+ if (!m_console.isNewW10() || forceResize) {
333
+ m_console.setFrozen(true);
334
+ }
335
+
336
+ const ConsoleScreenBufferInfo info = m_consoleBuffer->bufferInfo();
337
+ bool cursorVisible = true;
338
+ CONSOLE_CURSOR_INFO cursorInfo = {};
339
+ if (!GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo)) {
340
+ trace("GetConsoleCursorInfo failed");
341
+ } else {
342
+ cursorVisible = cursorInfo.bVisible != 0;
343
+ }
344
+
345
+ // If an app resizes the buffer height, then we enter "direct mode", where
346
+ // we stop trying to track incremental console changes.
347
+ const bool newDirectMode = (info.bufferSize().Y != BUFFER_LINE_COUNT);
348
+ if (newDirectMode != m_directMode) {
349
+ trace("Entering %s mode", newDirectMode ? "direct" : "scrolling");
350
+ resetConsoleTracking(Terminal::SendClear,
351
+ newDirectMode ? 0 : info.windowRect().top());
352
+ m_directMode = newDirectMode;
353
+
354
+ // When we switch from direct->scrolling mode, make sure the console is
355
+ // the right size.
356
+ if (!m_directMode) {
357
+ m_console.setFrozen(true);
358
+ forceResize = true;
359
+ }
360
+ }
361
+
362
+ if (m_directMode) {
363
+ // In direct-mode, resizing the console redraws the terminal, so do it
364
+ // before scraping.
365
+ if (forceResize) {
366
+ resizeImpl(info);
367
+ }
368
+ directScrapeOutput(info, cursorVisible);
369
+ } else {
370
+ if (!m_console.frozen()) {
371
+ if (!scrollingScrapeOutput(info, cursorVisible, true)) {
372
+ m_console.setFrozen(true);
373
+ }
374
+ }
375
+ if (m_console.frozen()) {
376
+ scrollingScrapeOutput(info, cursorVisible, false);
377
+ }
378
+ // In scrolling mode, we want to scrape before resizing, because we'll
379
+ // erase everything in the console buffer up to the top of the console
380
+ // window.
381
+ if (forceResize) {
382
+ resizeImpl(info);
383
+ }
384
+ }
385
+
386
+ finalInfoOut = forceResize ? m_consoleBuffer->bufferInfo() : info;
387
+ }
388
+
389
+ // Try to match Windows' behavior w.r.t. to the LVB attribute flags. In some
390
+ // situations, Windows ignores the LVB flags on a character cell because of
391
+ // backwards compatibility -- apparently some programs set the flags without
392
+ // intending to enable reverse-video or underscores.
393
+ //
394
+ // [rprichard 2017-01-15] I haven't actually noticed any old programs that need
395
+ // this treatment -- the motivation for this function comes from the MSDN
396
+ // documentation for SetConsoleMode and ENABLE_LVB_GRID_WORLDWIDE.
397
+ WORD Scraper::attributesMask()
398
+ {
399
+ const auto WINPTY_ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x4u;
400
+ const auto WINPTY_ENABLE_LVB_GRID_WORLDWIDE = 0x10u;
401
+ const auto WINPTY_COMMON_LVB_REVERSE_VIDEO = 0x4000u;
402
+ const auto WINPTY_COMMON_LVB_UNDERSCORE = 0x8000u;
403
+
404
+ const auto cp = GetConsoleOutputCP();
405
+ const auto isCjk = (cp == 932 || cp == 936 || cp == 949 || cp == 950);
406
+
407
+ const DWORD outputMode = [this]{
408
+ ASSERT(this->m_consoleBuffer != nullptr);
409
+ DWORD mode = 0;
410
+ if (!GetConsoleMode(this->m_consoleBuffer->conout(), &mode)) {
411
+ mode = 0;
412
+ }
413
+ return mode;
414
+ }();
415
+ const bool hasEnableLvbGridWorldwide =
416
+ (outputMode & WINPTY_ENABLE_LVB_GRID_WORLDWIDE) != 0;
417
+ const bool hasEnableVtProcessing =
418
+ (outputMode & WINPTY_ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0;
419
+
420
+ // The new Windows 10 console (as of 14393) seems to respect
421
+ // COMMON_LVB_REVERSE_VIDEO even in CP437 w/o the other enabling modes, so
422
+ // try to match that behavior.
423
+ const auto isReverseSupported =
424
+ isCjk || hasEnableLvbGridWorldwide || hasEnableVtProcessing || m_console.isNewW10();
425
+ const auto isUnderscoreSupported =
426
+ isCjk || hasEnableLvbGridWorldwide || hasEnableVtProcessing;
427
+
428
+ WORD mask = ~0;
429
+ if (!isReverseSupported) { mask &= ~WINPTY_COMMON_LVB_REVERSE_VIDEO; }
430
+ if (!isUnderscoreSupported) { mask &= ~WINPTY_COMMON_LVB_UNDERSCORE; }
431
+ return mask;
432
+ }
433
+
434
+ void Scraper::directScrapeOutput(const ConsoleScreenBufferInfo &info,
435
+ bool consoleCursorVisible)
436
+ {
437
+ const SmallRect windowRect = info.windowRect();
438
+
439
+ const SmallRect scrapeRect(
440
+ windowRect.left(), windowRect.top(),
441
+ std::min<SHORT>(std::min(windowRect.width(), m_ptySize.X),
442
+ MAX_CONSOLE_WIDTH),
443
+ std::min<SHORT>(std::min(windowRect.height(), m_ptySize.Y),
444
+ BUFFER_LINE_COUNT));
445
+ const int w = scrapeRect.width();
446
+ const int h = scrapeRect.height();
447
+
448
+ const Coord cursor = info.cursorPosition();
449
+ const bool showTerminalCursor =
450
+ consoleCursorVisible && scrapeRect.contains(cursor);
451
+ const int cursorColumn = !showTerminalCursor ? -1 : cursor.X - scrapeRect.Left;
452
+ const int cursorLine = !showTerminalCursor ? -1 : cursor.Y - scrapeRect.Top;
453
+
454
+ if (!showTerminalCursor) {
455
+ m_terminal->hideTerminalCursor();
456
+ }
457
+
458
+ largeConsoleRead(m_readBuffer, *m_consoleBuffer, scrapeRect, attributesMask());
459
+
460
+ for (int line = 0; line < h; ++line) {
461
+ const CHAR_INFO *const curLine =
462
+ m_readBuffer.lineData(scrapeRect.top() + line);
463
+ ConsoleLine &bufLine = m_bufferData[line];
464
+ if (bufLine.detectChangeAndSetLine(curLine, w)) {
465
+ const int lineCursorColumn =
466
+ line == cursorLine ? cursorColumn : -1;
467
+ m_terminal->sendLine(line, curLine, w, lineCursorColumn);
468
+ }
469
+ }
470
+
471
+ if (showTerminalCursor) {
472
+ m_terminal->showTerminalCursor(cursorColumn, cursorLine);
473
+ }
474
+ }
475
+
476
+ bool Scraper::scrollingScrapeOutput(const ConsoleScreenBufferInfo &info,
477
+ bool consoleCursorVisible,
478
+ bool tentative)
479
+ {
480
+ const Coord cursor = info.cursorPosition();
481
+ const SmallRect windowRect = info.windowRect();
482
+
483
+ if (m_syncRow != -1) {
484
+ // If a synchronizing marker was placed into the history, look for it
485
+ // and adjust the scroll count.
486
+ const int markerRow = findSyncMarker();
487
+ if (markerRow == -1) {
488
+ if (tentative) {
489
+ // I *think* it's possible to keep going, but it's simple to
490
+ // bail out.
491
+ return false;
492
+ }
493
+ // Something has happened. Reset the terminal.
494
+ trace("Sync marker has disappeared -- resetting the terminal"
495
+ " (m_syncCounter=%u)",
496
+ m_syncCounter);
497
+ resetConsoleTracking(Terminal::SendClear, windowRect.top());
498
+ } else if (markerRow != m_syncRow) {
499
+ ASSERT(markerRow < m_syncRow);
500
+ m_scrolledCount += (m_syncRow - markerRow);
501
+ m_syncRow = markerRow;
502
+ // If the buffer has scrolled, then the entire window is dirty.
503
+ markEntireWindowDirty(windowRect);
504
+ }
505
+ }
506
+
507
+ // Creating a new sync row requires clearing part of the console buffer, so
508
+ // avoid doing it if there's already a sync row that's good enough.
509
+ const int newSyncRow =
510
+ static_cast<int>(windowRect.top()) - SYNC_MARKER_LEN - SYNC_MARKER_MARGIN;
511
+ const bool shouldCreateSyncRow =
512
+ newSyncRow >= m_syncRow + SYNC_MARKER_LEN + SYNC_MARKER_MARGIN;
513
+ if (tentative && shouldCreateSyncRow) {
514
+ // It's difficult even in principle to put down a new marker if the
515
+ // console can scroll an arbitrarily amount while we're writing.
516
+ return false;
517
+ }
518
+
519
+ // Update the dirty line count:
520
+ // - If the window has moved, the entire window is dirty.
521
+ // - Everything up to the cursor is dirty.
522
+ // - All lines above the window are dirty.
523
+ // - Any non-blank lines are dirty.
524
+ if (m_dirtyWindowTop != -1) {
525
+ if (windowRect.top() > m_dirtyWindowTop) {
526
+ // The window has moved down, presumably as a result of scrolling.
527
+ markEntireWindowDirty(windowRect);
528
+ } else if (windowRect.top() < m_dirtyWindowTop) {
529
+ if (tentative) {
530
+ // I *think* it's possible to keep going, but it's simple to
531
+ // bail out.
532
+ return false;
533
+ }
534
+ // The window has moved upward. This is generally not expected to
535
+ // happen, but the CMD/PowerShell CLS command will move the window
536
+ // to the top as part of clearing everything else in the console.
537
+ trace("Window moved upward -- resetting the terminal"
538
+ " (m_syncCounter=%u)",
539
+ m_syncCounter);
540
+ resetConsoleTracking(Terminal::SendClear, windowRect.top());
541
+ }
542
+ }
543
+ m_dirtyWindowTop = windowRect.top();
544
+ m_dirtyLineCount = std::max(m_dirtyLineCount, cursor.Y + 1);
545
+ m_dirtyLineCount = std::max(m_dirtyLineCount, (int)windowRect.top());
546
+
547
+ // There will be at least one dirty line, because there is a cursor.
548
+ ASSERT(m_dirtyLineCount >= 1);
549
+
550
+ // The first line to scrape, in virtual line coordinates.
551
+ const int64_t firstVirtLine = std::min(m_scrapedLineCount,
552
+ windowRect.top() + m_scrolledCount);
553
+
554
+ // Read all the data we will need from the console. Start reading with the
555
+ // first line to scrape, but adjust the the read area upward to account for
556
+ // scanForDirtyLines' need to read the previous attribute. Read to the
557
+ // bottom of the window. (It's not clear to me whether the
558
+ // m_dirtyLineCount adjustment here is strictly necessary. It isn't
559
+ // necessary so long as the cursor is inside the current window.)
560
+ const int firstReadLine = std::min<int>(firstVirtLine - m_scrolledCount,
561
+ m_dirtyLineCount - 1);
562
+ const int stopReadLine = std::max(windowRect.top() + windowRect.height(),
563
+ m_dirtyLineCount);
564
+ ASSERT(firstReadLine >= 0 && stopReadLine > firstReadLine);
565
+ largeConsoleRead(m_readBuffer,
566
+ *m_consoleBuffer,
567
+ SmallRect(0, firstReadLine,
568
+ std::min<SHORT>(info.bufferSize().X,
569
+ MAX_CONSOLE_WIDTH),
570
+ stopReadLine - firstReadLine),
571
+ attributesMask());
572
+
573
+ // If we're scraping the buffer without freezing it, we have to query the
574
+ // buffer position data separately from the buffer content, so the two
575
+ // could easily be out-of-sync. If they *are* out-of-sync, abort the
576
+ // scrape operation and restart it frozen. (We may have updated the
577
+ // dirty-line high-water-mark, but that should be OK.)
578
+ if (tentative) {
579
+ const auto infoCheck = m_consoleBuffer->bufferInfo();
580
+ if (info.bufferSize() != infoCheck.bufferSize() ||
581
+ info.windowRect() != infoCheck.windowRect() ||
582
+ info.cursorPosition() != infoCheck.cursorPosition()) {
583
+ return false;
584
+ }
585
+ if (m_syncRow != -1 && m_syncRow != findSyncMarker()) {
586
+ return false;
587
+ }
588
+ }
589
+
590
+ if (shouldCreateSyncRow) {
591
+ ASSERT(!tentative);
592
+ createSyncMarker(newSyncRow);
593
+ }
594
+
595
+ // At this point, we're finished interacting (reading or writing) the
596
+ // console, and we just need to convert our collected data into terminal
597
+ // output.
598
+
599
+ scanForDirtyLines(windowRect);
600
+
601
+ // Note that it's possible for all the lines on the current window to
602
+ // be non-dirty.
603
+
604
+ // The line to stop scraping at, in virtual line coordinates.
605
+ const int64_t stopVirtLine =
606
+ std::min(m_dirtyLineCount, windowRect.top() + windowRect.height()) +
607
+ m_scrolledCount;
608
+
609
+ const bool showTerminalCursor =
610
+ consoleCursorVisible && windowRect.contains(cursor);
611
+ const int64_t cursorLine = !showTerminalCursor ? -1 : cursor.Y + m_scrolledCount;
612
+ const int cursorColumn = !showTerminalCursor ? -1 : cursor.X;
613
+
614
+ if (!showTerminalCursor) {
615
+ m_terminal->hideTerminalCursor();
616
+ }
617
+
618
+ bool sawModifiedLine = false;
619
+
620
+ const int w = m_readBuffer.rect().width();
621
+ for (int64_t line = firstVirtLine; line < stopVirtLine; ++line) {
622
+ const CHAR_INFO *curLine =
623
+ m_readBuffer.lineData(line - m_scrolledCount);
624
+ ConsoleLine &bufLine = m_bufferData[line % BUFFER_LINE_COUNT];
625
+ if (line > m_maxBufferedLine) {
626
+ m_maxBufferedLine = line;
627
+ sawModifiedLine = true;
628
+ }
629
+ if (sawModifiedLine) {
630
+ bufLine.setLine(curLine, w);
631
+ } else {
632
+ sawModifiedLine = bufLine.detectChangeAndSetLine(curLine, w);
633
+ }
634
+ if (sawModifiedLine) {
635
+ const int lineCursorColumn =
636
+ line == cursorLine ? cursorColumn : -1;
637
+ m_terminal->sendLine(line, curLine, w, lineCursorColumn);
638
+ }
639
+ }
640
+
641
+ m_scrapedLineCount = windowRect.top() + m_scrolledCount;
642
+
643
+ if (showTerminalCursor) {
644
+ m_terminal->showTerminalCursor(cursorColumn, cursorLine);
645
+ }
646
+
647
+ return true;
648
+ }
649
+
650
+ void Scraper::syncMarkerText(CHAR_INFO (&output)[SYNC_MARKER_LEN])
651
+ {
652
+ // XXX: The marker text generated here could easily collide with ordinary
653
+ // console output. Does it make sense to try to avoid the collision?
654
+ char str[SYNC_MARKER_LEN + 1];
655
+ winpty_snprintf(str, "S*Y*N*C*%08x", m_syncCounter);
656
+ for (int i = 0; i < SYNC_MARKER_LEN; ++i) {
657
+ output[i].Char.UnicodeChar = str[i];
658
+ output[i].Attributes = 7;
659
+ }
660
+ }
661
+
662
+ int Scraper::findSyncMarker()
663
+ {
664
+ ASSERT(m_syncRow >= 0);
665
+ CHAR_INFO marker[SYNC_MARKER_LEN];
666
+ CHAR_INFO column[BUFFER_LINE_COUNT];
667
+ syncMarkerText(marker);
668
+ SmallRect rect(0, 0, 1, m_syncRow + SYNC_MARKER_LEN);
669
+ m_consoleBuffer->read(rect, column);
670
+ int i;
671
+ for (i = m_syncRow; i >= 0; --i) {
672
+ int j;
673
+ for (j = 0; j < SYNC_MARKER_LEN; ++j) {
674
+ if (column[i + j].Char.UnicodeChar != marker[j].Char.UnicodeChar)
675
+ break;
676
+ }
677
+ if (j == SYNC_MARKER_LEN)
678
+ return i;
679
+ }
680
+ return -1;
681
+ }
682
+
683
+ void Scraper::createSyncMarker(int row)
684
+ {
685
+ ASSERT(row >= 1);
686
+
687
+ // Clear the lines around the marker to ensure that Windows 10's rewrapping
688
+ // does not affect the marker.
689
+ m_consoleBuffer->clearLines(row - 1, SYNC_MARKER_LEN + 1,
690
+ m_consoleBuffer->bufferInfo());
691
+
692
+ // Write a new marker.
693
+ m_syncCounter++;
694
+ CHAR_INFO marker[SYNC_MARKER_LEN];
695
+ syncMarkerText(marker);
696
+ m_syncRow = row;
697
+ SmallRect markerRect(0, m_syncRow, 1, SYNC_MARKER_LEN);
698
+ m_consoleBuffer->write(markerRect, marker);
699
+ }