table-kit.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. module.exports = class TableKit {
  2. titles
  3. colWidth
  4. tableCharacter
  5. constructor(titles, colWidth=30, tableCharacter='#') {
  6. this.titles = titles
  7. this.colWidth = colWidth
  8. this.tableCharacter = tableCharacter
  9. this.splitLine = ''.padEnd((colWidth + 2) * this.titles.length + 1, this.tableCharacter)
  10. }
  11. printTitles() {
  12. const padTitles = this.titles.map((title) => {
  13. return title.padEnd(this.colWidth, ' ')
  14. })
  15. const titleLine = (() => {
  16. let titleLine = ''
  17. for (const padTitle of padTitles) {
  18. titleLine = titleLine.concat(this.tableCharacter).concat(' ').concat(padTitle)
  19. }
  20. return titleLine.concat(this.tableCharacter)
  21. })()
  22. console.clear()
  23. console.info(this.splitLine)
  24. console.info(titleLine)
  25. console.info(this.splitLine)
  26. }
  27. showLine(rows=[], fileLogger=undefined) {
  28. const padRows = rows.map((col) => {
  29. return col.padEnd(this.colWidth, ' ')
  30. })
  31. const infoLine = (() => {
  32. let infoLine = ''
  33. for (const padRow of padRows) {
  34. infoLine = infoLine.concat(this.tableCharacter).concat(' ').concat(padRow)
  35. }
  36. return infoLine.concat(this.tableCharacter)
  37. })()
  38. console.info(infoLine)
  39. if (fileLogger) fileLogger.info(infoLine)
  40. }
  41. printEndLine() {
  42. console.info(this.splitLine)
  43. }
  44. }