forked from AlejandroDiBattista/CYPSoft-Prog3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
110 lines (83 loc) · 3.21 KB
/
utils.js
File metadata and controls
110 lines (83 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import fs from 'fs'
import chalk from 'chalk'
/// Configuración
const ANCHO = 80
const PROMPT = '=> '
const SEPARADOR = '// '
/// Funciones auxiliares
const rellenar = (texto, cantidad, relleno = ' ') =>
texto.toString().slice(0, cantidad).padEnd(cantidad, relleno)
const centrar = (texto, cantidad = ANCHO, relleno = ' ') =>
relleno.repeat(cantidad / 2 - texto.length / 2 - 6) + `|| ${texto.toUpperCase()} ||`
const espacios = (texto) =>
texto.replace(/\s+/g, ' ').trim()
const extraerCodigo = (linea) => {
const dentro = /^\s*\w+\((?<codigo>.*?)\)[ ;]*\s*(?<comentario>\/\/.*)?\s*$/;
const antes = /^\s*(?<codigo>.*?)[ ;]*\s*(?<comentario>\/\/.*)?\s*$/;
const coincidencias = linea.match(dentro) ?? linea.match(antes)
const { groups: { codigo, comentario } } = coincidencias ?? { groups: { codigo: '', comentario: '' } }
return { codigo, comentario }
}
const extraerLlamada = (nivel = 3) => {
const e = new Error().stack.split('\n')[nivel].split(/[:]/)
let [nombre, numero] = e.slice(-3, -1)
numero = parseInt(numero)
return { nombre, numero }
}
let lineas = []
const leerLineas = (origen) => fs.readFileSync(origen, 'utf8').split('\r\n');
let secciones = []
const agregar = (linea) => {
if (seccion.length) {
secciones.at(-1).push(linea)
} else {
console.log(linea)
}
}
/// API ///
function comenzar(titulo = 'Demostración', nivel=3) {
const { nombre } = extraerLlamada(nivel)
lineas = leerLineas(nombre)
console.clear()
console.log(chalk.bold.red(centrar(titulo)))
}
function seccion(titulo="") {
secciones.push([])
agregar(chalk.red(`\n\n~~~ ${titulo} ~~~`))
}
function ver(expresion) {
comenzar("",4)
const conExpresion = arguments.length > 0
const { numero } = extraerLlamada()
const linea = lineas[numero - (conExpresion ? 1 : 2)]
const { codigo, comentario } = extraerCodigo(linea)
agregar('')
if (comentario) {
agregar(chalk.italic.green(espacios(comentario)))
}
agregar(chalk.cyan(`${rellenar(codigo, ANCHO - 9)} `) + chalk.gray(SEPARADOR + rellenar(numero, 3)))
if (conExpresion) {
let texto = expresion
if (typeof expresion === 'undefined') texto = 'undefined'
if (typeof expresion === 'string') texto = `"${expresion}"`
if (typeof expresion === 'function') texto = 'function'
if (typeof expresion === 'object' && expresion !== null) texto = JSON.stringify(expresion)
let tipo = Array.isArray(expresion) ? `[${typeof expresion[0]}]` : typeof expresion
agregar(chalk.gray(PROMPT) + chalk.yellow(`${rellenar(texto, ANCHO - PROMPT.length - 9)} `) + chalk.gray(SEPARADOR + tipo))
}
}
function terminar(mostrar = -1) {
let salida = secciones
if (typeof mostrar === 'number') {
salida = [secciones.at(mostrar)];
} else if (typeof mostrar === 'string') {
salida = secciones.filter(seccion => seccion.at(0).toLowerCase().includes(mostrar.toLowerCase()))
}
for (const seccion of salida) {
for (const linea of seccion) {
console.log(linea)
}
}
console.log(chalk.red('\n||'))
}
export { comenzar, seccion, ver, terminar, ver as v, terminar as fin }