Skip to content

Commit

Permalink
index latexmk / pdf generation output
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonstyle committed Feb 4, 2021
1 parent c981664 commit 823564b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
12 changes: 6 additions & 6 deletions src/command/render/latekmk/latex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import { basename, join } from "path/mod.ts";
import { existsSync } from "fs/mod.ts";

import { message } from "../../../core/console.ts";
import { message, messageFormatData } from "../../../core/console.ts";
import { dirAndStem } from "../../../core/path.ts";
import { execProcess, ProcessResult } from "../../../core/process.ts";

import { PdfEngine } from "../../../config/pdf.ts";
import { PackageManager } from "./pkgmgr.ts";
import { kPdfGenerateMessageOptions } from "./pdf.ts";
import { kLatexBodyMessageOptions, kLatexHeaderMessageOptions } from "./pdf.ts";

export interface LatexCommandReponse {
log: string;
Expand Down Expand Up @@ -165,15 +165,15 @@ async function runLatexCommand(
// Redirect stdoutput to stderr
const stdoutHandler = (data: Uint8Array) => {
if (!quiet) {
Deno.stderr.writeSync(data);
messageFormatData(data, kLatexBodyMessageOptions);
}
};

// Run the command
const runCmd = async () => {
const result = await execProcess(runOptions, undefined, stdoutHandler);
if (!quiet && result.stderr) {
message(result.stderr);
message(result.stderr, kLatexBodyMessageOptions);
}
return result;
};
Expand All @@ -195,7 +195,7 @@ async function runLatexCommand(
if (!quiet) {
message(
`command ${latexCmd} not found, attempting install`,
kPdfGenerateMessageOptions,
kLatexHeaderMessageOptions,
);
}

Expand All @@ -211,7 +211,7 @@ async function runLatexCommand(
// Some other error has occurred
message(
`Error executing ${latexCmd}`,
kPdfGenerateMessageOptions,
kLatexHeaderMessageOptions,
);

return Promise.reject();
Expand Down
19 changes: 10 additions & 9 deletions src/command/render/latekmk/pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ import {
needsRecompilation,
} from "./parse-error.ts";

export const kPdfGenerateMessageOptions = { bold: true };
export const kLatexHeaderMessageOptions = { bold: true };
export const kLatexBodyMessageOptions = { indent: 2 };

export async function generatePdf(mkOptions: LatexmkOptions) {
if (!mkOptions.quiet) {
message(
`runnning ${mkOptions.engine.pdfEngine} - 1`,
kPdfGenerateMessageOptions,
kLatexHeaderMessageOptions,
);
}

Expand Down Expand Up @@ -153,7 +154,7 @@ async function initialCompileLatex(
// First be sure all packages are up to date
if (!packagesUpdated) {
if (!quiet) {
message("updating existing packages", kPdfGenerateMessageOptions);
message("updating existing packages", kLatexHeaderMessageOptions);
}
await pkgMgr.updatePackages(true, false);
packagesUpdated = true;
Expand Down Expand Up @@ -213,7 +214,7 @@ async function makeIndexIntermediates(
const indexFile = join(dir, `${stem}.idx`);
if (existsSync(indexFile)) {
if (!quiet) {
message("\nmaking index", kPdfGenerateMessageOptions);
message("making index", kLatexHeaderMessageOptions);
}

// Make the index
Expand Down Expand Up @@ -286,7 +287,7 @@ async function makeBibliographyIntermediates(

if (existsSync(auxBibFullPath) && requiresProcessing) {
if (!quiet) {
message("\ngenerating bibliography", kPdfGenerateMessageOptions);
message("generating bibliography", kLatexHeaderMessageOptions);
}

// If natbib, only use bibtex, otherwise, could use biber or bibtex
Expand Down Expand Up @@ -380,7 +381,7 @@ async function findAndInstallPackages(
function writeError(primary: string, secondary?: string, logFile?: string) {
message(
`\ncompilation failed- ${primary}`,
kPdfGenerateMessageOptions,
kLatexHeaderMessageOptions,
);

if (secondary) {
Expand Down Expand Up @@ -411,16 +412,16 @@ async function recompileLatexUntilComplete(
if (!quiet) {
message(
`maximum number of runs (${maxRuns}) reached`,
kPdfGenerateMessageOptions,
kLatexHeaderMessageOptions,
);
}
break;
}

if (!quiet) {
message(
`\nrunning ${engine.pdfEngine} - ${runCount + 2}`,
kPdfGenerateMessageOptions,
`running ${engine.pdfEngine} - ${runCount + 2}`,
kLatexHeaderMessageOptions,
);
}

Expand Down
20 changes: 11 additions & 9 deletions src/command/render/latekmk/texlive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/
import { ld } from "lodash/mod.ts";

import { message } from "../../../core/console.ts";
import { message, messageFormatData } from "../../../core/console.ts";
import { execProcess } from "../../../core/process.ts";
import { kPdfGenerateMessageOptions } from "./pdf.ts";
import { kLatexBodyMessageOptions, kLatexHeaderMessageOptions } from "./pdf.ts";

const tlmgr = Deno.build.os === "windows"
? ["cmd.exe", "/c", "tlmgr"]
Expand Down Expand Up @@ -41,8 +41,8 @@ export async function findPackages(
for (const searchTerm of searchTerms) {
if (!quiet) {
message(
`\nfinding package for ${searchTerm}`,
kPdfGenerateMessageOptions,
`finding package for ${searchTerm}`,
kLatexHeaderMessageOptions,
);
}
// Special case for a known package
Expand Down Expand Up @@ -132,16 +132,18 @@ export async function installPackages(
) {
if (!quiet) {
message(
`${pkgs.length} ${pkgs.length === 1 ? "package" : "packages"} to install`,
kPdfGenerateMessageOptions,
`> ${pkgs.length} ${
pkgs.length === 1 ? "package" : "packages"
} to install`,
kLatexHeaderMessageOptions,
);
}
let count = 1;
for (const pkg of pkgs) {
if (!quiet) {
message(
`installing ${pkg} (${count} of ${pkgs.length})`,
kPdfGenerateMessageOptions,
`> installing ${pkg} (${count} of ${pkgs.length})`,
kLatexHeaderMessageOptions,
);
}

Expand Down Expand Up @@ -248,7 +250,7 @@ function tlmgrCommand(
undefined,
(data: Uint8Array) => {
if (!quiet) {
Deno.stderr.writeSync(data);
messageFormatData(data, kLatexBodyMessageOptions);
}

if (stdout) {
Expand Down
21 changes: 21 additions & 0 deletions src/core/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ export function message(line: string, options?: MessageOptions) {
}
Deno.stderr.writeSync(new TextEncoder().encode(line + (newline ? "\n" : "")));
}

export function messageFormatData(data: Uint8Array, options?: MessageOptions) {
const decoder = new TextDecoder("utf8");
const encoder = new TextEncoder();

const { newline = true, bold = false, indent = 0 } = options || {};
let output = decoder.decode(data);
if (indent) {
const pad = " ".repeat(indent);
output = output
.split(/\r?\n/)
.map((output) => pad + output)
.join("\n");
}
if (bold) {
output = colors.bold(output);
}

Deno.stderr.writeSync(encoder.encode(output + (newline ? "\n" : "")));
}

export function writeFileToStdout(file: string) {
const df = Deno.openSync(file, { read: true });
const contents = Deno.readAllSync(df);
Expand Down

0 comments on commit 823564b

Please sign in to comment.