Skip to main content

A python library for creating nice layouts in the console environment

Project description

Conlay - Console Layout

PyPi version Github Issues Github License

Create visually pleasing console layouts with this easy-to-use Python library.

Installing

Install using pip

pip install conlay

or install it from PyPi

Example

from conlay import *

layout = Conlay()

blank_box = BoldBox(0, 0, 30, 5)
layout.add(blank_box)

label = ThinLabel(0, 0, "this is a test")
blank_box.add(label)

layout.print()

Console output:

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃╭──────────────╮            ┃
┃│this is a test│            ┃
┃╰──────────────╯            ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛


>

Conlay()

Conlay() is the core class within the library. It generates and prints out the layout.

layout = Conlay()

There are some attributes that can be used to modify or update the positioning, scale, color and other things. Because each LayoutElement() is a subclass of Conlay(), each LayoutElement() has the same attributes.

attribute description expected type default value
relative_x Relative x-position to the parent object int 0
relative_y Relative y-position to the parent object int 0
absolute_x Absolute x-position int 0
absolute_y Absolute y-position int 0
width Elements width int 0
min_width Minimal width. May limit the specified with int 0
max_width Maximal width. May limit the specified with int 0
height Elements height int 0
min_height Minimal height. May limit the specified with int 0
max_height Maximal height. May limit the specified with int 0
zindex Indicates whether an element is printed above or below other elements int 0
padding_x Specifies the padding on the x axis, which affects the absolute position of the child elements int 0
padding_y Specifies the padding on the y axis, which affects the absolute position of the child elements int 0
text Specifies the text content str n/a
placeholder Specifies the placeholder content str n/a
content Stores the text content of the input element, etc. str n/a
background Specifies whether the element should have a background or not bool False
background_color Specifies the background color Color.Bg Color.Bg.clear
border_color Specifies the border color Color.Fg Color.Fg.clear
text_color Specifies the text color Color.Fg Color.Fg.clear
placeholder_color Specifies the placeholder color Color.Fg Color.Fg.rgb(150, 150, 150)

Conlay.add()

You can use add() to add LayoutElements to other LayoutElements.

Syntax:

parent.add(child)
argument description expected type
child child element LayoutElements() or one of its subclasses such as Box(), Label(), etc.

Example:

# add a LayoutElement to the main layout 
layout = Conlay()
layout.add(ThinBox(...))

# add a LayoutElement to another LayoutElement
BoldBox(...).add(ThinLabel(...))

Conlay.print()

You have to call print() to generate and print out the layout.

Syntax:

layout.print()

Example:

layout = Conlay()
...
layout.print()

LayoutElement()

The LayoutElement() class serves as a superclass for all other layout elements such as Box(), Label(), etc.

Syntax:

element = LayoutElement(x, y, w, h, border)
argument description expected type
x Relative x-position to its parent element int
y Relative y-position to its parent element int
w Elements width int
h Elements height int
border Borders character set Border() or one of its subclasses such as Bold() or Thin()

Example:

from conlay import *

layout = Conlay()

element = LayoutElement(0, 0, 30, 5, Thin())
layout.add(element)

layout.print()

Console output:

╭────────────────────────────╮
│                            │
│                            │
│                            │
╰────────────────────────────╯


>

Box()

The Box() class is a subclass of the LayoutElement() class and is used to create a simple box.

Syntax:

element = Box(x, y, w, h, border)
argument description expected type
x Relative x-position to its parent element int
y Relative y-position to its parent element int
w Elements width int
h Elements height int
border Borders character set Border() or one of its subclasses such as Bold() or Thin()

Example:

from conlay import *

layout = Conlay()

box = Box(0, 0, 30, 5, Thin())
layout.add(box)

layout.print()

Console output:

╭────────────────────────────╮
│                            │
│                            │
│                            │
╰────────────────────────────╯


>

ThinBox()

The ThinBox() class is a subclass of the Box() class and is used to create a simple box with a Thin() Border.

Syntax:

element = ThinBox(x, y, w, h)
argument description expected type
x Relative x-position to its parent element int
y Relative y-position to its parent element int
w Elements width int
h Elements height int

Example:

from conlay import *

layout = Conlay()

thinbox = ThinBox(0, 0, 30, 5)
layout.add(thinbox)

layout.print()

Console output:

╭────────────────────────────╮
│                            │
│                            │
│                            │
╰────────────────────────────╯


>

BoldBox()

The BoldBox() class is a subclass of the Box() class and is used to create a simple box with a Bold() Border.

Syntax:

element = BoldBox(x, y, w, h)
argument description expected type
x Relative x-position to its parent element int
y Relative y-position to its parent element int
w Elements width int
h Elements height int

