Skip to content

Commit 3523a95

Browse files
authored
Merge pull request #63 from darkriszty/fb/column-padding
Fb/column padding
2 parents 4c53784 + c2e933c commit 3523a95

23 files changed

Lines changed: 1552 additions & 363 deletions

README.md

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ A VSCode command called `Prettify markdown tables` is also available to format t
2929
### Configurable settings:
3030
- The maximum texth length of a selection/entire document to consider for formatting. Default: 1M chars (limit does not apply from CLI or NPM).
3131
- Additional languages to support formatting for besides `markdown`. See possible configurable values [here](https://code.visualstudio.com/docs/languages/identifiers#_known-language-identifiers). Default: `[ ]`.
32-
- Keyboard shortcut to prettify the currently opened markdown document. Default: CTRL+ALT+M (CMD+ALT+M on Mac).
32+
- Column padding to make the columns more spaced out from each other. Default: `0` (no extra spacing/padding).
33+
- Keyboard shortcut to prettify the currently opened markdown document. Default: <kbd>CTRL</kbd>+<kbd>ALT</kbd>+<kbd>M</kbd> (<kbd>CMD</kbd>+<kbd>ALT</kbd>+<kbd>M</kbd> on Mac).
3334

3435
## NPM
3536

@@ -46,42 +47,51 @@ console.log(CliPrettify.prettify(
4647
`hello|world
4748
-|-
4849
foo|bar`));
49-
5050
/* Output:
5151
hello | world
5252
------|------
5353
foo | bar
5454
*/
55-
```
5655

57-
## Docker
56+
// specifying a column padding
57+
console.log(CliPrettify.prettify(
58+
`hello|world
59+
-|-
60+
foo|bar`, { columnPadding: 1 }));
61+
/* Output:
62+
hello | world
63+
------ | ------
64+
foo | bar
65+
*/
5866

59-
The core formatting logic is available as a node docker image: `docker pull darkriszty/prettify-md`.
67+
```
6068

61-
Available features from docker:
62-
- To prettify a file: `docker container run -i darkriszty/prettify-md < input.md`.
63-
- To prettify a file and save the output: `docker container run -i darkriszty/prettify-md < input.md > output.md`.
64-
- To check whether a file is prettyfied or not: `docker container run -i darkriszty/prettify-md --check < input.md`. This will fail with an exception and return code `1` if the file is not prettyfied.
69+
## Docker & CLI
6570

66-
## CLI
71+
The core formatting logic is available as a node docker image: `docker pull darkriszty/prettify-md` or as a stand alone CLI tool.
6772

68-
Formatting files or checking if they're already formatted is possible from the command line. This requires `node` and `npm` (optionally also `npx`).
73+
Formatting files or checking if they're already formatted is also possible from the command line without docker. This requires `node` and `npm` (optionally also `npx`).
6974

70-
### Available features from the command line:
71-
- To prettify a file: `npm run --silent prettify-md < input.md`.
72-
- To prettify a file and save the output: `npm run --silent prettify-md < input.md > output.md`.
73-
- To check whether a file is prettyfied or not: `npm run --silent check-md < input.md`. This will fail with an exception and return code `1` if the file is not prettyfied.
75+
| Feature | Docker | CLI |
76+
|---------------------------------------|-------------------------------------------------------------------------------|----------------------------------------------------------------|
77+
| Prettify a file | `docker container run -i darkriszty/prettify-md < input.md` | `npm run --silent prettify-md < input.md` |
78+
| Prettify a file and save the output | `docker container run -i darkriszty/prettify-md < input.md > output.md` | `npm run --silent prettify-md < input.md > output.md` |
79+
| Check whether a file is pretty or not | `docker container run -i darkriszty/prettify-md --check < input.md` | `npm run --silent check-md < input.md` |
80+
| Use `1` as column padding | `docker container run -i darkriszty/prettify-md --columnPadding=1 < input.md` | `npm run --silent prettify-md -- --columnPadding=1 < input.md` |
7481

75-
> Note: the `--silent` switch sets the NPM log level to silent, which is useful to hide the executed file name and concentrate on the actual output.
82+
> Notes:
83+
> * The prettify check (`--check` or `check-md`) will fail with an exception and return code `1` if the file is not prettyfied.
84+
> * The `--silent` switch sets the NPM log level to silent, which is useful to hide the executed file name and concentrate on the actual output.
85+
> * The `--` after the npm run script part is needed for npm to forward the arguments (for instance `--columnPadding=1`) to the actual prettyfier script.
86+
> * Optionally, use `npx` to prettify files: `npx markdown-table-prettify < input.md` instead of `npm run --silent prettify-md < input.md`.
7687
7788
### Installation
7889

79-
To access the CLI, the extension can either be used from the Github sources, from the already instaledl VSCode extension or from NPM.
90+
To access the CLI, the extension can either be used from the Github sources, from the already installed VSCode extension or from NPM.
8091

8192
#### Compiling from the source code
8293

83-
- Download the source code.
84-
- Go to the extension directory.
94+
- Clone or download the source code.
8595
- Run `npm install`.
8696
- Run `npm run compile`.
8797

@@ -94,7 +104,7 @@ Locate the installed extension path. The typical location of the installed exten
94104

95105
#### Getting it from NPM
96106

97-
Install the NPM package `npm install -g markdown-table-prettify`. Optionally, use `npx` to prettify files: `npx markdown-table-prettify < input.md` (instead of `npm run --silent prettify-md < input.md`).
107+
Install the NPM package `npm install -g markdown-table-prettify`.
98108

99109
## Known Issues
100110

cli/argumentsParser.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { CliOptions } from "./cliOptions";
2+
3+
export function parseArguments(processArgs: string[]): CliOptions {
4+
return <CliOptions> {
5+
check: Boolean(hasArgument(ArgumentNames.CHECK_ARG) || false),
6+
columnPadding: Number(getArgumentValue(ArgumentNames.PADDING_ARG) || 0)
7+
};
8+
9+
function hasArgument(key: string): boolean {
10+
return processArgs.length > 2 && processArgs.find(arg => arg.startsWith("--" + key)) !== undefined;
11+
}
12+
13+
function getArgumentValue(key: string): string {
14+
const hasArguments = processArgs.length > 2;
15+
const split = (hasArguments
16+
? processArgs.find(arg => arg.startsWith("--" + key)) || ""
17+
: "")
18+
.split("=");
19+
20+
return split.length == 2
21+
? split[1]
22+
: null;
23+
}
24+
}
25+
26+
class ArgumentNames {
27+
public static readonly CHECK_ARG : string = "check";
28+
public static readonly PADDING_ARG : string = "columnPadding";
29+
}

cli/cliOptions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface CliOptions {
2+
check: Boolean;
3+
columnPadding: number;
4+
}

cli/cliPrettify.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import { CliOptions} from "./cliOptions";
12
import { MultiTablePrettyfier } from "../src/prettyfiers/multiTablePrettyfier";
23
import { TableFinder } from "../src/tableFinding/tableFinder";
34
import { TableValidator } from "../src/modelFactory/tableValidator";
45
import { SelectionInterpreter } from "../src/modelFactory/selectionInterpreter";
56
import { TableFactory } from "../src/modelFactory/tableFactory";
67
import { AlignmentFactory } from "../src/modelFactory/alignmentFactory";
7-
import { TrimmerTransformer } from "../src/modelFactory/transformers/trimmerTransformer";
88
import { BorderTransformer } from "../src/modelFactory/transformers/borderTransformer";
9+
import { TrimmerTransformer } from "../src/modelFactory/transformers/trimmerTransformer";
10+
import { FairTableIndentationDetector } from "../src/modelFactory/tableIndentationDetector";
911
import { ConsoleLogger } from "../src/diagnostics/consoleLogger";
1012
import { SingleTablePrettyfier } from "../src/prettyfiers/singleTablePrettyfier";
1113
import { NoSizeLimitChecker } from "../src/prettyfiers/sizeLimit/noSizeLimitChecker";
@@ -15,22 +17,22 @@ import { ContentPadCalculator } from "../src/padCalculation/contentPadCalculator
1517
import { PadCalculatorSelector } from "../src/padCalculation/padCalculatorSelector";
1618
import { AlignmentMarkerStrategy } from "../src/viewModelFactories/alignmentMarking";
1719
import { TableStringWriter } from "../src/writers/tableStringWriter";
18-
import { FairTableIndentationDetector } from "../src/modelFactory/tableIndentationDetector";
20+
import { ValuePaddingProvider } from "../src/writers/valuePaddingProvider";
1921

2022
export class CliPrettify {
2123

22-
public static prettify(text: string): string {
23-
const prettyfier = this.createPrettyfier();
24+
public static prettify(text: string, options?: CliOptions): string {
25+
const prettyfier = this.createPrettyfier(options);
2426
return prettyfier.formatTables(text);
2527
}
2628

27-
public static check(text: string): void {
28-
if (this.prettify(text) !== text) {
29+
public static check(text: string, options?: CliOptions): void {
30+
if (this.prettify(text, options) !== text) {
2931
throw new Error("The input file is not prettyfied!");
3032
}
3133
}
3234

33-
private static createPrettyfier(): MultiTablePrettyfier {
35+
private static createPrettyfier(options?: CliOptions): MultiTablePrettyfier {
3436
const logger = new ConsoleLogger();
3537
return new MultiTablePrettyfier(
3638
new TableFinder(new TableValidator(new SelectionInterpreter(true))),
@@ -43,10 +45,10 @@ export class CliPrettify {
4345
),
4446
new TableValidator(new SelectionInterpreter(false)),
4547
new TableViewModelFactory(new RowViewModelFactory(
46-
new ContentPadCalculator(new PadCalculatorSelector(), " "),
48+
new ContentPadCalculator(new PadCalculatorSelector(), " "),
4749
new AlignmentMarkerStrategy(":")
4850
)),
49-
new TableStringWriter(),
51+
new TableStringWriter(new ValuePaddingProvider(options?.columnPadding ?? 0)),
5052
[ logger ],
5153
new NoSizeLimitChecker()
5254
),

cli/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#!/usr/bin/env node
22

3+
import { parseArguments } from "./argumentsParser";
4+
import { CliOptions } from "./cliOptions";
35
import { CliPrettify } from "./cliPrettify";
46
import { InputReader } from "./inputReader";
57

6-
const checkOnly = process.argv.length > 2 || process.argv.find(arg => arg === "--check");
8+
const cliOptions: CliOptions = parseArguments(process.argv);
79

810
InputReader.subscribe(input =>
9-
checkOnly
10-
? CliPrettify.check(input)
11-
: process.stdout.write(CliPrettify.prettify(input))
11+
cliOptions.check
12+
? CliPrettify.check(input, cliOptions)
13+
: process.stdout.write(CliPrettify.prettify(input, cliOptions))
1214
);

0 commit comments

Comments
 (0)