Skip to content
Myr

:sparkles: Update Termynal modifications, update first steps to use it

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Make Termynal have fixed sizes, add "fast" button, use it in first steps

authored by Sebastián Ramírez · committed by Sebastián Ramírez · parent e91c5a4 · Browse files · 4 files+244 added−194 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+244 added−194 removed

  • other:+244 added−194 removed

4 files+244 added−194 removed

modified:docs/css/termynal.css+6 added
modified:docs/js/custom.js+72 added-57 removed
modified:docs/js/termynal.js+71 added-7 removed
28 unmodified lines
29 */30 constructor(container = '#termynal', options = {}) {31 this.container = (typeof container === 'string') ? document.querySelector(container) : container;32 this.pfx = `data-${options.prefix || 'ty'}`;33 this.startDelay = options.startDelay33 this.originalStartDelay = this.startDelay = options.startDelay34 || parseFloat(this.container.getAttribute(`${this.pfx}-startDelay`)) || 600;35 this.typeDelay = options.typeDelay35 this.originalTypeDelay = this.typeDelay = options.typeDelay36 || parseFloat(this.container.getAttribute(`${this.pfx}-typeDelay`)) || 90;37 this.lineDelay = options.lineDelay37 this.originalLineDelay = this.lineDelay = options.lineDelay38 || parseFloat(this.container.getAttribute(`${this.pfx}-lineDelay`)) || 1500;39 this.progressLength = options.progressLength40 || parseFloat(this.container.getAttribute(`${this.pfx}-progressLength`)) || 40;41 this.progressChar = options.progressChar
2 unmodified lines
44 || parseFloat(this.container.getAttribute(`${this.pfx}-progressPercent`)) || 100;45 this.cursor = options.cursor46 || this.container.getAttribute(`${this.pfx}-cursor`) || '▋';47 this.lineData = this.lineDataToElements(options.lineData || []);48 this.loadLines()49 if (!options.noInit) this.init()50 }5152 loadLines() {53 // Load all the lines and create the container so that the size is fixed54 // Otherwise it would be changing and the user viewport would be constantly55 // moving as she/he scrolls56 const finish = this.generateFinish()57 finish.style.visibility = 'hidden'58 this.container.appendChild(finish)59 // Appends dynamically loaded lines to existing line elements.60 this.lines = [...this.container.querySelectorAll(`[${this.pfx}]`)].concat(this.lineData);61 for (let line of this.lines) {62 line.style.visibility = 'hidden'63 this.container.appendChild(line)64 }65 const restart = this.generateRestart()66 restart.style.visibility = 'hidden'67 this.container.appendChild(restart)68 this.container.setAttribute('data-termynal', '');69 }7071 /**72 * Initialise the widget, get lines, clear container and start animation.73 */74 init() {55 // Appends dynamically loaded lines to existing line elements.56 this.lines = [...this.container.querySelectorAll(`[${this.pfx}]`)].concat(this.lineData);5775 /** 76 * Calculates width and height of Termynal container.77 * If container is empty and lines are dynamically loaded, defaults to browser `auto` or CSS.78 */
4 unmodified lines
83 containerStyle.height : undefined;8485 this.container.setAttribute('data-termynal', '');86 this.container.innerHTML = '';87 for (let line of this.lines) {88 line.style.visibility = 'visible'89 }90 this.start();91 }9293 /**94 * Start the animation and rener the lines depending on their data attributes.95 */96 async start() {97 this.addFinish()98 await this._wait(this.startDelay);99100 for (let line of this.lines) {101 const type = line.getAttribute(this.pfx);
16 unmodified lines
118 }119120 line.removeAttribute(`${this.pfx}-cursor`);121 }122 this.addRestart()123 this.finishElement.style.visibility = 'hidden'124 this.lineDelay = this.originalLineDelay125 this.typeDelay = this.originalTypeDelay126 this.startDelay = this.originalStartDelay127 }128129 generateRestart() {130 const restart = document.createElement('a')131 restart.onclick = (e) => {132 e.preventDefault()133 this.container.innerHTML = ''134 this.init()135 }136 restart.href = '#'137 restart.setAttribute('data-terminal-control', '')138 restart.innerHTML = "restart \u27f3" // Refresh emoji139 return restart140 }141 142 generateFinish() {143 const finish = document.createElement('a')144 finish.onclick = (e) => {145 e.preventDefault()146 this.lineDelay = 0147 this.typeDelay = 0148 this.startDelay = 0149 }150 finish.href = '#'151 finish.setAttribute('data-terminal-control', '')152 finish.innerHTML = "fast \u2b95" // Fast emoji arrow153 this.finishElement = finish154 return finish155 }156157 addRestart() {158 const restart = this.generateRestart()159 this.container.appendChild(restart)160 }161162 addFinish() {163 const finish = this.generateFinish()164 this.container.appendChild(finish)165 }166167 /**168 * Animate a typed line.169 * @param {Node} line - The line element to render.170 */171 async type(line) {172 const chars = [...line.textContent];109 const delay = line.getAttribute(`${this.pfx}-typeDelay`) || this.typeDelay;173 line.textContent = '';174 this.container.appendChild(line);175176 for (let char of chars) {177 const delay = line.getAttribute(`${this.pfx}-typeDelay`) || this.typeDelay;178 await this._wait(delay);179 line.textContent += char;180 }181 }
85 unmodified lines
modified:docs/tutorial/first-steps.md+95 added-130 removed
8 unmodified lines
9Copy that to a file `main.py`.1011Test it:1213```bash14python main.py15```13<div class="termy">1417You will see an output like:15```console16$ python main.py1719```hl_lines=""18Hello World21```1923It just prints "Hello World".20// It just prints "Hello World".2125If you type:22// Now check the --help23$ python main.py --help2427```bash28python main.py --help29```3031it shows:3233```25Usage: main.py [OPTIONS]2627Options:28 --help Show this message and exit.29```3040...but this program is still not very useful. Let's increase that.31</div>3233...but this program is still not very useful. Let's extend it.3435## What is a **CLI argument**3637Here we will use the word **CLI argument** to refer to **CLI parameters** passed in some specific order to the CLI application. By default, they are *required*.3839If you go to your terminal and type:4041<div class="termy">4243```bash49ls ./myproject44$ ls ./myproject4546first-steps.md intro.md47```4849</div>5051`ls` will show the contents of the directory `./myproject`.5254* `ls` is the *command* (or "program", "CLI app").55* `./myproject` is a *CLI argument*.53* `ls` is the *program* (or "command", "CLI app").54* `./myproject` is a *CLI argument*, in this case it refers to the path of a directory.5556They are a bit different from **CLI options** that you will see later below.5758## Add a CLI argument
3 unmodified lines
62```Python hl_lines="4 5"63{!./src/first_steps/tutorial002.py!}64```6567If you run it without the argument:66<div class="termy">6769```bash70python main.py71```68```console6973it shows:70$ python main.py7175```72// If you run it without the argument, it shows a nice error7374Usage: main.py [OPTIONS] NAME75Try "main.py --help" for help.7677Error: Missing argument "NAME".80```7882Now pass that `name` *CLI argument*:79// Now pass that NAME CLI argument80$ python main.py Camila8184```bash85python main.py Camila86```82Hello Camila8388it shows:8984// Here "Camila" is the CLI argument85```91Hello Camila92```8694Here `Camila` is the *CLI argument*.87</div>8889## Two CLI arguments9091Now extend that to have 2 arguments, `name` and `lastname`:
1 unmodified line
93```Python hl_lines="4 5"94{!./src/first_steps/tutorial003.py!}95```96104Now get the help:97<div class="termy">98106```bash107python main.py --help108```99```console100// Check the main --help101$ python main.py --help102110it shows:111112```103Usage: main.py [OPTIONS] NAME LASTNAME104105Options:106 --help Show this message and exit.117```107119There are now 2 *CLI arguments*, `name` and `lastname`.108// There are now 2 CLI arguments, name and lastname109121Now pass a single `name` argument:110// Now pass a single name argument111$ python main.py Camila112123```bash124python main.py Camila125```126127it shows:128129```113Usage: main.py [OPTIONS] NAME LASTNAME114Try "main.py --help" for help.115116Error: Missing argument "LASTNAME".134```117136These 2 arguments are required, so, pass both:118// These 2 arguments are required, so, pass both:119$ python main.py Camila Gutiérrez120138```bash139python main.py Camila Gutiérrez140```141142it shows:143144```121Hello Camila Gutiérrez122```123124</div>125126!!! tip127 Notice that the order is important. The last name has to go after the first name.128129 If you called it with:130153 ```bash154 python main.py Gutiérrez Camila131 ```132 $ python main.py Gutiérrez Camila133 ```134135 your app wouldn't have a way to know which is the `name` and which the `lastname`. It expects the first *CLI argument* to be the `name` and the second *CLI argument* to be the `lastname`.136137## What is a **CLI option**138139Here we will use the word **CLI option** to refer to *CLI parameters* passed to the CLI application with a specific name. For example, if you go to your terminal and type:140141<div class="termy">142163```bash164ls ./myproject --size143```console144$ ls ./myproject --size14514612 first-steps.md 4 intro.md147```148149</div>150151`ls` will show the contents of the directory `./myproject` with their `size`.152169* `ls` is the *command* (or "program", "CLI app").153* `ls` is the *program* (or "command", "CLI app").154* `./myproject` is a *CLI argument*.155* `--size` is an optional *CLI option*.156157The program knows it has to show the size because it sees `--size`, not because of the order.
1 unmodified line
159A *CLI option* like `--size` doesn't depend on the order like a *CLI argument*.160161So, if you put the `--size` *before* the *CLI argument*, it still works:162163<div class="termy">164179```bash180ls --size ./myproject165```console166$ ls --size ./myproject16716812 first-steps.md 4 intro.md169```170171</div>172173The main visual difference between a *CLI option* and and a *CLI argument* is that the *CLI option* has `--` prepended to the name, like in "`--size`".174175A *CLI option* doesn't depend on the order because it has a predefined name (here it's `--size`). This is because the CLI app is looking specifically for a literal `--size` parameter or "flag", with that specific "name" (here the specific name is "`--size`"). The CLI app will check if you typed it or not, it will be actively looking for `--size` even if you didn't type it (to check if it's there or not).176
17 unmodified lines
194```195196Here `formal` is a `bool` that is `False` by default.197208If you get the help:198<div class="termy">199210```bash211python main.py --help212```200```console201// Get the help202$ python main.py --help203214it shows:215216```204Usage: main.py [OPTIONS] NAME LASTNAME205206Options:207 --formal / --no-formal208 --help Show this message and exit.209```210211</div>212213!!! tip214 Notice that it automatically creates a `--formal` and a `--no-formal` because it detected that `formal` is a `bool`.215216Now call it normally:217229```bash230python main.py Camila Gutiérrez231```218<div class="termy">219233it still shows:220```console221$ python main.py Camila Gutiérrez222235```223Hello Camila Gutiérrez237```224239But if you pass `--formal`:225// But if you pass --formal226$ python main.py Camila Gutiérrez --formal227241```bash242python main.py Camila Gutiérrez --formal243```228Good day Ms. Camila Gutiérrez.229245it shows:230// And as --formal is a CLI option you can put it anywhere in this command231$ python main.py Camila --formal Gutiérrez232247```233Good day Ms. Camila Gutiérrez.249```234251And as `formal` is a *CLI option*, you can put it anywhere in this command:235$ python main.py --formal Camila Gutiérrez236253```bash254python main.py Camila --formal Gutiérrez255```256257and:258259```bash260python main.py --formal Camila Gutiérrez261```262263both still show:264265```237Good day Ms. Camila Gutiérrez.238```239240</div>241242## A *CLI option* with a value243244To convert the `lastname` from a *CLI argument* to a *CLI option*, give it a default value of `""`:245
2 unmodified lines
248```249250As `lastname` now has a default value of `""` (an empty string) it is no longer required in the function, and **Typer** will now by default make it an optional *CLI option*.251279If you get the help:252<div class="termy">253281```bash282python main.py --help283```254```console255$ python main.py --help256285it now shows:286287```257Usage: main.py [OPTIONS] NAME258259Options:260 --lastname TEXT261 --formal / --no-formal262 --help Show this message and exit.263```264265</div>266267!!! tip268 Notice the `--lastname`, and notice that it takes a textual value.269270 A *CLI option* with a value like `--lastname` (contrary to a *CLI option* without a value, a `bool` flag, like `--formal` or `--size`) takes as its value whatever is at the *right side* of the *CLI option*.271301If you call it without a `--lastname`:272<div class="termy">273303```bash304python main.py Camila305```274```console275// Call it without a --lastname276$ python main.py Camila277307it shows:308309```278Hello Camila311```279313and you can pass the `--lastname`:280// Pass the --lastname281$ python main.py Camila --lastname Gutiérrez282315```bash316python main.py Camila --lastname Gutiérrez317```318319and it shows:320321```283Hello Camila Gutiérrez284```285286</div>287288!!! tip289 Notice that "`Gutiérrez`" is at the right side of `--lastname`. A *CLI option* with a value takes as its value whatever is at the *right side*.290291And as `--lastname` is now a *CLI option* that doesn't depend on the order, you can pass it before the name:292330```bash331python main.py --lastname Gutiérrez Camila332```293<div class="termy">294334and it will still work normally:295```console296$ python main.py --lastname Gutiérrez Camila297336```298// and it will still work normally299300Hello Camila Gutiérrez301```302303</div>304305## Arguments, options, parameters, optional, required306307Be aware that these terms refer to multiple things depending on the context, and sadly, those "contexts" mix frequently, so it's easy to get confused.308
65 unmodified lines