Skip to content

Commit

Permalink
cleanup: one function for all layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
numbertheory committed Jun 6, 2022
1 parent 7e357ff commit 71c48ba
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 81 deletions.
62 changes: 2 additions & 60 deletions dashport/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,63 +285,5 @@ def background(self, color):
for i in range(0, self.cols):
self.screen.insstr(j, i, " ", cpi(self.color_default, color))

def single_panel(self, **kwargs):
"""
Creates one single panel that takes up the screen
"""
self.panels["layout"] = layout.single_panel(
self, border=kwargs.get("border", False),
border_styles=kwargs.get("border_styles", [0]),
scroll=kwargs.get("scroll", False),
height=kwargs.get("height", self.rows),
width=kwargs.get("width", self.cols))

def split_screen_columns(self, **kwargs):
"""
Splits the screen into two vertical panels
"""
self.panels["layout"] = layout.split_screen_columns(
self, border=kwargs.get("border", False),
border_styles=kwargs.get("border_styles", [0, 0]),
scroll=kwargs.get("scroll", False))

def split_screen_rows(self, **kwargs):
"""
Splits the screen into two horizontal panels
"""
self.panels["layout"] = layout.split_screen_rows(
self, border=kwargs.get("border", False),
border_styles=kwargs.get("border_styles", [0, 0]),
scroll=kwargs.get("scroll", False),
center_divide_y=kwargs.get("center_divide_y"))

def split_screen_quad(self, **kwargs):
"""
Splits the screen into four quadrant panels
"""
self.panels["layout"] = layout.quadrants(
self, border=kwargs.get("border", False),
border_styles=kwargs.get("border_styles", [0, 0, 0, 0]),
scroll=kwargs.get("scroll", False))

def split_screen_three_vert(self, **kwargs):
"""
Splits the screen into three panels
"""
self.panels["layout"] = layout.three_panels_vert(
self, border=kwargs.get("border", False),
border_styles=kwargs.get("border_styles", [0, 0, 0]),
long_side=kwargs.get("long_side", "right"),
long_side_width=kwargs.get("long_side_width"),
scroll=kwargs.get("scroll", False))

