1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#![deny(missing_docs, missing_copy_implementations, trivial_casts, trivial_numeric_casts, unsafe_code,
        unused_import_braces, unused_qualifications)]

//! A simple `HexView` for [cursive](https://crates.io/crates/cursive).
//!
//! It is meant to display a data of u8 and format them like e.g. hexdump does.
//! You can interact with the view with your keyboard.
//! Currently the following keys are implemented:
//!
//! | Key                                 | Action                                                                                                                                                                                                                                                 |
//! |-------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
//! | <kbd>&leftarrow;</kbd>              | Move the cursor to the next, left nibble. If already on the left edge of the view, the event will be "ignored", which means the outer view will handle the event (e.g. focus the view next to this one)                                                |
//! | <kbd>&rightarrow;</kbd>             | Move the cursor to the next, right nibble. If already on the right edge of the view, the event will be "ignored", which means the outer view will handle the event (e.g. focus the view next to this one)                                              |
//! | <kbd>&uparrow;</kbd>                | Move the cursor to the previous line. If already on the top edge of the view, the event will be "ignored", which means the outer view will handle the event (e.g. focus the view next to this one)                                                     |
//! | <kbd>&downarrow;</kbd>              | Move the cursor to the next line. If already on the bottom edge of the,view, the event will be "ignored", which means the outer view will handle the event (e.g. focus the view next to this one)                                                      |
//! | <kbd>Home</kbd>                     | Move the cursor to the beginning of the current line.                                                                                                                                                                                                  |
//! | <kbd>End</kbd>                      | Move the cursor to the end of the current line.                                                                                                                                                                                                        |
//! | <kbd>Shift</kbd> + <kbd>Home</kbd>  | Move the cursor to position (0 ,0) which means to the beginning of the view.                                                                                                                                                                           |
//! | <kbd>Shift</kbd> + <kbd>End</kbd>   | Move the cursor to the last nibble in the view.                                                                                                                                                                                                        |
//! | <kbd>+</kbd>                        | Increase the amount of data by one byte. It will be filled up with `0`.                                                                                                                                                                                |
//! | <kbd>-</kbd>                        | Decrease the amount of data by one. Any data that will leave the viewable area, will be permanantly lost.                                                                                                                                              |
//! | <kbd>0-9</kbd>, <kbd>a-f</kbd>      | Set the nibble under the cursor to the corresponding hex value. Note, that this is only available in the editable state, see [`DisplayState`](enum.DisplayState.html#Editable) and [`set_display_state`](struct.HexView.html#method.set_display_state) |

extern crate cursive;
extern crate itertools;

use std::borrow::Borrow;
use std::cmp::min;

use cursive::{Printer, With};
use cursive::theme::{ColorStyle, Effect};
use cursive::direction::Direction;
use cursive::event::{Event, EventResult, Key, MouseEvent};
use cursive::view::View;
use cursive::vec::Vec2;
use itertools::Itertools;

/// Controls the possible interactions with a [HexView].
///
/// This enum is used for the [`set_display_state`] method
/// and controls the interaction inside of the cursive environment.
///
/// [HexView]: struct.HexView.html
/// [`set_display_state`]: struct.HexView.html#method.set_display_state
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum DisplayState {
    /// The view can neither be focused, nor edited
    Disabled,
    /// The view can be focused but not edited
    Enabled,
    /// The view can be focused and edited
    Editable,
}

/// Hexadecimal viewer.
///
/// This is a classic hexview which can be used to view and manipulate data which resides inside
/// this struct. There are severeal states in which the view can be operatered, see [`DisplayState`].
/// You should consider the corresponding method docs for each state.
///
/// [`DisplayState`]: enum.DisplayState.html
///
/// # Examples
///
/// ```
/// extern crate cursive;
/// extern crate cursive_hexview;
///
/// use cursive_hexview::{DisplayState,HexView};
///
/// fn main() {
///     let view = HexView::new().display_state(DisplayState::Editable);
///     let mut cur = cursive::Cursive::dummy();
///
///     cur.add_layer(cursive::views::Dialog::around(view).title("HexView"));
///
///     // cur.run();
/// }
/// ```
///
pub struct HexView {
    cursor: Vec2,
    data: Vec<u8>,
    state: DisplayState,
}

