Skip to content

Instantly share code, notes, and snippets.

@MightyPork
Last active September 28, 2017 20:10
Show Gist options
  • Save MightyPork/0cbd57797a5e1e1af4fb96f82dcd71d8 to your computer and use it in GitHub Desktop.
Save MightyPork/0cbd57797a5e1e1af4fb96f82dcd71d8 to your computer and use it in GitHub Desktop.

STRUCTS

enum ScreenSerializeTopic {
  TOPIC_CHANGE_SCREEN_OPTS  = (1<<0),
  TOPIC_CHANGE_CONTENT_ALL  = (1<<1),
  TOPIC_CHANGE_CONTENT_PART = (1<<2),
  TOPIC_CHANGE_TITLE        = (1<<3),
  TOPIC_CHANGE_BUTTONS      = (1<<4),
  TOPIC_CHANGE_CURSOR       = (1<<5),
  TOPIC_INTERNAL            = (1<<6), // debugging internal state
  TOPIC_BELL                = (1<<7), // beep
};

enum SgrAttrBits {
  ATTR_FG        = (1<<0),  //!< 1 if not using default background color (ignore cell bg) - color extension bit
  ATTR_BG        = (1<<1),  //!< 1 if not using default foreground color (ignore cell fg) - color extension bit
  ATTR_BOLD      = (1<<2),  //!< Bold font
  ATTR_UNDERLINE = (1<<3),  //!< Underline decoration
  ATTR_INVERSE   = (1<<4),  //!< Invert colors - this is useful so we can clear then with SGR manipulation commands
  ATTR_BLINK     = (1<<5),  //!< Blinking
  ATTR_ITALIC    = (1<<6),  //!< Italic font
  ATTR_STRIKE    = (1<<7),  //!< Strike-through decoration
  ATTR_OVERLINE  = (1<<8),  //!< Over-line decoration
  ATTR_FAINT     = (1<<9),  //!< Faint foreground color (reduced alpha)
  ATTR_FRAKTUR   = (1<<10), //!< Fraktur font (unicode substitution)
};

SEQUENCE

Legend

u(number) - unicode encoding, codepoint(number+1)
'x'       - literal ascii character
'\xAB'    - ascii character in hex
ascii(x)  - literal ascii character (variable)
"xyz"     - utf-8 string

Begin

'U'
u(topic_bits) - bits from enum ScreenSerializeTopic
# now any of the following sections, depends on bits in the above word
{
  'O' - screen opts
  u(height)
  u(width)
  u(theme)
  u(lower 3 nibbles of default FG)
  u(upper 3 nibbles of default FG)
  u(lower 3 nibbles of default BG)
  u(upper 3 nibbles of default BG)
  u(
    (scr.cursor_visible << 0) |
    (termconf_live.debugbar << 1) | // debugbar - this was previously "hanging"
    (scr.cursors_alt_mode << 2) |
    (scr.numpad_alt_mode << 3) |
    (termconf_live.fn_alt_mode << 4) |
    ((mouse_tracking.mode > MTM_NONE) << 5) | // disables context menu
    ((mouse_tracking.mode >= MTM_NORMAL) << 6) | // disables selecting
    (termconf_live.show_buttons << 7) |
    (termconf_live.show_config_links << 8) |
    ((termconf_live.cursor_shape & 0x07) << 9) | // 9,10,11 - cursor shape based on DECSCUSR
    (termconf_live.crlf_mode << 12) |
    (scr.bracketed_paste << 13) |
    (scr.reverse_video << 14)
  ) # this is mostly identical to the old options word
}

{
  'T' - title
  "title"
  '\x01'
}

{
  'B' - button labels
  u(count)  
  LOOP {
    "button"
    '\x01'
  }
}

{
  'D' - internal state (debug)
  u(
    (scr.insert_mode << 0) |
    (cursor.conceal << 1) |
    (cursor.auto_wrap << 2) |
    (cursor.reverse_wrap << 3) |
    (cursor.origin_mode << 4) |
    (cursor_saved << 5) |
    (state_backup.alternate_active << 6)
  )
  u(cursor_attrs) - bits from enum SgrAttrBits
  u(region_first_row)
  u(region_last_row)
  u(charset_slot_num) - G0 or G1 - 0 or 1
  ascii(charset0) - codepage in G0 ('A', 'B', etc)
  ascii(charset1) - codepage in G1 ('A', 'B', etc)
  u(free_system_heap_size)
  u(num_connected_clients)
}

{
  '!' - bell (beep upon load)
}

{
  'C' - cursor moved
  u(y) - 0-based
  u(x) - 0-based
  u(
    (hanging << 0)
  )
}

{
  'S' - screen content
  # Now the dimensions are defined.
  # This will be used in the future for partial redraws
  u(y0) - left top Y (0-based)
  u(x0) - left top X (0-based)
  u(h) - content height
  u(w) - content width
  
  # now the content is serialized as a sequence of the following:
  LOOP {  
    CHARACTER:
      <any utf8> - character using last attrs and colors
      
    REPEAT:  
      '\x02'
      u(count) - repeat last attrs, colors and character
    
    FG+BG:
      '\x03'
      u(bg << 8 | fg) - change fg and bg
    
    FG:
      '\x05'
      u(fg) - change fg only
    
    BG:
      '\x06'
      u(bg) - change bg only
      
    ATTRS:
      '\x04'
      u(attrs_word) - change attrs; bits from enum SgrAttrBits
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment