From c8e9199b035f0d72611a5f7b304162e49c82333d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 6 Jun 2018 09:17:31 +0530 Subject: [PATCH] Refactor tab drawing code --- kitty/tab_bar.py | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/kitty/tab_bar.py b/kitty/tab_bar.py index 0b3035c7a..c57183309 100644 --- a/kitty/tab_bar.py +++ b/kitty/tab_bar.py @@ -15,6 +15,29 @@ from .window import calculate_gl_geometry TabBarData = namedtuple('TabBarData', 'title is_active is_last needs_attention') +DrawData = namedtuple('DrawData', 'leading_spaces sep trailing_spaces bell_on_tab bell_fg') + + +def draw_tab_with_separator(draw_data, screen, tab, before, max_title_length): + if draw_data.leading_spaces: + screen.draw(' ' * draw_data.leading_spaces) + if tab.needs_attention and draw_data.bell_on_tab: + fg = screen.cursor.fg + screen.cursor.fg = draw_data.bell_fg + screen.draw('🔔 ') + screen.cursor.fg = fg + screen.draw(tab.title) + if draw_data.trailing_spaces: + screen.draw(' ' * draw_data.trailing_spaces) + extra = screen.cursor.x - before - max_title_length + if extra > 0: + screen.cursor.x -= extra + 1 + screen.draw('…') + end = screen.cursor.x + screen.cursor.bold = screen.cursor.italic = False + screen.cursor.fg = screen.cursor.bg = 0 + screen.draw(draw_data.sep) + return end class TabBar: @@ -53,6 +76,7 @@ def as_rgb(x): self.active_bg = as_rgb(color_as_int(opts.active_tab_background)) self.active_fg = as_rgb(color_as_int(opts.active_tab_foreground)) self.bell_fg = as_rgb(0xff0000) + self.draw_data = DrawData(self.leading_spaces, self.sep, self.trailing_spaces, self.opts.bell_on_tab, self.bell_fg) def patch_colors(self, spec): if 'active_tab_foreground' in spec: @@ -96,26 +120,11 @@ def update(self, data): for t in data: s.cursor.bg = self.active_bg if t.is_active else 0 - s.cursor.fg = fg = self.active_fg if t.is_active else 0 + s.cursor.fg = self.active_fg if t.is_active else 0 s.cursor.bold, s.cursor.italic = self.active_font_style if t.is_active else self.inactive_font_style before = s.cursor.x - if self.leading_spaces: - s.draw(' ' * self.leading_spaces) - if t.needs_attention and self.opts.bell_on_tab: - s.cursor.fg = self.bell_fg - s.draw('🔔 ') - s.cursor.fg = fg - s.draw(t.title) - if self.trailing_spaces: - s.draw(' ' * self.trailing_spaces) - extra = s.cursor.x - before - max_title_length - if extra > 0: - s.cursor.x -= extra + 1 - s.draw('…') - cr.append((before, s.cursor.x)) - s.cursor.bold = s.cursor.italic = False - s.cursor.fg = s.cursor.bg = 0 - s.draw(self.sep) + end = draw_tab_with_separator(self, s, t, before, max_title_length) + cr.append((before, end)) if s.cursor.x > s.columns - max_title_length and not t.is_last: s.draw('…') break