-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
π feat(tabletron): add new function to replace terminalColumns
π¦ chore(package.json): add @types/node as a dev dependency π fix(index.ts): change default export to tabletron The new function tabletron replaces the old terminalColumns function. It has the same functionality but with a new name. The package.json file now includes @types/node as a dev dependency. The default export in index.ts is now tabletron instead of terminalColumns.
- Loading branch information
nyxb
committed
May 20, 2023
1 parent
8b684fe
commit 89039bc
Showing
7 changed files
with
254 additions
and
3 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,32 @@ | ||
/** | ||
* Demonstrates how 'auto' splits column widths by re-rendering on terminal resize. | ||
* | ||
* Run the example: | ||
* $ npx esno examples/auto-resize.ts | ||
*/ | ||
|
||
import { promisify } from 'node:util' | ||
import ansiEscapes from 'ansi-escapes' | ||
import color from '@nyxb/picocolors' | ||
import terminalColumns from '../src' | ||
|
||
const { red, blue, green } = color | ||
|
||
const tableData = [ | ||
[ | ||
red('A'.repeat(20)), | ||
blue('B'.repeat(30)), | ||
green('C'.repeat(40)), | ||
], | ||
] | ||
|
||
function renderTable() { | ||
const table = terminalColumns(tableData) | ||
process.stdout.write(ansiEscapes.clearTerminal + table) | ||
} | ||
|
||
process.stdout.on('resize', renderTable) | ||
renderTable() | ||
|
||
// Keep Node.js from exiting | ||
promisify(setTimeout)(60 * 60 * 1000) |
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,58 @@ | ||
/** | ||
* Demonstrates responsive behavior by faking the terminal width | ||
* (Demo in README) | ||
* | ||
* Run the example in 80x27: | ||
* $ npx esno examples/lorem-ipsum.ts | ||
*/ | ||
|
||
import ansiEscapes from 'ansi-escapes'; | ||
import { bold, underline, italic } from 'colorette'; | ||
import terminalColumns from '../src'; | ||
|
||
const tableData = [ | ||
[ | ||
italic('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Id neque aliquam vestibulum morbi blandit cursus risus at.'), | ||
underline('Sit amet luctus venenatis lectus magna. Nisi porta lorem mollis aliquam ut porttitor leo a. Sem integer vitae justo eget magna. Erat pellentesque adipiscing commodo elit.'), | ||
bold('Ultrices tincidunt arcu non sodales neque. Quis blandit turpis cursus in hac habitasse platea dictumst quisque. Libero enim sed faucibus turpis in eu mi bibendum neque.'), | ||
], | ||
]; | ||
|
||
const renderTable = (stdoutColumns: number) => { | ||
const table = terminalColumns(tableData, { | ||
stdoutColumns, | ||
columns: [ | ||
{ | ||
align: 'right', | ||
paddingRight: 4, | ||
paddingBottom: 1, | ||
}, | ||
{ | ||
paddingRight: 4, | ||
paddingBottom: 1, | ||
}, | ||
], | ||
}); | ||
|
||
process.stdout.write(`${ansiEscapes.clearTerminal + table}\n\n\n`); | ||
}; | ||
|
||
const stdoutWidth = process.stdout.columns; | ||
let tableWidth = process.stdout.columns; | ||
let movingDown = true; | ||
|
||
setInterval(() => { | ||
if (movingDown) { | ||
tableWidth -= 1; | ||
if (tableWidth === 30) { | ||
movingDown = false; | ||
} | ||
} else { | ||
tableWidth += 1; | ||
if (tableWidth === stdoutWidth) { | ||
movingDown = true; | ||
} | ||
} | ||
|
||
renderTable(tableWidth); | ||
}, 100); |
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,157 @@ | ||
/** | ||
* Demonstrates how to make a responsive table. | ||
* | ||
* Run the example: | ||
* $ npx esno examples/responsive-table.ts | ||
*/ | ||
import { promisify } from 'util'; | ||
import ansiEscapes from 'ansi-escapes'; | ||
import terminalColumns from '../src'; | ||
|
||
const tableData = [ | ||
[ | ||
'Jacky', | ||
'Mapp', | ||
'Georgian', | ||
'1992-08-09', | ||
], | ||
[ | ||
'Raphaela', | ||
'Gaddes', | ||
'Filipino', | ||
'1991-07-22', | ||
], | ||
[ | ||
'Mellie', | ||
'Hassey', | ||
'Dhivehi', | ||
'2000-02-06', | ||
], | ||
[ | ||
'Dru', | ||
'Clout', | ||
'Thai', | ||
'1997-09-17', | ||
], | ||
[ | ||
'Sig', | ||
'Evered', | ||
'Telugu', | ||
'1993-12-17', | ||
], | ||
[ | ||
'Velvet', | ||
'Gambrell', | ||
'Telugu', | ||
'1995-10-18', | ||
], | ||
[ | ||
'Alta', | ||
'Bagenal', | ||
'Thai', | ||
'1992-06-03', | ||
], | ||
[ | ||
'Jerrome', | ||
'Fosten', | ||
'Kashmiri', | ||
'2000-09-11', | ||
], | ||
[ | ||
'Derk', | ||
'Emons', | ||
'Ndebele', | ||
'1994-04-30', | ||
], | ||
[ | ||
'Glennis', | ||
'Patmore', | ||
'Swati', | ||
'2000-06-05', | ||
], | ||
]; | ||
|
||
function breakpoints(stdoutColumns: number) { | ||
// Large screens - auto | ||
if (stdoutColumns > 100) { | ||
return [ | ||
{ | ||
width: 'auto', | ||
paddingLeft: 2, | ||
paddingRight: 1, | ||
}, | ||
{ | ||
width: 'auto', | ||
paddingRight: 1, | ||
}, | ||
{ | ||
width: 'auto', | ||
paddingRight: 1, | ||
}, | ||
{ | ||
width: 'auto', | ||
paddingRight: 2, | ||
}, | ||
]; | ||
} | ||
|
||
// Smaller screens | ||
if (stdoutColumns > 30) { | ||
return [ | ||
{ | ||
width: '50%', | ||
paddingLeft: 2, | ||
paddingRight: 1, | ||
}, | ||
{ | ||
width: '50%', | ||
paddingRight: 2, | ||
}, | ||
{ | ||
width: '50%', | ||
paddingLeft: 2, | ||
paddingRight: 1, | ||
}, | ||
{ | ||
width: '50%', | ||
paddingRight: 2, | ||
paddingBottom: 1, | ||
}, | ||
]; | ||
} | ||
|
||
return { | ||
// Remove responsiveness | ||
stdoutColumns: 1000, | ||
columns: [ | ||
{ | ||
width: 'content-width', | ||
paddingLeft: 2, | ||
paddingRight: 1, | ||
}, | ||
{ | ||
width: 'content-width', | ||
paddingRight: 1, | ||
}, | ||
{ | ||
width: 'content-width', | ||
paddingRight: 1, | ||
}, | ||
{ | ||
width: 'content-width', | ||
paddingRight: 2, | ||
}, | ||
], | ||
}; | ||
} | ||
|
||
const renderTable = () => { | ||
const table = terminalColumns(tableData, breakpoints); | ||
process.stdout.write(ansiEscapes.clearTerminal + table); | ||
}; | ||
|
||
process.stdout.on('resize', renderTable); | ||
renderTable(); | ||
|
||
// Keep Node.js from exiting | ||
promisify(setTimeout)(60 * 60 * 1000); |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { terminalColumns } from './terminal-columns' | ||
import { tabletron } from './tabletron' | ||
import { breakpoints } from './breakpoints' | ||
import type { Options } from './types' | ||
|
||
export { | ||
terminalColumns as default, | ||
tabletron as default, | ||
breakpoints, | ||
Options, | ||
} |
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