impl Default for HexView {
    /// Creates a new, default `HexView` with an empty databuffer and disabled state.
    fn default() -> Self {
        HexView::new()
    }
}

impl HexView {
    /// Creates a new, default `HexView` with an empty databuffer and disabled state.
    ///
    /// # Examples
    ///
    /// ```
    /// # use cursive_hexview::HexView;
    /// let view = HexView::new();
    /// ```
    pub fn new() -> Self {
        Self::new_from_iter(Vec::<u8>::new())
    }

    /// Crates a new `HexView` with the given data and disabled state.
    ///
    /// # Examples
    ///
    /// With data from a `Vec`:
    ///
    /// ```
    /// # use cursive_hexview::HexView;
    /// let view = HexView::new_from_iter(vec![3, 6, 1, 8, 250]);
    /// ```
    ///
    /// With data from a byte string literal:
    ///
    /// ```
    /// # use cursive_hexview::HexView;
    /// let view = HexView::new_from_iter(b"Hello, World!");
    /// ```
    ///
    /// Or with a slice
    ///
    /// ```
    /// # use cursive_hexview::HexView;
    /// let view = HexView::new_from_iter(&[5, 6, 2, 89]);
    /// ```
    pub fn new_from_iter<B: Borrow<u8>, I: IntoIterator<Item = B>>(data: I) -> Self {
        HexView {
            cursor: Vec2::zero(),
            data: data.into_iter().map(|u| *u.borrow()).collect(),
            state: DisplayState::Disabled,
        }
    }

    /// Returns a reference to the internal data.
    ///
    /// # Examples
    ///
    /// ```
    /// # use cursive_hexview::HexView;
    /// let data = vec![3, 4, 9, 1];
    /// let view = HexView::new_from_iter(data.clone());
    /// assert_eq!(view.data(), &data);
    /// ```
    ///
    pub fn data(&self) -> &Vec<u8> {
        &self.data
    }

    /// Sets the data during the lifetime of this instance.
    ///
    /// For insance to update the data due to an external event.
    ///
    /// ```
    /// # use cursive_hexview::HexView;
    /// let mut view = cursive_hexview::HexView::new();
    /// view.set_data(b"Hello, World!".to_owned().iter());
    /// ```
    pub fn set_data<B: Borrow<u8>, I: IntoIterator<Item = B>>(&mut self, data: I) {
        self.data = data.into_iter().map(|u| *u.borrow()).collect();
    }

    /// [set_display_state](#method.set_display_state)
    pub fn display_state(self, state: DisplayState) -> Self {
        self.with(|s| s.set_display_state(state))
    }

    /// Sets the state of the view to one of the variants from `DisplayState`.
    ///
    /// This will alter the behavior of the view accrodingly to the set state.
    ///
    /// If the state is set to `Disabled` this view can neither be focused nor edited. If the state
    /// is set to `Enabled` it can be focused and the cursor can be moved around, but no data can
    /// be altered. If set to `Editable` this view behaves like `Enabled` but the data *can* be altered.
    ///
    /// # Note
    ///
    /// This has nothing to do with rusts type system, which means even when this instance is set to
    /// `Disabled` you still can alter the data through [set_data](#method.set_data) but you cannot
    /// alter it with the keyboard commands (<kbd>+</kbd>, <kbd>-</kbd>, <kbd>#hexvalue</kbd>).
    ///
    /// # Examples
    ///
    /// ```
    /// # use cursive_hexview::{DisplayState,HexView};
    /// let view = HexView::new().display_state(DisplayState::Editable);
    /// ```
    pub fn set_display_state(&mut self, state: DisplayState) {
        self.state = state;
    }

