Skip to content

Commit

Permalink
Fix typing syntax for earlier python versions, add examples tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonscript committed Apr 3, 2024
1 parent 240a96d commit c5dce56
Show file tree
Hide file tree
Showing 7 changed files with 322 additions and 226 deletions.
14 changes: 10 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"configurations": [
{
"name": "Basic examples",
"type": "python",
"type": "debugpy",
"request": "launch",
"program": "examples/basic_example.py",
"cwd": "${workspaceFolder}",
Expand All @@ -18,7 +18,7 @@
},
{
"name": "Complete example set",
"type": "python",
"type": "debugpy",
"request": "launch",
"program": "examples/complete_example.py",
"cwd": "${workspaceFolder}",
Expand All @@ -29,15 +29,21 @@
"justMyCode": true
},
{
"name": "Run Tinta tests",
"type": "python",
"name": "Run Tinta tests (workspace python)",
"type": "debugpy",
"request": "launch",
"console": "integratedTerminal",
"module": "pytest",
"env": {
"_PYTEST_RAISE": "1"
},
"args": ["-xv"]
},
{
"name": "Run Tinta tests (all versions)",
"type": "debugpy",
"preLaunchTask": "Run All Pytest Versions",
"request": "launch"
}
]
}
253 changes: 137 additions & 116 deletions examples/basic_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,119 +29,140 @@
from pathlib import Path

sys.path.append(str(Path().cwd().parent / "tinta"))
# pylint: disable=wrong-import-position, wrong-import-order, import-error
from tinta import Tinta # noqa: E402

# End import shim

Tinta.load_colors("examples/colors.ini")

# The most basic example we can get.
Tinta("That's a really nice car!").print()

# Prints the entire string in red.
Tinta().red("That's a really nice red car!").print()

# Prints the first half in blue, then the rest in red.
Tinta().blue("That's a cool blue car").red("but not as cool as my red one").print()

# Prints the first few words in green, separated by _*_,
# then the final word in purple.
Tinta().green(
"Sometimes", "We", "Want", "To", "Join", "Words", "Differently", sep=" * "
).purple("Neat!").print()

# Here we underline a word.
Tinta().gray("It's").underline("really").normal(
"important to be able to style things."
).print()

# Here we tried to print in pink, but used the plaintext arg in print.
# You'll notice we still support Python's multiline \ feature.
Tinta().pink(
"But it's equally important to be able " "to print things in plaintext, too"
).print(plaintext=True)

# Let's try some f-strings.
animal = "Tiger"
Tinta().orange(f"Hey, we support f-strings, too! Raaarrr, said Ms. {animal}.")

# And dimming some text.
Tinta().blue("We can").dim("dim").normal("things").print()

# Things getting out of hand? You can break them up easily in multiple
# lines, without having to fiddle with \.
tint = Tinta()
tint.push("Sometimes we need to")
tint.pink("break up long lines of text")
tint.gray("to make them easier to read.")
tint.line("We can even write to a new line!")
tint.print()

# You could do the same using multiple segments, or ()
Tinta().vanilla(
"I like ice cream", "it comes in all sorts" "of great and yummy flavors."
).print()

(Tinta().vanilla("I like ice cream").red("especially with cherries on top.").print())

# When you're done printing, Tinta resets itself, but you can still
# reuse the original variable.
tint.push("After a print, Tinta resets itself").green()
tint.line("but you can still use the same initialized version.")
tint.print()

# Using native print()'s built-in end, we can terminate a string
# without a newline.
Tinta("And of course as always,").print(end="")
Tinta(" you can print with end=''").print()

# Not enough colors in config.yaml? Add your own on the fly!
Tinta(
"Did you know, you can", "write with ansi codes directly, too?", color=127
).print()

# Have some fun with separators.
Tinta("A bird", "I like birds", sep="; ").push(
"And also cats", "and dogs", sep=" "
).print(sep="\n")

# You could get really fancy and inject some formatted text in the middle,
# using f-strings.
(
Tinta()
.mint("Fate.")
.dark_gray("It protects")
.underline()
.blue(f"fools{Tinta().normal().dark_gray(',').to_str()}", sep="")
.normal()
.pink("little children,")
.dark_gray("and ships named")
.purple("Enterprise.")
.print()
)