def split_screen_three_horizontal(self, **kwargs):
"""
Splits the screen into three panels
"""
self.panels["layout"] = layout.three_panels_horizontal(
self, border=kwargs.get("border", False),
border_styles=kwargs.get("border_styles", [0, 0, 0]),
long_side=kwargs.get("long_side", "top"),
long_side_height=kwargs.get("long_side_height"),
scroll=kwargs.get("scroll", False))
def layout(self, layout_name, **kwargs):
self.panels["layout"] = getattr(layout, layout_name)(self, **kwargs)
16 changes: 9 additions & 7 deletions dashport/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
def single_panel(app, **kwargs):
border = kwargs.get("border", False)
border_styles = kwargs.get("border_styles", [0])
height = kwargs.get("height", app.rows)
height = kwargs.get("height", app.rows - app.title_offset)
width = kwargs.get("width", app.cols)
if isinstance(border, bool):
border = [border]
Expand All @@ -29,11 +29,13 @@ def split_screen_columns(app, **kwargs):
border_styles = kwargs.get("border_styles", [0, 0])
if isinstance(border, bool):
border = [border] * 2
win1, panel1 = app.panel(height=app.rows, length=split_cols, y=0, x=0,
win1, panel1 = app.panel(height=app.rows - app.title_offset,
length=split_cols, y=0, x=0,
scroll=kwargs.get("scroll", False),
border=border[0],
border_style=border_styles[0])
win2, panel2 = app.panel(height=app.rows, length=split_cols,
win2, panel2 = app.panel(height=app.rows - app.title_offset,
length=split_cols,
y=0,
x=split_cols,
scroll=kwargs.get("scroll", False),
Expand All @@ -48,7 +50,7 @@ def split_screen_columns(app, **kwargs):

def split_screen_rows(app, **kwargs):
if not kwargs.get("center_divide_y"):
split_rows = [int(app.rows / 2), int(app.rows / 2)]
split_rows = [int(app.rows / 2) - app.title_offset, int(app.rows / 2)]
else:
split_rows = [int(kwargs.get("center_divide_y")),
app.rows - int(kwargs.get("center_divide_y"))]
Expand All @@ -74,7 +76,7 @@ def split_screen_rows(app, **kwargs):


def quadrants(app, **kwargs):
split_rows = int(app.rows / 2)
split_rows = int(app.rows / 2) - app.title_offset
split_cols = int(app.cols / 2)
border = kwargs.get("border", False)
border_styles = kwargs.get("border_styles", [0, 0, 0, 0])
Expand Down Expand Up @@ -109,7 +111,7 @@ def quadrants(app, **kwargs):


def three_panels_vert(app, **kwargs):
split_rows = int(app.rows / 2)
split_rows = int(app.rows / 2) - app.title_offset
split_cols = int(app.cols / 2)
long_side = kwargs.get("long_side", "right")
border = kwargs.get("border", False)
Expand Down Expand Up @@ -164,7 +166,7 @@ def three_panels_vert(app, **kwargs):


def three_panels_horizontal(app, **kwargs):
split_rows = int(app.rows / 2)
split_rows = int(app.rows / 2) - app.title_offset
split_cols = int(app.cols / 2)
long_side = kwargs.get("long_side", "top")
border = kwargs.get("border", False)
Expand Down
2 changes: 1 addition & 1 deletion examples/layouts/split_screen_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def quit(app):
def dashport(stdscr):
app = Dashport(stdscr, color_default=17)
app.add_control("q", quit, case_sensitive=False)
app.split_screen_columns(border=True)
app.layout("split_screen_columns", border=True)
app.print("hello", x=1, y=1, panel="layout.1")
app.print("world", x=1, y=2, panel="layout.1")
while True:
Expand Down
5 changes: 3 additions & 2 deletions examples/layouts/split_screen_quad.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ def quit(app):
def dashport(stdscr):
app = Dashport(stdscr, color_default=176)
app.add_control("q", quit, case_sensitive=False)
app.split_screen_quad(border=[True, False, True, True],
border_styles=[0, 1, 1, None])
app.layout("quadrants",
border=[True, False, True, True],
border_styles=[0, 1, 1, None])
app.panels["layout"][3].border('M', 'M', '=', '=', ' ', ' ', ' ', ' ')
app.print("panel 0", x=1, y=1, panel="layout.0",
A_BOLD=True, A_ITALIC=True)
Expand Down
2 changes: 1 addition & 1 deletion examples/layouts/split_screen_rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def quit(app):
def dashport(stdscr):
app = Dashport(stdscr, color_default=17)
app.add_control("q", quit, case_sensitive=False)
app.split_screen_rows(border=True)
app.layout("split_screen_rows", border=True)
app.print("hello", x=1, y=1, panel="layout.0")
app.print("world", x=1, y=1, panel="layout.1")
while True:
Expand Down
5 changes: 3 additions & 2 deletions examples/layouts/split_screen_three_horizontal.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ def quit(app):
def dashport(stdscr):
app = Dashport(stdscr, color_default=176)
app.add_control("q", quit, case_sensitive=False)
app.split_screen_three_horizontal(
border=[False, False, True], long_side="bottom", long_side_height=10)
app.layout("three_panels_horizontal",
border=[False, False, True], long_side="bottom",
long_side_height=10)
app.print("panel 0", panel="layout.0")
app.print(f"Rows: {app.panel_dimensions[0][0]}", x=5, y=2,
panel="layout.0")
Expand Down
5 changes: 3 additions & 2 deletions examples/layouts/split_screen_three_vert.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ def quit(app):
def dashport(stdscr):
app = Dashport(stdscr, color_default=176)
app.add_control("q", quit, case_sensitive=False)
app.split_screen_three_vert(border=True, long_side="right",
long_side_width=20)
app.layout("three_panels_vert",
border=True, long_side="right",
long_side_width=20)
app.print("panel 0", x=1, y=1, panel="layout.0")
app.print(f"Rows: {app.panel_dimensions[0][0]}", x=5, y=2,
panel="layout.0")
Expand Down
2 changes: 1 addition & 1 deletion examples/util/all_box_drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def dashport(stdscr):
app = Dashport(stdscr)
height = app.rows - 2
app.title_bar(text="Q = quit, B = Next page", align="bottom", color=121)
app.single_panel(scroll=True, height=height)
app.layout("single_panel", scroll=True, height=height)
app.add_control("q", quit)
app.add_control("KEY_UP", move_up)
app.add_control("KEY_DOWN", move_down)
Expand Down
2 changes: 1 addition & 1 deletion examples/views/interactive_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def quit():

def dashport(stdscr):
app = Dashport(stdscr, color_default=8)
app.single_panel(border=False, scroll=True, height=app.rows - 1)
app.layout("single_panel", border=False, scroll=True, height=app.rows - 1)
app.commands = []
while True:
while True:
Expand Down
6 changes: 3 additions & 3 deletions examples/views/multiple_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def quit(app):


def four_way_split(app):
app.split_screen_quad(border=True)
app.layout("quadrants", border=True, height=app.rows - 2)
app.print("panel 0", panel="layout.0")
app.print(f"Rows: {app.panel_dimensions[0][0]}", x=5, y=2,
panel="layout.0")
Expand All @@ -36,8 +36,8 @@ def four_way_split(app):


def three_way_split(app):
app.split_screen_three_vert(border=True, long_side="right",
long_side_width=100)
app.layout("three_panels_vert", border=True, long_side="right",
long_side_width=100)
app.print("panel 0", panel="layout.0")
app.print(f"Rows: {app.panel_dimensions[0][0]}", x=5, y=2,
panel="layout.0")
Expand Down
2 changes: 1 addition & 1 deletion examples/views/scroll_panels.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def dashport(stdscr):
app = Dashport(stdscr, scroll=True)
app.add_control("q", quit)
rows, cols = app.screen.getmaxyx()
app.split_screen_three_vert(border=True, scroll=True)
app.layout("three_panels_vert", border=True, scroll=True)
app.title_bar(text=f"Title {app.cursor_x},{app.cursor_y}",
align="top", color=256)
for i in range(0, 1000):
Expand Down

0 comments on commit 71c48ba

Please sign in to comment.