    /// Returns the length of the data.
    ///
    /// # Examples
    ///
    /// ```
    /// # use cursive_hexview::HexView;
    /// let view = HexView::new_from_iter(vec![0, 1, 2, 3]);
    /// assert_eq!(4, view.len());
    /// ```
    ///
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Checks whether the data is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// # use cursive_hexview::HexView;
    /// let view = HexView::new();
    /// assert!(view.is_empty());
    /// ```
    ///
    /// ```
    /// # use cursive_hexview::HexView;
    /// let view = HexView::new_from_iter(b"ABC");
    /// assert!(!view.is_empty());
    /// ```
    ///
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// Sets the length of the data which this view displays.
    ///
    /// If the new length is greater than the current one, 0's will be appended to the data.
    /// If the new length is less than the current one, the data will be truncated and is lost.
    ///
    /// # Examples
    ///
    /// ```
    /// # use cursive_hexview::HexView;
    /// let mut view = HexView::new();
    /// view.set_len(3);
    ///
    /// assert_eq!(view.len(), 3);
    /// assert_eq!(view.data(), &vec![0u8, 0u8, 0u8]);
    /// ```
    ///
    pub fn set_len(&mut self, length: usize) {
        if self.data.len() != length {
            let oldlen = self.data.len();
            self.data.resize(length, 0);
            if oldlen > length {
                self.cursor.y = min(self.cursor.y, self.get_widget_height());
                self.cursor.x = min(self.cursor.x, (self.data.len() * 2).saturating_sub(1));
            }
        }
    }
}

enum Field {
    Addr,
    AddrSep,
    Hex,
    AsciiSep,
    Ascii,
}

//TODO: Maybe make them configurable anyhow. Ideas?!
const U8S_PER_LINE: usize = 16;
const NIBBLES_PER_LINE: usize = U8S_PER_LINE * 2;
const CHARS_PER_GROUP: usize = 4;
const CHARS_PER_SPACING: usize = 1;
const ADDR_HEX_SEPERATOR: &str = ": ";
const HEX_ASCII_SEPERATOR: &str = " │ ";

/// calcs the position in a line with spacing
fn get_cursor_offset(vec: Vec2) -> Vec2 {
    (
        ((vec.x as f32 / CHARS_PER_GROUP as f32).floor() as usize) * CHARS_PER_SPACING,
        0,
    ).into()
}

/// returns the number of elements in `row` for a given `datalen` with `elements_per_line`
fn get_elements_in_row(datalen: usize, row: usize, elements_per_line: usize) -> usize {
    min(
        datalen.saturating_sub(elements_per_line * row),
        elements_per_line,
    )
}

/// returns the maximal cursor position in a `row` for a `datalen` and `elements_per_line`
fn get_max_x_in_row(datalen: usize, row: usize, elements_per_line: usize) -> usize {
    (get_elements_in_row(datalen, row, elements_per_line) * 2).saturating_sub(1)
}

/// converts the character either to itself if it `is_ascii_graphic`
fn make_printable<T: Borrow<u8>>(c: T) -> char {
    let c = *c.borrow();
    if c.is_ascii_graphic() {
        c as char
    } else {
        '.'
    }
}

// implements helper functions for this struct
impl HexView {
    /// Counts how many digits we need to align the addresses evenly.
    ///
    /// E.g. we need 2 digits for 20 elements (0x14), but only 1 for 10 elements (0xA)
    fn get_addr_digit_length(&self) -> usize {
        match self.data.len() {
            0...1 => 1,
            e => (e as f64).log(16.0).ceil() as usize,
        }
    }

    /// Counts how many rows we need to display the complete data
    fn get_widget_height(&self) -> usize {
        match self.data.len() {
            0 => 1,
            e => (e as f64 / 16.0).ceil() as usize,
        }
    }

    /// calcs the offset to the current position to match the spacing we insert to group the hex chars.
    ///
    /// e.g. cursor (5, 0) will result in (6, 0) because of the 1 space spacing after the fourth char
    /// and cursor (9, 0) will result in (11, 0) because of the 1+1 spacing after the fourth and eighth char
    fn get_cursor_offset(&self) -> Vec2 {
        self.cursor + get_cursor_offset(self.cursor)
    }

    /// gets the amount of nibbles in the current row
    fn get_elements_in_current_row(&self) -> usize {
        get_elements_in_row(self.data.len(), self.cursor.y, U8S_PER_LINE)
    }

    /// gets the max cursor-x position in the current row
    fn get_max_x_in_current_row(&self) -> usize {
        get_max_x_in_row(self.data.len(), self.cursor.y, U8S_PER_LINE)
    }

    /// advances the x position by one
    ///
    /// Returns either an `EventResult::Ignored` if the end of
    /// the line is reached or `EventResult::Consumed(None)` if it was successful.
    fn cursor_x_advance(&mut self) -> EventResult {
        let max_pos = self.get_max_x_in_current_row();
        if self.cursor.x == max_pos {
            return EventResult::Ignored;
        } else {
            self.cursor.x = min(self.cursor.x + 1, max_pos);
        }
        EventResult::Consumed(None)
    }

    /// Gets the element under the cursor
    ///
    /// (which points to a nibble, but we are interested in the
    /// whole u8)
    ///
    /// Returns none if the cursor is out of range.
    fn get_element_under_cursor(&self) -> Option<u8> {
        let elem = self.cursor.y * U8S_PER_LINE + self.cursor.x / 2;
        if let Some(d) = self.data.get(elem) {
            Some(*d)
        } else {
            None
        }
    }

    /// Converts the visual position to a non spaced one.
    ///
    /// This function is used to convert the
    /// point where the mouse clicked to the real cursor position without padding
    fn convert_visual_to_real_cursor(&self, pos: Vec2) -> Vec2 {
        let mut res = pos;
        let hex_offset = self.get_field_length(Field::Addr) + self.get_field_length(Field::AddrSep);

        res.y = min(self.get_widget_height() - 1, pos.y);
        res.x = res.x.saturating_sub(hex_offset);
        res.x = res.x.saturating_sub(get_cursor_offset(res).x);
        res.x = min(
            get_max_x_in_row(self.data.len(), res.y, U8S_PER_LINE),
            res.x,
        );

        res
    }

    /// returns the displayed characters per field
    #[allow(unknown_lints, needless_pass_by_value)]
    fn get_field_length(&self, field: Field) -> usize {
        match field {
            Field::Addr => self.get_addr_digit_length(),
            Field::AddrSep => ADDR_HEX_SEPERATOR.len(),
            Field::Hex => (CHARS_PER_GROUP + CHARS_PER_SPACING) * U8S_PER_LINE / 2,
            Field::AsciiSep => HEX_ASCII_SEPERATOR.len(),
            Field::Ascii => NIBBLES_PER_LINE,
        }
    }
}

// implements draw-helper functions
// it will look as follows
// addr: hexehex hexhex hexhex ... | asciiiiiii
// the addr field will be padded, so that all addresses are equal in length
// the hex field will be grouped by 4 character (nibble) and seperated by 1 space
// the seperator is a special pipe, which is longer and connects with the lower and bottom "pipe" (BOX DRAWINGS LIGHT VERTICAL \u{2502})
// the ascii part is just the ascii char of the coressponding hex value if it is [graphical](https://doc.rust-lang.org/std/primitive.u8.html#method.is_ascii_graphic), if not it will be displayed as a dot (.)
impl HexView {
    /// draws the addr field into the printer
    fn draw_addr(&self, printer: &Printer) {
        let digits_len = self.get_addr_digit_length();
        for lines in 0..self.get_widget_height() {
            printer.print(
                (0, lines),
                &format!("{:0len$X}", lines * U8S_PER_LINE, len = digits_len),
            );
        }
    }