# Tinta is also smart about how we join things together. If you join
# several objects together, it collapses repeated whitespace. You
# can also use 'sep' to force sections to collapse.
t = (
Tinta()
.pink("A section")
.push()
.white()
.blue("of text", sep="")
.green(",")
.push()
.purple("separated.")
)
t.print()

# And finally, you can use some helper tools to clear the current
# console and move up a line.
Tinta().yellow("Loading...").print()
time.sleep(1)
Tinta.clearline()
Tinta().green("Done").print()
time.sleep(1)
Tinta.up()
Tinta().green("Done :)").print()


def basic():
# pylint: disable=wrong-import-position, wrong-import-order, import-error
from tinta import Tinta # noqa: E402

# End import shim

Tinta.load_colors("examples/colors.ini")

# The most basic example we can get.
Tinta("That's a really nice car!").print()

# Prints the entire string in red.
Tinta().red("That's a really nice red car!").print()

# Prints the first half in blue, then the rest in red.
Tinta().blue("That's a cool blue car").red("but not as cool as my red one").print()

# Prints the first few words in green, separated by _*_,
# then the final word in purple.
Tinta().green(
"Sometimes", "We", "Want", "To", "Join", "Words", "Differently", sep=" * "
).purple("Neat!").print()

# Here we underline a word.
Tinta().gray("It's").underline("really").normal(
"important to be able to style things."
).print()

# Here we tried to print in pink, but used the plaintext arg in print.
# You'll notice we still support Python's multiline \ feature.
Tinta().pink(
"But it's equally important to be able " "to print things in plaintext, too"
).print(plaintext=True)

# Let's try some f-strings.
animal = "Tiger"
Tinta().orange(f"Hey, we support f-strings, too! Raaarrr, said Ms. {animal}.")

# And dimming some text.
Tinta().blue("We can").dim("dim").normal("things").print()

# Things getting out of hand? You can break them up easily in multiple
# lines, without having to fiddle with \.
tint = Tinta()
tint.push("Sometimes we need to")
tint.pink("break up long lines of text")
tint.gray("to make them easier to read.")
tint.nl("We can even write to a new line!")
tint.print()

# You could do the same using multiple segments, or ()
Tinta().vanilla(
"I like ice cream", "it comes in all sorts" "of great and yummy flavors."
).print()

(
Tinta()
.vanilla("I like ice cream")
.red("especially with cherries on top.")
.print()
)

# When you're done printing, Tinta resets itself, but you can still
# reuse the original variable.
tint.push("After a print, Tinta resets itself").green()
tint.nl("but you can still use the same initialized version.")
tint.print()

# Using native print()'s built-in end, we can terminate a string
# without a newline.
Tinta("And of course as always,").print(end="")
Tinta(" you can print with end=''").print()

# Not enough colors in config.yaml? Add your own on the fly!
Tinta(
"Did you know, you can", "write with ansi codes directly, too?", color=127
).print()

# Get the string representation of the Tinta object with to_str().
Tinta("Sometimes you just want to get the string").to_str()
Tinta().purple("Which").pink("is").green("pretty").blue("cool").to_str()
Tinta().vanilla("If you need it in plaintext, you can do that, too.").to_str(
plaintext=True
)
t = Tinta().mint("⭐like this⭐")
print(f"You can also use a Tinta object in an f-string directly, {t}")

# Have some fun with separators.
Tinta("A bird", "I like birds", sep="; ").push(
"And also cats", "and dogs", sep=" "
).print(sep="\n")

# You could get really fancy and inject some formatted text in the middle,
# using f-strings.
(
Tinta()
.mint("Fate.")
.dark_gray("It protects")
.underline()
.blue(f"fools{Tinta().normal().dark_gray(',').to_str()}", sep="")
.normal()
.pink("little children,")
.dark_gray("and ships named")
.purple("Enterprise.")
.print()
)

# Tinta is also smart about how we join things together. If you join
# several objects together, it collapses repeated whitespace. You
# can also use 'sep' to force sections to collapse.
t = (
Tinta()
.pink("A section")
.push()
.white()
.blue("of text", sep="")
.green(",")
.push()
.purple("separated.")
)
t.print()

# And finally, you can use some helper tools to clear the current
# console and move up a line.
Tinta().yellow("Loading...").print()
time.sleep(1)
Tinta.clearline()
Tinta().green("Done").print()
time.sleep(1)
Tinta.up()
Tinta().green("Done :)").print()


if __name__ == "__main__":
basic()
Loading

0 comments on commit c5dce56

Please sign in to comment.