Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
WebFreak001 committed May 10, 2023
0 parents commit 290a026
Show file tree
Hide file tree
Showing 13 changed files with 817 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Run Unittests
on:
push:
branches:
- master
pull_request:

jobs:
dubtest:
name: Dub Tests
strategy:
fail-fast: false
matrix:
# ubuntu / linux must run on kinda old linux for glibc compatibility!
os: [ubuntu-20.04]
dc: [dmd-latest, ldc-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2

- name: Install D compiler
uses: dlang-community/setup-dlang@v1
timeout-minutes: 5
with:
compiler: ${{ matrix.dc }}

- name: Run unittests
run: dub test
timeout-minutes: 5

- name: Run format tests
run: cd test && dub --single checkfmt.d
timeout-minutes: 5
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.dub
docs.json
__dummy.html
docs/
/sdlfmt
libsdlfmt.so
libsdlfmt.dylib
sdlfmt.dll
libsdlfmt.a
sdlfmt.lib
sdlfmt-test-*
*.exe
*.pdb
*.o
*.obj
*.lst
test/checkfmt
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Jan Jurzitza

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# sdlfmt

```
Auto-formatter for SDLang files
Usage:
sdlfmt [OPTIONS...] [--] FILENAMES...
General options:
-i --inplace Edit files in place
-h --help This help information.
Formatter options:
--end_of_line (lf|cr|crlf)
-t --indent_style (tab|space)
--indent_size (default = 1 for tab, 4 for space)
--whitespace_around_equals (default = false)
--backslash_temp_indent_size (default = 2)
Use - as only file name if you want to read from stdin
By default, only a single file can be passed in as input and the formatted
output will be dumped to stdout. Use --inplace to be able to specify multiple
input files.
```
5 changes: 5 additions & 0 deletions dub.sdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name "sdlfmt"
description "A minimal D application."
authors "webfreak"
license "MIT"
dependency "sdlite" version="~>1.1.2"
7 changes: 7 additions & 0 deletions dub.selections.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"fileVersion": 1,
"versions": {
"sdlite": "1.1.2",
"taggedalgebraic": "0.11.22"
}
}
110 changes: 110 additions & 0 deletions source/app.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import sdlfmt;
import std.array;
import std.file;
import std.getopt;
import std.stdio;
import std.utf : validate;

int main(string[] args)
{
GetoptResult getoptHelp;
SdlFmtConfig config = SdlFmtConfig.getopt(args, getoptHelp);

bool inplace;
auto extraGetopt = args.getopt(std.getopt.config.passThrough,
"inplace|i", "Edit files in place", &inplace);

void printUsage()
{
defaultGetoptPrinter(
"Auto-formatter for SDLang files\nUsage:\n"
~ "\tsdlfmt [OPTIONS...] [--] FILENAMES..."
~ "\n\n General options:", extraGetopt.options);
defaultGetoptPrinter("\nFormatter options:", getoptHelp.options[0 .. $ - 1]); // exclude -h / --help
writeln();
writeln("Use - as only file name if you want to read from stdin");
writeln();
writeln("By default, only a single file can be passed in as input and the formatted");
writeln("output will be dumped to stdout. Use --inplace to be able to specify multiple");
writeln("input files.");
}

if (getoptHelp.helpWanted)
{
printUsage();
return 0;
}

if (args.length == 1)
{
printUsage();
return 1;
}

auto files = args[1 .. $];

version (Windows)
{
// On Windows, set stdout to binary mode (needed for correct EOL writing)
// See Phobos' stdio.File.rawWrite
{
import std.stdio : _O_BINARY;
immutable fd = stdout.fileno;
_setmode(fd, _O_BINARY);
version (CRuntime_DigitalMars)
{
import core.atomic : atomicOp;
import core.stdc.stdio : __fhnd_info, FHND_TEXT;

atomicOp!"&="(__fhnd_info[fd], ~FHND_TEXT);
}
}
}

if (files == ["-"])
{
auto buffer = appender!(const(ubyte)[]);
ubyte[4096] inputBuffer;
while (true)
{
auto b = stdin.rawRead(inputBuffer[]);
if (b.length)
buffer ~= b;
else
break;
}
auto text = cast(const(char)[]) buffer.data;

validate(text);

auto result = config.format(text, "(stdin)");
// never stream to stdout!
stdout.rawWrite(result);
}
else if (inplace)
{
import std.file : write;

foreach (file; files)
{
auto input = readText(file);
auto result = config.format(input, file);
// never stream to this file!
write(file, result);
}
}
else
{
if (files.length > 1)
{
stderr.writeln("Must run with `--inplace` if you want to specify multiple input files");
return 1;
}

auto result = config.format(readText(files[0]), files[0]);
// never stream to stdout!
stdout.rawWrite(result);
}

return 0;
}
Loading

0 comments on commit 290a026

Please sign in to comment.