    fn draw_addr_hex_sep(&self, printer: &Printer) {
        printer.print_vline((0, 0), self.get_widget_height(), ADDR_HEX_SEPERATOR);
    }

    /// draws the hex fields between the addr and ascii representation
    fn draw_hex(&self, printer: &Printer) {
        for (i, c) in self.data.chunks(U8S_PER_LINE).enumerate() {
            let hex = c.chunks(2)
                .map(|c| {
                    if c.len() == 2 {
                        format!("{:02X}{:02X}", c[0], c[1])
                    } else {
                        format!("{:02X}", c[0])
                    }
                })
                .format(" ");
            printer.print((0, i), &format!("{}", hex));
        }
    }

    /// draws the ascii seperator between the hex and ascii representation
    fn draw_ascii_sep(&self, printer: &Printer) {
        printer.print_vline((0, 0), self.get_widget_height(), HEX_ASCII_SEPERATOR);
    }

    /// draws the ascii chars
    fn draw_ascii(&self, printer: &Printer) {
        for (i, c) in self.data.chunks(16).enumerate() {
            let ascii: String = c.iter().map(make_printable).collect();
            printer.print((0, i), &ascii);
        }
    }

    /// this highlights the complete hex byte under the cursor
    fn highlight_current_hex(&self, printer: &Printer) {
        if let Some(elem) = self.get_element_under_cursor() {
            let high = self.cursor.x % 2 == 0;
            let hpos = self.get_cursor_offset();
            let dpos = hpos.map_x(|x| if high { x + 1 } else { x - 1 });

            let fem = format!("{:02X}", elem);
            let s = fem.split_at(1);
            let ext = |hl| if hl { s.0 } else { s.1 };

            printer.with_color(ColorStyle::highlight(), |p| p.print(hpos, ext(high)));
            printer.with_color(ColorStyle::secondary(), |p| {
                p.with_effect(Effect::Reverse, |p| p.print(dpos, ext(!high)))
            });
        }
    }

    /// this highlights the corresponding ascii value of the hex which is under the cursor
    fn highlight_current_ascii(&self, printer: &Printer) {
        if let Some(elem) = self.get_element_under_cursor() {
            let pos = self.cursor.map_x(|x| x / 2);
            let ascii = make_printable(&elem);
            printer.with_color(ColorStyle::highlight(), |p| p.print(pos, &ascii.to_string()));
        }
    }
}

