Skip to content

Commit

Permalink
Merge pull request #272 from codefori/feature/formatted_json
Browse files Browse the repository at this point in the history
Formatted JSON columns
  • Loading branch information
worksofliam authored Sep 3, 2024
2 parents 1bbc180 + 6b372d1 commit 661328d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/views/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export function getHeader(options: {withCollapsed?: boolean} = {}): string {
#resultset td {
padding: 5px 15px;
}
#resultset tbody tr:hover {
background-color: var(--vscode-list-hoverBackground);
}
#resultset tbody tr {
border-bottom: 1px solid var(--vscode-activityBar-border);
Expand Down
22 changes: 18 additions & 4 deletions src/views/results/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,27 @@ export function generateScroller(basicSelect: string, isCL: boolean, withCancel?
// Insert a cell at the end of the row
var newCell = newRow.insertCell();
// Append a text node to the cell
//TODO: handle cell formatting here
var newDiv = document.createElement("div");
newDiv.className = "hoverable";
newDiv.appendChild(document.createTextNode(cell === undefined ? 'null' : cell));
// Append a formatted JSON object to the cell
const contentMightBeJson = typeof cell === 'string' && (cell.startsWith('{') || cell.startsWith('[')) && (cell.endsWith('}') || cell.endsWith(']'));
let isJson = false;
if (contentMightBeJson) {
try {
const cellJson = JSON.stringify(JSON.parse(cell), null, 2);
newDiv.style.whiteSpace = "pre";
newDiv.style["font-family"] = "monospace";
newDiv.innerText = cellJson;
isJson = true;
} catch (e) {}
}
if (!isJson) {
// Append a text node to the cell
newDiv.appendChild(document.createTextNode(cell === undefined ? 'null' : cell));
}
newCell.appendChild(newDiv);
}
}
Expand Down

0 comments on commit 661328d

Please sign in to comment.