Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding colourizing utilities for making pretty output text. #257

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions metaprogramming/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,23 @@ cc_test(
],
)

################################################################################
# color.
################################################################################
cc_library(
name = "color",
hdrs = ["color.h"],
)

cc_test(
name = "color_test",
srcs = ["color_test.cc"],
deps = [
":color",
"@googletest//:gtest_main",
],
)

################################################################################
# Concatenate.
################################################################################
Expand Down
76 changes: 76 additions & 0 deletions metaprogramming/color.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef JNI_BIND_METAPROGRAMMING_COLOUR_H_
#define JNI_BIND_METAPROGRAMMING_COLOUR_H_

#include <string>
#include <string_view>

namespace jni::metaprogramming {

struct Color {
static constexpr std::string_view kNone{"\033[0m"};

// --- Text Colors ---
static constexpr std::string_view kBlack = "\033[0;30m";
static constexpr std::string_view kBlackBold = "\033[1;30m";
static constexpr std::string_view kRed = "\033[0;31m";
static constexpr std::string_view kRedBold = "\033[1;31m";
static constexpr std::string_view kGreen = "\033[0;32m";
static constexpr std::string_view kGreenBold = "\033[1;32m";
static constexpr std::string_view kYellow = "\033[0;33m";
static constexpr std::string_view kYellowBold = "\033[1;33m";
static constexpr std::string_view kBlue = "\033[0;34m";
static constexpr std::string_view kBlueBold = "\033[1;34m";
static constexpr std::string_view kPurple = "\033[0;35m";
static constexpr std::string_view kPurpleBold = "\033[1;35m";
static constexpr std::string_view kCyan = "\033[0;36m";
static constexpr std::string_view kCyanBold = "\033[1;36m";
static constexpr std::string_view kWhite = "\033[0;37m";
static constexpr std::string_view kWhiteBold = "\033[1;37m";

// --- Background Colors ---
static constexpr std::string_view kBgBlack = "\033[40m";
static constexpr std::string_view kBgBlackBold = "\033[100m";
static constexpr std::string_view kBgRed = "\033[41m";
static constexpr std::string_view kBgRedBold = "\033[101m";
static constexpr std::string_view kBgGreen = "\033[42m";
static constexpr std::string_view kBgGreenBold = "\033[102m";
static constexpr std::string_view kBgYellow = "\033[43m";
static constexpr std::string_view kBgYellowBold = "\033[103m";
static constexpr std::string_view kBgBlue = "\033[44m";
static constexpr std::string_view kBgBlueBold = "\033[104m";
static constexpr std::string_view kBgPurple = "\033[45m";
static constexpr std::string_view kBgPurpleBold = "\033[105m";
static constexpr std::string_view kBgCyan = "\033[46m";
static constexpr std::string_view kBgCyanBold = "\033[106m";
static constexpr std::string_view kBgWhite = "\033[47m";
static constexpr std::string_view kBgWhiteBold = "\033[107m";
};

// Colorize function for runtime manipulation of text. This is suitable when
// the string is unknowable until runtime.
inline std::string Colorize(std::string_view colour, std::string_view str,
bool colourize = true) {
return !colourize ? std::string{str}
: std::string{colour} + std::string{str} +
std::string{Color::kNone};
}

} // namespace jni::metaprogramming

#endif // JNI_BIND_METAPROGRAMMING_COLOUR_H_
46 changes: 46 additions & 0 deletions metaprogramming/color_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "color.h"

#include <cstdio>
#include <string>

#include <gtest/gtest.h>

using ::jni::metaprogramming::Color;
using ::jni::metaprogramming::Colorize;

namespace {

TEST(ColorTest, HasBlue) {
std::string output = Colorize(Color::kBlue, "Blue words").data();

printf("%s\n", output.c_str());

EXPECT_TRUE(Colorize(Color::kBlue, "Blue words").find(Color::kBlue) !=
std::string::npos);
}

TEST(ColorTest, HasGreenAndDoesntBleed) {
std::string output = Colorize(Color::kRed, "Red words").data();
printf("%s, no color.\n", output.c_str());

EXPECT_TRUE(output.find(Color::kRed) != std::string::npos);
EXPECT_TRUE(output.find(Color::kNone) != std::string::npos);
}

} // namespace
Loading