Skip to content

[utils] Add a util to help printing with colors. NFC #4665

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

Merged
merged 3 commits into from
Sep 7, 2016
Merged
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
47 changes: 47 additions & 0 deletions include/swift/Basic/ColorUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//===--- ColorUtils.h - -----------------------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines an 'OSColor' class for helping printing colorful outputs
// to the terminal.
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_BASIC_COLORUTILS_H
#define SWIFT_BASIC_COLORUTILS_H

#include "llvm/Support/raw_ostream.h"

namespace swift {

/// RAII class for setting a color for a raw_ostream and resetting when it goes
/// out-of-scope.
class OSColor {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a doc-comment, e.g "RAII class for setting a color for a raw_ostream and resetting when it goes out-of-scope."

llvm::raw_ostream &OS;
bool HasColors;
public:
OSColor(llvm::raw_ostream &OS, llvm::raw_ostream::Colors Color) : OS(OS) {
HasColors = OS.has_colors();
if (HasColors)
OS.changeColor(Color);
}
~OSColor() {
if (HasColors)
OS.resetColor();
}

OSColor &operator<<(char C) { OS << C; return *this; }
OSColor &operator<<(llvm::StringRef Str) { OS << Str; return *this; }
};

} // namespace swift

#endif // SWIFT_BASIC_COLORUTILS_H