Add page sep feature

This commit is contained in:
John Sennesael 2022-03-31 19:46:07 -05:00
parent 5f91fd8b04
commit 5ad543aec3
8 changed files with 60 additions and 18 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
[syntax=glob]
*.o
*.d
*.*~
*.sw*
build/*

View File

@ -2,12 +2,16 @@ SOURCES := $(wildcard src/*.cpp)
OBJS = $(patsubst %.cpp,%.o,$(SOURCES))
CXXFLAGS += -Wall -pedantic -Werror -std=c++17 -I src/
%.o: %.cpp
$(CXX) -c $(CXXFLAGS) -o $@ $<
all: justify
justify: $(OBJS)
$(info Linking...)
$(CXX) $(CXXFLAGS) -o justify $(OBJS) $(LDFLAGS)
%.o: %.cpp
$(CXX) $(CXXFLAGS) -MD -MT $@ -o $@ -c $<
clean:
$(info Cleaning...)
find . -name '*.o' -exec rm {} \;
rm justify

View File

@ -42,7 +42,8 @@ std::vector<std::wstring> columns(
size_t columnHorizontalSpacing,
size_t columnVerticalSpacing,
size_t maxColumnHeight,
size_t width)
size_t width,
wchar_t pageSepLineChar)
{
std::vector<std::wstring> result;
@ -130,6 +131,7 @@ std::vector<std::wstring> columns(
// If we're still here, we'll just keep iterating until we hit the
// maximum column height.
}
// We might have some left-over.
if (!currentBlock.empty()) allBlocks.emplace_back(currentBlock);
// Cool! we have blocks now - now we have to arrange them into ROWS.
@ -178,7 +180,11 @@ std::vector<std::wstring> columns(
outLine = StringRightTrim(outLine);
result.emplace_back(outLine);
outLine.clear();
}
// If desired, add a page separator line using the desired characters.
if (pageSepLineChar != 0)
{
result.emplace_back(std::wstring(width - 1, pageSepLineChar));
}
}
return result;

View File

@ -29,4 +29,5 @@ std::vector<std::wstring> columns(
size_t columnHorizontalSpacing,
size_t columnVerticalSpacing,
size_t maxColumnHeight,
size_t width);
size_t width,
wchar_t pageSepLineChar = 0);

View File

@ -64,6 +64,8 @@ void usage(const std::string& program)
<< std::endl
<< " (default: 0, =infinite)"
<< std::endl;
std::cout << " -p,--pagesep [CHAR] Page separator line character."
<< std::endl;
std::cout << std::endl << "Alignment options: "
<< std::endl << std::endl;
std::cout << " -w,--width [NUM] Text width to format text into."
@ -208,6 +210,15 @@ Settings parseArgs(const StrVector& args)
}
continue;
}
else if ((arg == "-p") || (arg == "--pagesep"))
{
if (!checkParam("character", arg, args, it)) return result;
std::wstring charStr = WideStringFromString(*it);
if (!charStr.empty())
{
result.pageSepChar = charStr[0];
}
}
else if ((arg == "-h") || (arg == "--help"))
{
usage(args[0]);
@ -265,10 +276,6 @@ bool justify(const Settings& settings)
&inputFile,
[](std::wistream*) noexcept {}
);
if (!inputFile.is_open())
{
throw std::runtime_error("Failed to open.");
}
}
catch (const std::exception& e)
{
@ -289,10 +296,6 @@ bool justify(const Settings& settings)
try
{
outputFile.open(settings.outFile, std::ifstream::out);
if (!outputFile.is_open())
{
throw std::runtime_error("Failed to open.");
}
outputStream = std::shared_ptr<std::wostream>(
&outputFile,
[](std::wostream*) noexcept {}
@ -355,7 +358,8 @@ bool justify(const Settings& settings)
settings.hspacing,
settings.vspacing,
maxColHeight,
settings.width
settings.width,
settings.pageSepChar
);
}
else
@ -376,13 +380,13 @@ int main(int argc, char* argv[])
// Set locale to make unicode input work properly.
const char* langEnv = std::getenv("LANG");
const std::string sysLocale{langEnv ? langEnv : "C"};
try
{
std::locale::global(std::locale(sysLocale));
} catch (const std::exception& e)
{
std::cerr << "Could not set locale to: " << sysLocale << " : " << e.what() << std::endl;
std::cerr << "Could not set locale to: " << sysLocale << " : "
<< e.what() << std::endl;
return 5;
}
@ -394,7 +398,6 @@ int main(int argc, char* argv[])
{
// Get terminal width (to use as a default)
/// @TODO make sure this works on most unixes
/// Tested OK so far on GNU+Linux/FreeBSD/NetBSD
winsize termSize;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &termSize);
settings.width = termSize.ws_col;

View File

@ -33,6 +33,7 @@ struct Settings
Justification justify{Justification::FILL};
size_t maxColHeight{};
std::string outFile;
wchar_t pageSepChar{0};
bool shouldExit{false};
size_t vspacing{1};
size_t width{};

19
src/stringHelper.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "stringHelper.h"
#include <string>
static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> conv;
std::wstring WideStringFromString(const std::string& input)
{
std::wstring result;
try
{
result = conv.from_bytes(input);
}
catch(const std::range_error&)
{
return L"";
}
return result;
}

View File

@ -16,9 +16,12 @@
with Justify. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef STRINGHELPER_H_INCLUDE
#define STRINGHELPER_H_INCLUDE
#include <algorithm>
#include <codecvt>
#include <locale>
#include <string>
#include <vector>
@ -146,3 +149,7 @@ std::basic_string<T> StringToLower(const std::basic_string<T>& str)
std::transform(copy.begin(), copy.end(), copy.begin(), ::tolower);
return copy;
}
std::wstring WideStringFromString(const std::string& input);
#endif