Skip to content

Commit 66eeece

Browse files
authored
Merge pull request #82 from honeytreelabs/dev-fix-code-format-cli-stdin
fix CodeFormat CLI to allow reading from stdin
2 parents 9ffcbcb + 88333e0 commit 66eeece

File tree

3 files changed

+11
-15
lines changed

3 files changed

+11
-15
lines changed

CodeFormat/src/CodeFormat.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int main(int argc, char** argv)
3939
.Add<bool>("overwrite", "ow", "Format overwrite the input file")
4040
.Add<std::string>("workspace", "w",
4141
"Specify workspace directory,if no input file is specified, bulk formatting is performed")
42-
.Add<int>("stdin", "i", "Read from stdin and specify read size")
42+
.Add<bool>("stdin", "i", "Read from stdin and specify read size")
4343
.Add<std::string>("config", "c",
4444
"Specify .editorconfig file, it decides on the effect of formatting")
4545
.Add<bool>("detect-config", "d",
@@ -84,29 +84,28 @@ int main(int argc, char** argv)
8484
return -1;
8585
}
8686

87-
if (cmd.HasOption("file"))
87+
if (cmd.HasOption("file") || cmd.HasOption("stdin"))
8888
{
8989
auto luaFormat = std::make_shared<LuaFormat>();
90-
if (cmd.HasOption("file"))
90+
if (cmd.HasOption("file") && !cmd.HasOption("stdin"))
9191
{
9292
if (!luaFormat->SetInputFile(cmd.Get<std::string>("file")))
9393
{
9494
std::cerr << Util::format("Can not find file {}", cmd.Get<std::string>("file")) << std::endl;
9595
return -1;
9696
}
9797
}
98-
else if (cmd.HasOption("stdin"))
98+
else if (cmd.HasOption("stdin") && !cmd.HasOption("file"))
9999
{
100100
SET_BINARY_MODE();
101-
std::size_t size = cmd.Get<int>("stdin");
102-
if (!luaFormat->ReadFromStdin(size))
101+
if (!luaFormat->ReadFromStdin())
103102
{
104103
return -1;
105104
}
106105
}
107106
else
108107
{
109-
std::cerr << "not special input file" << std::endl;
108+
std::cerr << "Either --file or --stdin must be specified." << std::endl;
110109
return -1;
111110
}
112111

CodeFormat/src/LuaFormat.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,11 @@ bool LuaFormat::SetInputFile(std::string_view input)
2323
return _parser != nullptr;
2424
}
2525

26-
bool LuaFormat::ReadFromStdin(std::size_t size)
26+
bool LuaFormat::ReadFromStdin()
2727
{
28-
std::string buffer;
29-
buffer.resize(size);
30-
std::cin.get(buffer.data(), size, EOF);
31-
auto realSize = strnlen(buffer.data(), size);
32-
buffer.resize(realSize);
33-
_parser = LuaParser::LoadFromBuffer(std::move(buffer));
28+
std::stringstream buffer;
29+
buffer << std::cin.rdbuf();
30+
_parser = LuaParser::LoadFromBuffer(std::move(buffer.str()));
3431
return _parser != nullptr;
3532
}
3633

CodeFormat/src/LuaFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class LuaFormat
1717

1818
bool SetInputFile(std::string_view input);
1919

20-
bool ReadFromStdin(std::size_t size);
20+
bool ReadFromStdin();
2121

2222
void SetOutputFile(std::string_view path);
2323

0 commit comments

Comments
 (0)