impl View for HexView {
    fn on_event(&mut self, event: Event) -> EventResult {
        if self.state == DisplayState::Disabled {
            return EventResult::Ignored;
        }

        match event {
            //view keys
            Event::Key(k) => match k {
                Key::Left => if self.cursor.x == 0 {
                    return EventResult::Ignored;
                } else {
                    self.cursor.x = self.cursor.x.saturating_sub(1)
                },
                Key::Right => {
                    return self.cursor_x_advance();
                }
                Key::Up => {
                    if self.cursor.y == 0 {
                        return EventResult::Ignored;
                    } else {
                        self.cursor.y = self.cursor.y.saturating_sub(1)
                    }
                }
                Key::Down => {
                    if self.cursor.y == self.get_widget_height().saturating_sub(1) {
                        return EventResult::Ignored;
                    } else {
                        let max_pos = min(self.data.len(), self.cursor.y / 2 + 16).saturating_sub(1);
                        self.cursor.y = min(self.cursor.y + 1, max_pos);
                        self.cursor.x = min(
                            self.cursor.x,
                            self.get_elements_in_current_row().saturating_sub(1) * 2,
                        );
                    }
                }
                Key::Home => self.cursor.x = 0,
                Key::End => self.cursor.x = self.get_max_x_in_current_row(),
                _ => {
                    return EventResult::Ignored;
                }
            },
            Event::Shift(Key::Home) => self.cursor = (0, 0).into(),
            Event::Shift(Key::End) => {
                self.cursor = (
                    get_max_x_in_row(self.data.len(), self.get_widget_height() - 1, 16),
                    self.get_widget_height() - 1,
                ).into()
            }

            //edit keys
            Event::Char(c) => {
                if self.state != DisplayState::Editable {
                    return EventResult::Ignored;
                }

                match c {
                    '+' => {
                        let datalen = self.data.len();
                        self.set_len(datalen + 1);
                    }
                    '-' => {
                        let datalen = self.data.len();
                        self.set_len(datalen.saturating_sub(1));
                    }
                    _ => {
                        if let Some(val) = c.to_digit(16) {
                            if let Some(dat) = self.get_element_under_cursor() {
                                let realpos = self.cursor;
                                let elem = realpos.y * 16 + realpos.x / 2;
                                let high = self.cursor.x % 2 == 0;
                                let mask = 0xF << if high { 4 } else { 0 };

                                self.data[elem] = (dat & !mask) | ((val as u8) << if high { 4 } else { 0 });
                                self.cursor_x_advance();
                            }
                        } else {
                            return EventResult::Ignored;
                        }
                    }
                }
            }
            Event::Mouse {
                offset,
                position,
                event,
            } => match event {
                MouseEvent::Press(_) => {
                    if let Some(position) = position.checked_sub(offset) {
                        self.cursor = self.convert_visual_to_real_cursor(position);
                    } else {
                        return EventResult::Ignored;
                    }
                }
                _ => {
                    return EventResult::Ignored;
                }
            },
            _ => {
                return EventResult::Ignored;
            }
        };

        EventResult::Consumed(None)
    }

    fn required_size(&mut self, _: Vec2) -> Vec2 {
        let length = self.get_field_length(Field::Addr) + self.get_field_length(Field::AddrSep)
            + self.get_field_length(Field::Hex) + self.get_field_length(Field::AsciiSep)
            + self.get_field_length(Field::Ascii);

        (length, self.get_widget_height()).into()
    }

    fn draw(&self, printer: &Printer) {
        let height = self.get_widget_height();
        //they are a tuple of (offset, len)
        let addr = (0usize, self.get_field_length(Field::Addr));
        let addr_sep = (addr.0 + addr.1, self.get_field_length(Field::AddrSep));
        let hex = (addr_sep.0 + addr_sep.1, self.get_field_length(Field::Hex));
        let ascii_sep = (hex.0 + hex.1, self.get_field_length(Field::AsciiSep));
        let ascii = (
            ascii_sep.0 + ascii_sep.1,
            self.get_field_length(Field::Ascii),
        );

        self.draw_addr(&printer.offset((addr.0, 0)).cropped((addr.1, height)));
        self.draw_addr_hex_sep(&printer.offset((addr_sep.0, 0)).cropped((addr_sep.1, height)));
        self.draw_hex(&printer.offset((hex.0, 0)).cropped((hex.1, height)));
        self.draw_ascii_sep(&printer.offset((ascii_sep.0, 0)).cropped((ascii_sep.1, height)));
        self.draw_ascii(&printer.offset((ascii.0, 0)).cropped((ascii.1, height)));

        if self.state != DisplayState::Disabled {
            self.highlight_current_hex(&printer.offset((hex.0, 0)).cropped((hex.1, height)).focused(true));
            self.highlight_current_ascii(&printer.offset((ascii.0, 0)).cropped((ascii.1, height)).focused(true));
        }
    }

    fn take_focus(&mut self, _: Direction) -> bool {
        self.state != DisplayState::Disabled
    }
}

//TODO: needs_relayout: only when cursor moved or data has been updated (either internally or externally)
//      required_size:  support different views (e.g. wihtout ascii, without addr, hex only)