Example:

from conlay import *

layout = Conlay()

boldbox = BoldBox(0, 0, 30, 5)
layout.add(boldbox)

layout.print()

Console output:

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃                            ┃
┃                            ┃
┃                            ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛


>

Label()

The Label() class is a subclass of the LayoutElement() class and is used to create a simple box.

Syntax:

element = Label(x, y, text, border)
argument description expected type
x Relative x-position to its parent element int
y Relative y-position to its parent element int
text Text content str
border Borders character set Border() or one of its subclasses such as Bold() or Thin()
from conlay import *

layout = Conlay()

label = Label(0, 0, "this is a test", Thin())
layout.add(label)

layout.print()

Console output:

╭──────────────╮
│this is a test│
╰──────────────╯


>

ThinLabel()

The ThinLabel() class is a subclass of the Label() class and is used to create a simple label with a Thin() Border.

Syntax:

element = ThinLabel(x, y, text)
argument description expected type
x Relative x-position to its parent element int
y Relative y-position to its parent element int
text Text content str
from conlay import *

layout = Conlay()

thinlabel = ThinLabel(0, 0, "this is a test")
layout.add(thinlabel)

layout.print()

Console output:

╭──────────────╮
│this is a test│
╰──────────────╯


>

BoldLabel()

The BoldLabel() class is a subclass of the Label() class and is used to create a simple label with a Bold() Border.

Syntax:

element = BoldLabel(x, y, text)
argument description expected type
x Relative x-position to its parent element int
y Relative y-position to its parent element int
text Text content str
from conlay import *

layout = Conlay()

boldlabel = BoldLabel(0, 0, "this is a test")
layout.add(boldlabel)

layout.print()

Console output:

┏━━━━━━━━━━━━━━┓
┃this is a test┃
┗━━━━━━━━━━━━━━┛


>

Input()

The Input() class is a subclass of the LayoutElement() class and is used to create a input field.

Syntax:

element = Input(x, y, text, length, border)
argument description expected type
x Relative x-position to its parent element int
y Relative y-position to its parent element int
text Text content str
length expected input length int
border Borders character set Border() or one of its subclasses such as Bold() or Thin()
from conlay import *

layout = Conlay()

input_element = Input(0, 0, "input:", 10, Thin())
layout.add(input_element)

layout.print()

Console output:

╭────────────────╮
│input:          │
╰────────────────╯


>

after you've entered a text, you can get it with the element.content variable

from conlay import *

layout = Conlay()

input_element = Input(0, 0, "input:", 10, Thin())
layout.add(input_element)

layout.print()

print(input_element.content)

ThinInput()

The ThinInput() class is a subclass of the Input() class and is used to create a simple input field with a Thin() Border.

Syntax:

element = ThinInput(x, y, text, length)
argument description expected type
x Relative x-position to its parent element int
y Relative y-position to its parent element int
text Text content str
length expected input length int
from conlay import *

layout = Conlay()

input_element = ThinInput(0, 0, "input:", 10)
layout.add(input_element)

layout.print()

Console output:

╭────────────────╮
│input:          │
╰────────────────╯


>

BoldInput()

The BoldInput() class is a subclass of the Input() class and is used to create a simple input field with a Bold() Border.

Syntax:

element = BoldInput(x, y, text, length)
argument description expected type
x Relative x-position to its parent element int
y Relative y-position to its parent element int
text Text content str
length expected input length int
from conlay import *

layout = Conlay()

input_element = BoldInput(0, 0, "input:", 10)
layout.add(input_element)

layout.print()

Console output:

┏━━━━━━━━━━━━━━━━┓
┃input:          ┃
┗━━━━━━━━━━━━━━━━┛


>

Cursor

The Cursor class provides a few functions for simple Ansi Escape cursor actions.

Cursor.setPosition()

