HexView.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. //
  2. // HexView.cs: A hexadecimal viewer
  3. //
  4. // TODO:
  5. // - Support searching and highlighting of the search result
  6. // - Bug showing the last line
  7. //
  8. namespace Terminal.Gui;
  9. /// <summary>An hex viewer and editor <see cref="View"/> over a <see cref="System.IO.Stream"/></summary>
  10. /// <remarks>
  11. /// <para>
  12. /// <see cref="HexView"/> provides a hex editor on top of a seekable <see cref="Stream"/> with the left side
  13. /// showing an hex dump of the values in the <see cref="Stream"/> and the right side showing the contents (filtered
  14. /// to non-control sequence ASCII characters).
  15. /// </para>
  16. /// <para>Users can switch from one side to the other by using the tab key.</para>
  17. /// <para>
  18. /// To enable editing, set <see cref="AllowEdits"/> to true. When <see cref="AllowEdits"/> is true the user can
  19. /// make changes to the hexadecimal values of the <see cref="Stream"/>. Any changes are tracked in the
  20. /// <see cref="Edits"/> property (a <see cref="SortedDictionary{TKey, TValue}"/>) indicating the position where the
  21. /// changes were made and the new values. A convenience method, <see cref="ApplyEdits"/> will apply the edits to
  22. /// the <see cref="Stream"/>.
  23. /// </para>
  24. /// <para>Control the first byte shown by setting the <see cref="DisplayStart"/> property to an offset in the stream.</para>
  25. /// </remarks>
  26. public class HexView : View
  27. {
  28. private const int bsize = 4;
  29. private const int displayWidth = 9;
  30. private int bpl;
  31. private long displayStart, pos;
  32. private SortedDictionary<long, byte> edits = [];
  33. private bool firstNibble, leftSide;
  34. private Stream source;
  35. private static readonly Rune SpaceCharRune = new (' ');
  36. private static readonly Rune PeriodCharRune = new ('.');
  37. /// <summary>Initializes a <see cref="HexView"/> class.</summary>
  38. /// <param name="source">
  39. /// The <see cref="Stream"/> to view and edit as hex, this <see cref="Stream"/> must support seeking,
  40. /// or an exception will be thrown.
  41. /// </param>
  42. public HexView (Stream source)
  43. {
  44. Source = source;
  45. // BUG: This will always call the most-derived definition of CanFocus.
  46. // Either seal it or don't set it here.
  47. CanFocus = true;
  48. CursorVisibility = CursorVisibility.Default;
  49. leftSide = true;
  50. firstNibble = true;
  51. // PERF: Closure capture of 'this' creates a lot of overhead.
  52. // BUG: Closure capture of 'this' may have unexpected results depending on how this is called.
  53. // The above two comments apply to all of the lambdas passed to all calls to AddCommand below.
  54. // Things this view knows how to do
  55. AddCommand (Command.Left, () => MoveLeft ());
  56. AddCommand (Command.Right, () => MoveRight ());
  57. AddCommand (Command.LineDown, () => MoveDown (bytesPerLine));
  58. AddCommand (Command.LineUp, () => MoveUp (bytesPerLine));
  59. AddCommand (Command.Accept, () => ToggleSide ());
  60. AddCommand (Command.PageUp, () => MoveUp (bytesPerLine * Frame.Height));
  61. AddCommand (Command.PageDown, () => MoveDown (bytesPerLine * Frame.Height));
  62. AddCommand (Command.TopHome, () => MoveHome ());
  63. AddCommand (Command.BottomEnd, () => MoveEnd ());
  64. AddCommand (Command.StartOfLine, () => MoveStartOfLine ());
  65. AddCommand (Command.EndOfLine, () => MoveEndOfLine ());
  66. AddCommand (Command.StartOfPage, () => MoveUp (bytesPerLine * ((int)(position - displayStart) / bytesPerLine)));
  67. AddCommand (
  68. Command.EndOfPage,
  69. () => MoveDown (bytesPerLine * (Frame.Height - 1 - (int)(position - displayStart) / bytesPerLine))
  70. );
  71. // Default keybindings for this view
  72. KeyBindings.Add (Key.CursorLeft, Command.Left);
  73. KeyBindings.Add (Key.CursorRight, Command.Right);
  74. KeyBindings.Add (Key.CursorDown, Command.LineDown);
  75. KeyBindings.Add (Key.CursorUp, Command.LineUp);
  76. KeyBindings.Add (Key.Enter, Command.Accept);
  77. KeyBindings.Add (Key.V.WithAlt, Command.PageUp);
  78. KeyBindings.Add (Key.PageUp, Command.PageUp);
  79. KeyBindings.Add (Key.V.WithCtrl, Command.PageDown);
  80. KeyBindings.Add (Key.PageDown, Command.PageDown);
  81. KeyBindings.Add (Key.Home, Command.TopHome);
  82. KeyBindings.Add (Key.End, Command.BottomEnd);
  83. KeyBindings.Add (Key.CursorLeft.WithCtrl, Command.StartOfLine);
  84. KeyBindings.Add (Key.CursorRight.WithCtrl, Command.EndOfLine);
  85. KeyBindings.Add (Key.CursorUp.WithCtrl, Command.StartOfPage);
  86. KeyBindings.Add (Key.CursorDown.WithCtrl, Command.EndOfPage);
  87. LayoutComplete += HexView_LayoutComplete;
  88. }
  89. /// <summary>Initializes a <see cref="HexView"/> class.</summary>
  90. public HexView () : this (new MemoryStream ()) { }
  91. /// <summary>
  92. /// Gets or sets whether this <see cref="HexView"/> allow editing of the <see cref="Stream"/> of the underlying
  93. /// <see cref="Stream"/>.
  94. /// </summary>
  95. /// <value><c>true</c> if allow edits; otherwise, <c>false</c>.</value>
  96. public bool AllowEdits { get; set; } = true;
  97. /// <summary>The bytes length per line.</summary>
  98. public int BytesPerLine => bytesPerLine;
  99. /// <summary>Gets the current cursor position starting at one for both, line and column.</summary>
  100. public Point CursorPosition
  101. {
  102. get
  103. {
  104. if (!IsInitialized)
  105. {
  106. return Point.Empty;
  107. }
  108. var delta = (int)position;
  109. int line = delta / bytesPerLine + 1;
  110. int item = delta % bytesPerLine + 1;
  111. return new Point (item, line);
  112. }
  113. }
  114. /// <summary>
  115. /// Sets or gets the offset into the <see cref="Stream"/> that will be displayed at the top of the
  116. /// <see cref="HexView"/>
  117. /// </summary>
  118. /// <value>The display start.</value>
  119. public long DisplayStart
  120. {
  121. get => displayStart;
  122. set
  123. {
  124. position = value;
  125. SetDisplayStart (value);
  126. }
  127. }
  128. /// <summary>
  129. /// Gets a <see cref="SortedDictionary{TKey, TValue}"/> describing the edits done to the <see cref="HexView"/>.
  130. /// Each Key indicates an offset where an edit was made and the Value is the changed byte.
  131. /// </summary>
  132. /// <value>The edits.</value>
  133. public IReadOnlyDictionary<long, byte> Edits => edits;
  134. /// <summary>Gets the current character position starting at one, related to the <see cref="Stream"/>.</summary>
  135. public long Position => position + 1;
  136. /// <summary>
  137. /// Sets or gets the <see cref="Stream"/> the <see cref="HexView"/> is operating on; the stream must support
  138. /// seeking ( <see cref="Stream.CanSeek"/> == true).
  139. /// </summary>
  140. /// <value>The source.</value>
  141. public Stream Source
  142. {
  143. get => source;
  144. set
  145. {
  146. if (value is null)
  147. {
  148. throw new ArgumentNullException ("source");
  149. }
  150. if (!value.CanSeek)
  151. {
  152. throw new ArgumentException ("The source stream must be seekable (CanSeek property)", "source");
  153. }
  154. source = value;
  155. if (displayStart > source.Length)
  156. {
  157. DisplayStart = 0;
  158. }
  159. if (position > source.Length)
  160. {
  161. position = 0;
  162. }
  163. SetNeedsDisplay ();
  164. }
  165. }
  166. private int bytesPerLine
  167. {
  168. get => bpl;
  169. set
  170. {
  171. bpl = value;
  172. OnPositionChanged ();
  173. }
  174. }
  175. private long position
  176. {
  177. get => pos;
  178. set
  179. {
  180. pos = value;
  181. OnPositionChanged ();
  182. }
  183. }
  184. /// <summary>
  185. /// This method applies and edits made to the <see cref="Stream"/> and resets the contents of the
  186. /// <see cref="Edits"/> property.
  187. /// </summary>
  188. /// <param name="stream">If provided also applies the changes to the passed <see cref="Stream"/></param>
  189. /// .
  190. public void ApplyEdits (Stream stream = null)
  191. {
  192. foreach (KeyValuePair<long, byte> kv in edits)
  193. {
  194. source.Position = kv.Key;
  195. source.WriteByte (kv.Value);
  196. source.Flush ();
  197. if (stream is { })
  198. {
  199. stream.Position = kv.Key;
  200. stream.WriteByte (kv.Value);
  201. stream.Flush ();
  202. }
  203. }
  204. edits = new SortedDictionary<long, byte> ();
  205. SetNeedsDisplay ();
  206. }
  207. /// <summary>
  208. /// This method discards the edits made to the <see cref="Stream"/> by resetting the contents of the
  209. /// <see cref="Edits"/> property.
  210. /// </summary>
  211. public void DiscardEdits () { edits = new SortedDictionary<long, byte> (); }
  212. /// <summary>Event to be invoked when an edit is made on the <see cref="Stream"/>.</summary>
  213. public event EventHandler<HexViewEditEventArgs> Edited;
  214. /// <inheritdoc/>
  215. protected internal override bool OnMouseEvent (MouseEvent me)
  216. {
  217. if (!me.Flags.HasFlag (MouseFlags.Button1Clicked)
  218. && !me.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
  219. && !me.Flags.HasFlag (MouseFlags.WheeledDown)
  220. && !me.Flags.HasFlag (MouseFlags.WheeledUp))
  221. {
  222. return false;
  223. }
  224. if (!HasFocus)
  225. {
  226. SetFocus ();
  227. }
  228. if (me.Flags == MouseFlags.WheeledDown)
  229. {
  230. DisplayStart = Math.Min (DisplayStart + bytesPerLine, source.Length);
  231. return true;
  232. }
  233. if (me.Flags == MouseFlags.WheeledUp)
  234. {
  235. DisplayStart = Math.Max (DisplayStart - bytesPerLine, 0);
  236. return true;
  237. }
  238. if (me.Position.X < displayWidth)
  239. {
  240. return true;
  241. }
  242. int nblocks = bytesPerLine / bsize;
  243. int blocksSize = nblocks * 14;
  244. int blocksRightOffset = displayWidth + blocksSize - 1;
  245. if (me.Position.X > blocksRightOffset + bytesPerLine - 1)
  246. {
  247. return true;
  248. }
  249. leftSide = me.Position.X >= blocksRightOffset;
  250. long lineStart = me.Position.Y * bytesPerLine + displayStart;
  251. int x = me.Position.X - displayWidth + 1;
  252. int block = x / 14;
  253. x -= block * 2;
  254. int empty = x % 3;
  255. int item = x / 3;
  256. if (!leftSide && item > 0 && (empty == 0 || x == block * 14 + 14 - 1 - block * 2))
  257. {
  258. return true;
  259. }
  260. firstNibble = true;
  261. if (leftSide)
  262. {
  263. position = Math.Min (lineStart + me.Position.X - blocksRightOffset, source.Length);
  264. }
  265. else
  266. {
  267. position = Math.Min (lineStart + item, source.Length);
  268. }
  269. if (me.Flags == MouseFlags.Button1DoubleClicked)
  270. {
  271. leftSide = !leftSide;
  272. if (leftSide)
  273. {
  274. firstNibble = empty == 1;
  275. }
  276. else
  277. {
  278. firstNibble = true;
  279. }
  280. }
  281. SetNeedsDisplay ();
  282. return true;
  283. }
  284. ///<inheritdoc/>
  285. public override void OnDrawContent (Rectangle viewport)
  286. {
  287. Attribute currentAttribute;
  288. Attribute current = ColorScheme.Focus;
  289. Driver.SetAttribute (current);
  290. Move (0, 0);
  291. int nblocks = bytesPerLine / bsize;
  292. var data = new byte [nblocks * bsize * viewport.Height];
  293. Source.Position = displayStart;
  294. int n = source.Read (data, 0, data.Length);
  295. Attribute activeColor = ColorScheme.HotNormal;
  296. Attribute trackingColor = ColorScheme.HotFocus;
  297. for (var line = 0; line < viewport.Height; line++)
  298. {
  299. Rectangle lineRect = new (0, line, viewport.Width, 1);
  300. if (!Viewport.Contains (lineRect))
  301. {
  302. continue;
  303. }
  304. Move (0, line);
  305. Driver.SetAttribute (ColorScheme.HotNormal);
  306. Driver.AddStr ($"{displayStart + line * nblocks * bsize:x8} ");
  307. currentAttribute = ColorScheme.HotNormal;
  308. SetAttribute (GetNormalColor ());
  309. for (var block = 0; block < nblocks; block++)
  310. {
  311. for (var b = 0; b < bsize; b++)
  312. {
  313. int offset = line * nblocks * bsize + block * bsize + b;
  314. byte value = GetData (data, offset, out bool edited);
  315. if (offset + displayStart == position || edited)
  316. {
  317. SetAttribute (leftSide ? activeColor : trackingColor);
  318. }
  319. else
  320. {
  321. SetAttribute (GetNormalColor ());
  322. }
  323. Driver.AddStr (offset >= n && !edited ? " " : $"{value:x2}");
  324. SetAttribute (GetNormalColor ());
  325. Driver.AddRune (SpaceCharRune);
  326. }
  327. Driver.AddStr (block + 1 == nblocks ? " " : "| ");
  328. }
  329. for (var bitem = 0; bitem < nblocks * bsize; bitem++)
  330. {
  331. int offset = line * nblocks * bsize + bitem;
  332. byte b = GetData (data, offset, out bool edited);
  333. Rune c;
  334. if (offset >= n && !edited)
  335. {
  336. c = SpaceCharRune;
  337. }
  338. else
  339. {
  340. if (b < 32)
  341. {
  342. c = PeriodCharRune;
  343. }
  344. else if (b > 127)
  345. {
  346. c = PeriodCharRune;
  347. }
  348. else
  349. {
  350. Rune.DecodeFromUtf8 (new ReadOnlySpan<byte> (ref b), out c, out _);
  351. }
  352. }
  353. if (offset + displayStart == position || edited)
  354. {
  355. SetAttribute (leftSide ? trackingColor : activeColor);
  356. }
  357. else
  358. {
  359. SetAttribute (GetNormalColor ());
  360. }
  361. Driver.AddRune (c);
  362. }
  363. }
  364. void SetAttribute (Attribute attribute)
  365. {
  366. if (currentAttribute != attribute)
  367. {
  368. currentAttribute = attribute;
  369. Driver.SetAttribute (attribute);
  370. }
  371. }
  372. }
  373. /// <summary>Method used to invoke the <see cref="Edited"/> event passing the <see cref="KeyValuePair{TKey, TValue}"/>.</summary>
  374. /// <param name="e">The key value pair.</param>
  375. public virtual void OnEdited (HexViewEditEventArgs e) { Edited?.Invoke (this, e); }
  376. /// <summary>
  377. /// Method used to invoke the <see cref="PositionChanged"/> event passing the <see cref="HexViewEventArgs"/>
  378. /// arguments.
  379. /// </summary>
  380. public virtual void OnPositionChanged () { PositionChanged?.Invoke (this, new HexViewEventArgs (Position, CursorPosition, BytesPerLine)); }
  381. /// <inheritdoc/>
  382. public override bool OnProcessKeyDown (Key keyEvent)
  383. {
  384. if (!AllowEdits)
  385. {
  386. return false;
  387. }
  388. // Ignore control characters and other special keys
  389. if (keyEvent < Key.Space || keyEvent.KeyCode > KeyCode.CharMask)
  390. {
  391. return false;
  392. }
  393. if (leftSide)
  394. {
  395. int value;
  396. var k = (char)keyEvent.KeyCode;
  397. if (k >= 'A' && k <= 'F')
  398. {
  399. value = k - 'A' + 10;
  400. }
  401. else if (k >= 'a' && k <= 'f')
  402. {
  403. value = k - 'a' + 10;
  404. }
  405. else if (k >= '0' && k <= '9')
  406. {
  407. value = k - '0';
  408. }
  409. else
  410. {
  411. return false;
  412. }
  413. byte b;
  414. if (!edits.TryGetValue (position, out b))
  415. {
  416. source.Position = position;
  417. b = (byte)source.ReadByte ();
  418. }
  419. RedisplayLine (position);
  420. if (firstNibble)
  421. {
  422. firstNibble = false;
  423. b = (byte)((b & 0xf) | (value << bsize));
  424. edits [position] = b;
  425. OnEdited (new HexViewEditEventArgs (position, edits [position]));
  426. }
  427. else
  428. {
  429. b = (byte)((b & 0xf0) | value);
  430. edits [position] = b;
  431. OnEdited (new HexViewEditEventArgs (position, edits [position]));
  432. MoveRight ();
  433. }
  434. return true;
  435. }
  436. return false;
  437. }
  438. /// <summary>Event to be invoked when the position and cursor position changes.</summary>
  439. public event EventHandler<HexViewEventArgs> PositionChanged;
  440. ///<inheritdoc/>
  441. public override Point? PositionCursor ()
  442. {
  443. var delta = (int)(position - displayStart);
  444. int line = delta / bytesPerLine;
  445. int item = delta % bytesPerLine;
  446. int block = item / bsize;
  447. int column = item % bsize * 3;
  448. int x = displayWidth + block * 14 + column + (firstNibble ? 0 : 1);
  449. int y = line;
  450. if (!leftSide)
  451. {
  452. x = displayWidth + bytesPerLine / bsize * 14 + item - 1;
  453. }
  454. Move (x, y);
  455. return new (x, y);
  456. }
  457. internal void SetDisplayStart (long value)
  458. {
  459. if (value > 0 && value >= source.Length)
  460. {
  461. displayStart = source.Length - 1;
  462. }
  463. else if (value < 0)
  464. {
  465. displayStart = 0;
  466. }
  467. else
  468. {
  469. displayStart = value;
  470. }
  471. SetNeedsDisplay ();
  472. }
  473. //
  474. // This is used to support editing of the buffer on a peer List<>,
  475. // the offset corresponds to an offset relative to DisplayStart, and
  476. // the buffer contains the contents of a screenful of data, so the
  477. // offset is relative to the buffer.
  478. //
  479. //
  480. private byte GetData (byte [] buffer, int offset, out bool edited)
  481. {
  482. long pos = DisplayStart + offset;
  483. if (edits.TryGetValue (pos, out byte v))
  484. {
  485. edited = true;
  486. return v;
  487. }
  488. edited = false;
  489. return buffer [offset];
  490. }
  491. private void HexView_LayoutComplete (object sender, LayoutEventArgs e)
  492. {
  493. // Small buffers will just show the position, with the bsize field value (4 bytes)
  494. bytesPerLine = bsize;
  495. if (Viewport.Width - displayWidth > 17)
  496. {
  497. bytesPerLine = bsize * ((Viewport.Width - displayWidth) / 18);
  498. }
  499. }
  500. private bool MoveDown (int bytes)
  501. {
  502. RedisplayLine (position);
  503. if (position + bytes < source.Length)
  504. {
  505. position += bytes;
  506. }
  507. else if ((bytes == bytesPerLine * Viewport.Height && source.Length >= DisplayStart + bytesPerLine * Viewport.Height)
  508. || (bytes <= bytesPerLine * Viewport.Height - bytesPerLine
  509. && source.Length <= DisplayStart + bytesPerLine * Viewport.Height))
  510. {
  511. long p = position;
  512. while (p + bytesPerLine < source.Length)
  513. {
  514. p += bytesPerLine;
  515. }
  516. position = p;
  517. }
  518. if (position >= DisplayStart + bytesPerLine * Viewport.Height)
  519. {
  520. SetDisplayStart (DisplayStart + bytes);
  521. SetNeedsDisplay ();
  522. }
  523. else
  524. {
  525. RedisplayLine (position);
  526. }
  527. return true;
  528. }
  529. private bool MoveEnd ()
  530. {
  531. position = source.Length;
  532. if (position >= DisplayStart + bytesPerLine * Viewport.Height)
  533. {
  534. SetDisplayStart (position);
  535. SetNeedsDisplay ();
  536. }
  537. else
  538. {
  539. RedisplayLine (position);
  540. }
  541. return true;
  542. }
  543. private bool MoveEndOfLine ()
  544. {
  545. position = Math.Min (position / bytesPerLine * bytesPerLine + bytesPerLine - 1, source.Length);
  546. SetNeedsDisplay ();
  547. return true;
  548. }
  549. private bool MoveHome ()
  550. {
  551. DisplayStart = 0;
  552. SetNeedsDisplay ();
  553. return true;
  554. }
  555. private bool MoveLeft ()
  556. {
  557. RedisplayLine (position);
  558. if (leftSide)
  559. {
  560. if (!firstNibble)
  561. {
  562. firstNibble = true;
  563. return true;
  564. }
  565. firstNibble = false;
  566. }
  567. if (position == 0)
  568. {
  569. return true;
  570. }
  571. if (position - 1 < DisplayStart)
  572. {
  573. SetDisplayStart (displayStart - bytesPerLine);
  574. SetNeedsDisplay ();
  575. }
  576. else
  577. {
  578. RedisplayLine (position);
  579. }
  580. position--;
  581. return true;
  582. }
  583. private bool MoveRight ()
  584. {
  585. RedisplayLine (position);
  586. if (leftSide)
  587. {
  588. if (firstNibble)
  589. {
  590. firstNibble = false;
  591. return true;
  592. }
  593. firstNibble = true;
  594. }
  595. if (position < source.Length)
  596. {
  597. position++;
  598. }
  599. if (position >= DisplayStart + bytesPerLine * Viewport.Height)
  600. {
  601. SetDisplayStart (DisplayStart + bytesPerLine);
  602. SetNeedsDisplay ();
  603. }
  604. else
  605. {
  606. RedisplayLine (position);
  607. }
  608. return true;
  609. }
  610. private bool MoveStartOfLine ()
  611. {
  612. position = position / bytesPerLine * bytesPerLine;
  613. SetNeedsDisplay ();
  614. return true;
  615. }
  616. private bool MoveUp (int bytes)
  617. {
  618. RedisplayLine (position);
  619. if (position - bytes > -1)
  620. {
  621. position -= bytes;
  622. }
  623. if (position < DisplayStart)
  624. {
  625. SetDisplayStart (DisplayStart - bytes);
  626. SetNeedsDisplay ();
  627. }
  628. else
  629. {
  630. RedisplayLine (position);
  631. }
  632. return true;
  633. }
  634. private void RedisplayLine (long pos)
  635. {
  636. var delta = (int)(pos - DisplayStart);
  637. int line = delta / bytesPerLine;
  638. SetNeedsDisplay (new (0, line, Viewport.Width, 1));
  639. }
  640. private bool ToggleSide ()
  641. {
  642. leftSide = !leftSide;
  643. RedisplayLine (position);
  644. firstNibble = true;
  645. return true;
  646. }
  647. }