Advertisement
tofu_ink

Untitled

Mar 10th, 2025 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. issues with this are sometimes, the terminal doesn't update, so you have to run it again.
  3. The terminal has a buffer, so when you go to a different area and come back,
  4. it re-renders from the buffer so the table will not be there
  5. */
  6. var default_options = {
  7.   color: '#CAA243',
  8.   border: '#888'
  9. };
  10. export function table(list, headers, passed_options) {
  11.   if (list.length == 0)
  12.     return;
  13.   if (!passed_options)
  14.     passed_options = {};
  15.   var options = {...default_options, ...passed_options}
  16.   var has_headers = headers ? true : false;
  17.   headers = headers? headers : Object.keys(list[0]);
  18.   var style_header = ' style="padding: 2px 7px; text-align: center;';
  19.   var style_cell   = ' style="padding: 2px 7px; text-align: right;';
  20.   var html = `<table style="color: ${options.color};margin-right: auto; border-collapse: collapse; border: 1px solid ${options.border};"><thead><tr>`;
  21.   for (var i = 0; i < headers.length; ++i)
  22.     html += '<td ' + style_header + '">'+(has_headers ? headers[i].t : headers[i])+'</td>';
  23.   html += '</tr></thead><tbody>';
  24.  
  25.   for (var i = 0; i < list.length; ++i) {
  26.     var ex_style = list[i].style ? list[i].style : '';
  27.     if (!has_headers)
  28.       html += '<tr><td' + style_cell + ex_style +'">' + Object.values(list[i]).join('</td><td' + style_cell + ex_style +'">') + '</td></tr>'
  29.     else {
  30.       html += '<tr>';
  31.       for (var m = 0; m < headers.length; ++m) {
  32.         html += '<td' + style_cell + ex_style +'">' + list[i][headers[m].c] + '</td>';
  33.       }
  34.       html += '</tr>';
  35.     }
  36.   }
  37.   html += '</tbody></table><br />';
  38.  
  39.   eval('document').getElementById("terminal").insertAdjacentHTML("beforeend",
  40.     `<li class="jss44 MuiListItem-root MuiListItem-gutters MuiListItem-padding css-1578zj2">
  41.         <p class="jss92 MuiTypography-root MuiTypography-body1 css-cxl1tz">${html}</p></li>`);
  42. }
  43.  
  44.  
  45.  
  46.  
  47. /* how to use the code */
  48.     var current_list = [];
  49.     for (var i = 0; i < data.Cities.length; ++i) {
  50.         var city = {...data.Cities[i]};
  51.         current_list.push(city);
  52.         city.communities = format(city.communities);
  53.         city.population = format(city.population);
  54.         city.chaos = format(city.chaos);
  55.         if (city.name == data.city)
  56.             city.style=";color:#11874B;";
  57.     }
  58.    
  59.     table(current_list, [{t:'City',c:'name'}, {t:'Communities',c:'communities'}, {t:'Population',c:'population'}, {t:'Choas',c:'chaos'}]);
  60.  
  61.  
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement