-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Docs: Placement and control sequence examples
- Loading branch information
1 parent
55824f6
commit 09f4045
Showing
6 changed files
with
162 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
.. _sequence example: | ||
|
||
================================ | ||
Animating with control sequences | ||
================================ | ||
|
||
.. py:currentmodule:: chafa | ||
This is a fun little example that demonstrates how to use the :py:meth:`TermInfo.emit` method. | ||
|
||
.. image:: example_img/animation.gif | ||
:width: 300 | ||
:align: center | ||
|
||
The code that produced this animation follows: | ||
|
||
:: | ||
|
||
from chafa import * | ||
from time import sleep | ||
import sys | ||
|
||
SL=15 | ||
|
||
def snake(n, step): | ||
if step < 0: turns = ["┏","┛"] | ||
else: turns = ["┓", "┗"] | ||
|
||
if n == 1: | ||
return turns[0] | ||
elif n == SL - 1: | ||
return turns[1] | ||
|
||
return "┃" | ||
|
||
def gradient(x, y): | ||
return (80 + 5*x, 255 - (100 + 10*y), 200) | ||
|
||
# Grab our info | ||
db = TermDb() | ||
info = db.detect() | ||
|
||
# Clear the terminal | ||
clear = info.emit(TermSeq.CHAFA_TERM_SEQ_CLEAR) | ||
no_cursor = info.emit(TermSeq.CHAFA_TERM_SEQ_DISABLE_CURSOR) | ||
|
||
print(clear.decode()) | ||
print(no_cursor.decode()) | ||
|
||
rows = list(range(1,SL)) | ||
cols = list(range(1,32)) | ||
|
||
for x in cols: | ||
# We want to step backwards if column is even | ||
step = 2 * (x%2) - 1 | ||
|
||
for y in rows[::step]: | ||
# Grab char | ||
char = snake(y, step) | ||
|
||
# Move to position | ||
out = info.emit( | ||
TermSeq.CHAFA_TERM_SEQ_CURSOR_TO_POS, | ||
x, y | ||
) | ||
|
||
# Set color | ||
color = info.emit( | ||
TermSeq.CHAFA_TERM_SEQ_SET_COLOR_FG_DIRECT, | ||
*gradient(x,y) | ||
) | ||
|
||
sys.stdout.buffer.write(out) | ||
sys.stdout.buffer.write(color) | ||
sys.stdout.write(char) | ||
sys.stdout.flush() | ||
|
||
sleep(0.01) | ||
|
||
# All back to normal! | ||
reset = info.emit(TermSeq.CHAFA_TERM_SEQ_RESET_TERMINAL_HARD) | ||
print(reset.decode()) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
.. py:currentmodule:: chafa | ||
=========================== | ||
Using :py:class:`Placement` | ||
=========================== | ||
|
||
Here is an example on how to use the :py:class:`Placement` class to draw an image in the center of the :py:class:`Canvas`. | ||
|
||
.. image:: example_img/center_snake.png | ||
:width: 500 | ||
:align: center | ||
|
||
.. versionadded:: 1.2.0 | ||
|
||
:: | ||
|
||
import chafa | ||
from pathlib import Path | ||
from PIL import Image | ||
|
||
# Init canvas config | ||
config = chafa.CanvasConfig() | ||
|
||
# Set canvas height and width | ||
config.width = 70 | ||
config.height = 20 | ||
|
||
# Set cell dimensions for accurate aspect ratio | ||
config.cell_width = 18 | ||
config.cell_height = 46 | ||
|
||
# Set pixel mode | ||
config.pixel_mode = chafa.PixelMode.CHAFA_PIXEL_MODE_KITTY | ||
|
||
# Open image with PIL | ||
image = Image.open(Path(__file__).parent / "snake.jpg") | ||
|
||
width = image.width | ||
height = image.height | ||
bands = len(image.getbands()) | ||
|
||
# Put image into correct format | ||
pixels = image.tobytes() | ||
|
||
# Init the canvas | ||
canvas = chafa.Canvas(config) | ||
|
||
# Init frame | ||
frame = chafa.Frame( | ||
chafa.PixelType.CHAFA_PIXEL_RGB8, | ||
pixels, | ||
height, | ||
width, | ||
width * bands | ||
) | ||
|
||
# Init image and assign our frame to it | ||
chafa_image = chafa.Image() | ||
chafa_image.frame = frame | ||
|
||
# Init the placement of our image | ||
placement = chafa.Placement(chafa_image) | ||
|
||
# Set our desired placement | ||
placement.tuck = chafa.Tuck.CHAFA_TUCK_SHRINK_TO_FIT | ||
placement.halign = chafa.Align.CHAFA_ALIGN_CENTER | ||
placement.valign = chafa.Align.CHAFA_ALIGN_CENTER | ||
|
||
# Put the image on the canvas | ||
canvas.placement = placement | ||
|
||
# Write picture | ||
print(canvas.print().decode()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters