table-kit.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 colWidth = 30
  13. const padTitles = this.titles.map((title) => {
  14. return title.padEnd(colWidth, ' ')
  15. })
  16. const titleLine = (() => {
  17. let titleLine = ''
  18. for (const padTitle of padTitles) {
  19. titleLine = titleLine.concat(this.tableCharacter).concat(' ').concat(padTitle)
  20. }
  21. return titleLine.concat(this.tableCharacter)
  22. })()
  23. console.clear()
  24. console.info(this.splitLine)
  25. console.info(titleLine)
  26. console.info(this.splitLine)
  27. }
  28. showLine(rows=[], fileLogger=undefined) {
  29. const padRows = rows.map((col) => {
  30. return col.padEnd(this.colWidth, ' ')
  31. })
  32. const infoLine = (() => {
  33. let infoLine = ''
  34. for (const padRow of padRows) {
  35. infoLine = infoLine.concat(this.tableCharacter).concat(' ').concat(padRow)
  36. }
  37. return infoLine.concat(this.tableCharacter)
  38. })()
  39. console.info(infoLine)
  40. if (fileLogger) fileLogger.info(infoLine)
  41. }
  42. printEndLine(logger=undefined) {
  43. console.info(this.splitLine)
  44. if (logger) logger.info('')
  45. }
  46. }