Skip to content

Commit

Permalink
V2.8
Browse files Browse the repository at this point in the history
V2.8
  • Loading branch information
kerem3338 authored Nov 12, 2022
1 parent eb291b7 commit 33db76b
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 32 deletions.
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Zoda
Copyright (c) 2022 Zoda

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
![boip simple example](assets/boip.png)
## Türkçe

# Boip
**Boip ile artık Metin tabanlı animasyonlar yapabilirsiniz. Boip hem hızlı hemde kullanımı basittir**

## English
**With Boip you can now make Text based animations. Boip is both fast and simple to use.**
**With Boip you can now make Text based animations. Boip is both fast and simple to use.**<br>
<br>
<br>
# installation

Windows / Linux
* [Github](https://github.com/kerem3338/Boip) sayfası üzerinden indirin.<br>
* Install on [Github](https://github.com/kerem3338/Boip) page.
<br>
<br>
# Example / Örnek
<br>

![Boip simple example](assets/example.gif)<br><br>
**[Source code](examples/arrow.py)**

![Boip simple example](assets/example.gif)<br>
_Basit bir ok animasyonu_<br>
_A simple arrow animation_<br>
* **[Source code](animator.py)**<br>
* **[Changes](changes.md)**
193 changes: 171 additions & 22 deletions animator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
"""
Boip Animator is a python library for creating text-based animations.
Expand All @@ -11,6 +12,9 @@
app.scene("<<<")
app.scene("<<<<")
app.play()
For automatic documantion run this command : python -m pydoc animator
If you want documation to html file run this command : python -m pydoc -w animator
"""

import os
Expand All @@ -20,11 +24,21 @@
import glob
import getpass
import threading
import curses
from pathlib import Path

config = {
"err-show": True
}


try:
import colored_traceback
colored_traceback.add_hook()
except ModuleNotFoundError:
pass


try:
import cursor # Bu kütüphane konsolda imleci saklamak için gereklidir. | This library is required to store the cursor in the console.
except ModuleNotFoundError:
Expand All @@ -37,6 +51,10 @@
pass


SOURCEPATH = Path(__file__).parents[0]

def source_path(path):
return os.path.abspath(os.path.join(SOURCEPATH, path))

class Frame:
"""Frame class for creating frames."""
Expand All @@ -45,44 +63,65 @@ def __init__(self,content:str):


def change(self,content:str):
"""Change the Frame content"""
self.content=content

def __str__(self):
return self.content

class Animator:
def __init__(self, sleep_mode=True, sleep:int or float=1.0):
def __init__(self, sleep_mode=True, warning=True,add_empty=False,sleep:int or float=1.0):
self.sleep_mode=sleep_mode
self.system_lang = locale.getdefaultlocale()[0]



if self.system_lang == "tr_TR":
self.sleep_error = "Hata: Uyku Modu aktif değil"
else:
self.sleep_error = "Error: Sleep mode not activated!"

self.scenes_count = 0
self.scenes = []
self.scenes:list = []


if add_empty:
self.scene("")

self.sleep = sleep
self.add_empty=add_empty
self.nmode_char="\n"
self.version = "2.7"

self.command_frames:dict={}
self.commands_dir:str=source_path("commands")
self.version = "2.8"
self.log=""

self.animation_info = {"author": getpass.getuser(), "usr_lang": self.system_lang, "sleep_mode": self.sleep_mode, "sleep": self.sleep, "scene_count": self.scenes_count, "boip_ver": self.version}

def version(self):
"""Returns version of Boip"""
return self.version

def lenght(self):
"""Calculates animation lenght (it may not be real)
#Warnings#
* works good with cplay()
* works wors with play()"""
if self.sleep_mode is True:
if type(self.sleep) == int:
return int(self.scenes_count)*int(self.sleep)
return int(self.scenes_count)*self.sleep
elif type(self.sleep) == float:
return float(self.scenes_count)*self.sleep
else:
print(self.sleep_error)
if type(self.sleep) == int:
return int(self.scenes_count*0.1)
elif type(self.sleep) == float:
return float(self.scenes_count*0.1)

def create_config(self, filename):
"""Creates config file for animation"""
with open(filename, "w") as file:
file.write(self.animation_info)

Expand All @@ -103,22 +142,35 @@ def scenes_from_dir(self, dir, fileextension="txt"):
"""adds new scenes from dir
Example
test_directory/
0.txt
1.txt
0.txt -> "-*
1.txt -> "*-"
"""
for i in range(len(glob.glob(f"{dir}\\*.{fileextension}"))):
with open(f"{dir}\\{i}.{fileextension}", "r") as file:
self.scenes.append(file.read())
self.scenes_count += 1

def logreset(self):
"""cleans log"""
self._log=""

def log(self,text:str,addscene=True):
self.log+=text
if addscene:
self.scene(self.log)

def logtype(self,text:str):
for char in text:
self.log(char)

def scene(self, scene):
"""add new scene"""
"""Add new scene"""

self.scenes.append(scene)
self.scenes_count += 1

def shape(self, shape, position=None):
"""returns a shape"""
"""Returns a shape"""
square = "##\n##"
if shape == "square":
return square
Expand All @@ -127,7 +179,7 @@ def shape(self, shape, position=None):


def copy_last(self, copy_count=None):
"""copy last scene"""
"""Copy last scene"""
if copy_count is None:
last_scene = len(self.scenes)-1
self.scenes.append(self.scenes[last_scene])
Expand All @@ -139,6 +191,7 @@ def copy_last(self, copy_count=None):
self.scenes.append(self.scenes[last_scene])

def copy_from_id(self, id, copy_count=None):
"""copies the frame from the entered ID"""
if copy_count is None:
scene = self.scenes[id]
self.scenes.append(scene)
Expand All @@ -149,15 +202,15 @@ def copy_from_id(self, id, copy_count=None):
self.scenes.append(self.scenes[id])


def scene_from_id(self, id):
print(self.scenes[id])
def scene_from_id(self, id:int):
return self.scenes[id]

def list_scenes(self):
for i in range(len(self.scenes)):
print(self.scenes[i])

def scenes_count(self):
"""NOT:Sahne içinde kullanılırsa bulunduğu sahneyi eklemez örnek: eğer projenizde 6 sahne varsa 5 sahne gösterecektir eğer sahnenin içinde kullanmazsanız sahne sayınızı normal bir şekilde gösterecektir"""
"""Returns"""
return self.scenes_count

def set_sleep(self, sleep:int or float):
Expand All @@ -178,6 +231,93 @@ def clear(self,nmode=False,scenelenght=None,lastlenght=None):
os.system("clear")
else:
print(self.nmode_char*(scenelenght*os.get_terminal_size().columns))

def testcommandframe(self):
self.command_frames[0]="<COMMAND BEGIN>exec_from_file:test<+><COMMAND END>"

def commandframe(self,id):
def check():
if not id in self.command_frames:
raise Exception(f"InvalidCommandFrame: Frame {id} not in command frames list")
else:
if self.command_frames[id].startswith("<COMMAND BEGIN>") and self.command_frames[id].endswith("<COMMAND END>")==False:
raise Exception(f"InvalidCommand: '<COMMAND BEGIN>' or '<COMMAND END>' not found check command begin and command end")
else:
return True

if check():
new_cframe:str=self.command_frames[id]
new_cframe=new_cframe.replace("<COMMAND BEGIN>","")
new_cframe=new_cframe.replace("<COMMAND END>","")
commands=new_cframe.split("<+>")

while("" in commands):
commands.remove("")

for command in commands:
if command == "":
continue
else:
lenght=len(command.split(":"))
if not lenght == 2:
raise Exception(f"InvalidCommand: The command needs 2 arguments to perform the function not {lenght}")
else:
c=command.split(":")


if c[0] == "sleep":
#Hata:ValueError
self.sleep=float(c[1])

#Dosya içeriğini python kodu olarak yürüt
elif c[0] == "exec_from_file":
file=str(c[1])
if file in os.listdir(self.commands_dir):
with open(source_path(self.commands_dir+"\\"+file),"r") as f:
exec(f.read())
else:
raise FileNotFoundError(f"Boip command file '{file}' not found in '{self.commands_dir}' directory")
else:
raise Exception(f"CommandError: command '{c[0]}' not found.")




def cplay(self,stdscr:curses.wrapper):
"""Play animation with curses (without blankink)
if you want close the animation press 'Q' or 'S' to stop animation"""


curses.curs_set(0)
stdscr.nodelay(1)
playing_scene=0
p=None
for i in range(len(self.scenes)):
try:
if i in self.command_frames:
self.commandframe(i)
playing_scene=i
scene_lenght=len(self.scenes[i].splitlines())
last_lenght=len(self.scenes[i].splitlines())
stdscr.clear()
#Sahneyi çiz
stdscr.addstr(0,0,self.scenes[i])
time.sleep(self.sleep)
stdscr.refresh()
x=stdscr.getch()
if x == ord("q") or x==ord("s"):
break
except KeyboardInterrupt:
self.clear()
print(f"Exited... Last scene: {playing_scene}")
sys.exit()

def new_command(self,frameid:int,command:str,exec_command:bool=False):
"""Adds new command to commands"""
self.command_frames[frameid]=command
if exec_command:
self.commandframe(frameid)

def play(self,nmode=False):
"""Starts Animation"""
Expand All @@ -190,10 +330,13 @@ def play(self,nmode=False):
if self.sleep_mode is True:
for i in range(len(self.scenes)):
try:
if i in self.command_frames:
self.commandframe(i)
playing_scene=i
scene_lenght=len(self.scenes[i].splitlines())
last_lenght=len(self.scenes[i].splitlines())
self.clear(nmode=nmode,scenelenght=scene_lenght,lastlenght=last_lenght)
#Sahneyi çiz
print(self.scenes[i])
time.sleep(self.sleep)
self.clear(nmode=nmode,scenelenght=scene_lenght,lastlenght=last_lenght)
Expand All @@ -206,10 +349,13 @@ def play(self,nmode=False):
else:
for i in range(len(self.scenes)):
try:
if i in self.command_frames:
self.commandframe(i)
playing_scene=i
scene_lenght=len(self.scenes[i].splitlines())
last_lenght=len(self.scenes[i].splitlines())
playing_scene=i
self.clear(nmode=nmode,scenelenght=scene_lenght,lastlenght=last_lenght)
#Sahneyi çiz
print(self.scenes[i])
self.clear(nmode=nmode,scenelenght=scene_lenght,lastlenght=last_lenght)
except KeyboardInterrupt:
Expand Down Expand Up @@ -255,22 +401,25 @@ def export_scenes_dir(self, dir, fileextension="txt"):
print(f"{dir} Dosya yolu bulunamadı")
else:
print("f{dir} File path not found")

def export_scenes(self, exportfile, encoding="utf8",type="normal",x=30,y=30):
"""export all scenes"""
with open(exportfile, "w", encoding=encoding) as exportfile:

if type=="asciimation":
exportfile.write(f"x:{x}\ny:{y}\nBEGIN\n")
for i in range(len(self.scenes)):
if i==0:
pass
else:exportfile.write("\nEND\n")
scene=self.scenes[i]
scene=scene.replace("END","end")
exportfile.write(scene)
for i in range(len(self.scenes)):
if i != 0:
exportfile.write("\nEND\n")
else: pass
scene=self.scenes[i]
scene=scene.replace("END","end")
exportfile.write(scene)
exportfile.write("\nEND")

if type=="normal":
for i in range(len(self.scenes)):
exportfile.write(",")
exportfile.write(self.scenes[i])

Loading

0 comments on commit 33db76b

Please sign in to comment.