Set the cursor to a specific position. The Ansi Escape Sequence used for this command is \x1b[<y>;<x>H.

Syntax:

Cursor.setPosition(x, y)

Example:

Cursor.setPosition(10, 5)

Cursor.shiftHorizontal()

Move the cursor along the X axis. The Ansi Escape Sequence used for this command is \x1b[<x>D and \x1b[<x>C.

Syntax:

Cursor.shiftHorizontal(x)

Example:

# move the cursor 10 collumns to the right
Cursor.shiftHorizontal(10)

# move the cursor 5 collumns to the left
Cursor.shiftHorizontal(-5)

Cursor.shiftVertical()

Move the cursor along the Y axis. The Ansi Escape Sequence used for this command is \x1b[<y>A and \x1b[<y>B.

Syntax:

Cursor.shiftVertical(y)

Example:

# move the cursor 10 collumns down
Cursor.shiftVertical(10)

# move the cursor 5 collumns up
Cursor.shiftVertical(-5)

Cursor.hide()

Hides the cursor. The Ansi Escape Sequence used for this command is \x1b[?25l.

Syntax:

Cursor.hide()

Cursor.show()

Shows the cursor. The Ansi Escape Sequence used for this command is \x1b[?25h.

Syntax:

Cursor.show()

Console

The Console class provides a few functions for simple Ansi Escape console actions.

Console.reset()

This Function resets the console. The Ansi Escape Sequence used for this command is \x1bc.

Syntax:

Console.reset()

Console.clear()

This Function clears the console. The Ansi Escape Sequence used for this command is \x1b[3J.

Syntax:

Console.clear()

Console.eraseLineToEnd()

Erase the line from the cursor position to the end. The Ansi Escape Sequence used for this command is \x1b[0K.

Syntax:

Console.eraseLineToEnd()

Console.eraseLineFromStart()

Erase the line from start to the cursor position. The Ansi Escape Sequence used for this command is \x1b[1K.

Syntax:

Console.eraseLineFromStart()

Console.eraseLine()

Erase the line. The Ansi Escape Sequence used for this command is \x1b[2K.

Syntax:

Console.eraseLine()

Color

The Color class allows you to set the foreground and background color using the Ansi Escape Color Codes.

Color.Bg

The Color.Bg class provides a few predefined background Ansi Escape Color Codes.

Variable Name rgb values ansi escape sequence
Color.Bg.black 0, 0, 0 \x1b[49m
Color.Bg.black 0, 0, 0 \x1b[48;2;0;0;0m
Color.Bg.white 255, 255, 255 \x1b[48;2;255;255;255m
Color.Bg.red 205, 49, 49 \x1b[48;2;205;49;49m
Color.Bg.green 13, 188, 121 \x1b[48;2;13;188;121m
Color.Bg.blue 36, 114, 200 \x1b[48;2;36;114;200m
Color.Bg.yellow 229, 229, 16 \x1b[48;2;229;229;16m
Color.Bg.purple 188, 63, 188 \x1b[48;2;188;63;188m
Color.Bg.cyan 17, 168, 205 \x1b[48;2;17;168;205m

Color.Bg.rgb()

This function takes three arguments r, g, b and returns a customized background Ansi Escape Color Code

Syntax:

Color.Bg.rgb(r, g, b)

Example:

>>> Color.Bg.rgb(255, 0, 100)
'\x1b[48;2;255;0;100m'

Color.Fg

The Color.Fg class provides a few predefined foreground Ansi Escape Color Codes.

Variable Name rgb values ansi escape sequence
Color.Bg.black 0, 0, 0 \x1b[39m
Color.Fg.black 0, 0, 0 \x1b[38;2;0;0;0m
Color.Fg.white 255, 255, 255 \x1b[38;2;255;255;255m
Color.Fg.red 205, 49, 49 \x1b[38;2;205;49;49m
Color.Fg.green 13, 188, 121 \x1b[38;2;13;188;121m
Color.Fg.blue 36, 114, 200 \x1b[38;2;36;114;200m
Color.Fg.yellow 229, 229, 16 \x1b[38;2;229;229;16m
Color.Fg.purple 188, 63, 188 \x1b[38;2;188;63;188m
Color.Fg.cyan 17, 168, 205 \x1b[38;2;17;168;205m

Color.Fg.rgb()

This function takes three arguments r, g, b and returns a customized foreground Ansi Escape Color Code

Syntax:

Color.Fg.rgb(r, g, b)

Example:

>>> Color.Fg.rgb(255, 0, 100)
'\x1b[38;2;255;0;100m'

Border

The Border() class and its subclasses serve as a character set and define the required Unicode characters.

class attributes unicode characters
vertical n/a
horizontal n/a
top_left n/a
top_right n/a
bottom_left n/a
bottom_right n/a

Thin()

The Thin() class is a subclass of Border() and serves as a character set and defines the required thin Unicode characters.

class attributes unicode characters
vertical \u2503
horizontal \u2501
top_left \u250F
top_right \u2513
bottom_left \u2517
bottom_right \u251B

Bold()

The Bold() class is a subclass of Border() and serves as a character set and defines the required bold Unicode characters.

class attributes unicode characters
vertical \u2502
horizontal \u2500
top_left \u256D
top_right \u256E
bottom_left \u2570
bottom_right \u256F

Summary

Elements
Cursor & Console
Coloring
Border & Border Types

License

Licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

conlay-1.1.0.tar.gz (12.2 kB view hashes)

Uploaded Source

Built Distribution

conlay-1.1.0-py3-none-any.whl (8.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page