Giter VIP home page Giter VIP logo

colorful_terminal's Introduction

Colorful Terminal (CTL)

一个轻量级的在终端输出彩色信息的代码库。

1 安装

CTL 基于ANSI语法实现在终端进行彩色字体的输出不需要依赖于任何其他的库。并且CTL只有一个头文件,在加入到你的工程里时,最简单的方法就是把它拖入到你的工程里。

你有两种方式使用,1)直接拖入到你的工程目录下;2)运行install.sh脚本文件会帮助你把头文件安装到头文件目录

git clone https://github.com/mengkai98/colorful_terminal.git
cd colorful_terminal
chmod +x install.sh
sudo ./install.sh

只需要在你的代码里包含这个头文件即可

#include <colorful_terminal/colorful_terminal.hpp>

2 基础用法

CTL中提供了几个预设方便进行输出

#include "colorful_terminal.hpp"
int main(int argc, char const *argv[])
{
    ctl::r_out()<<"这是红色"<<ctl::endl;
    ctl::hr_out()<<"这是高亮红色"<<ctl::endl;

    ctl::g_out()<<"这是绿色"<<ctl::endl;
    ctl::hg_out()<<"这是高亮绿色"<<ctl::endl;
    
    ctl::y_out()<<"这是黄色"<<ctl::endl;
    ctl::hy_out()<<"这是高亮黄色"<<ctl::endl;

    ctl::bl_out()<<"这是蓝色"<<ctl::endl;
    ctl::hbl_out()<<"这是高亮蓝色"<<ctl::endl;

    ctl::p_out()<<"这是紫色"<<ctl::endl;
    ctl::hp_out()<<"这是高亮紫色"<<ctl::endl;

    ctl::dg_out()<<"这是深绿色"<<ctl::endl;
    ctl::hdg_out()<<"这是高亮的深绿色"<<ctl::endl;
    return 0;
}

结果如下图所示:

image-20230610235955919

注意,在使用ctl输出后,务必通过ctl::endl进行结尾,这样会清除掉前面设定的格式

3 参数表

CTL自带有制表功能,只需要做一些简单的操作,就可以将参数表输出。使用宏CTL_TABLE_ADD_VAR将变量加入到table里。使用make_table_and_out将参数表打印到终端上。

int a = 0;
float b = 0.5;
std::string c = "hello word";
Eigen::Vector3d v{1,2,3};
Eigen::Matrix2d m;
m.setIdentity();
ctl::table_out *to = new ctl::table_out();
CTL_TABLE_ADD_VAR(to,a);
CTL_TABLE_ADD_VAR(to,b);
CTL_TABLE_ADD_VAR(to,c);
CTL_TABLE_ADD_VAR(to,v);
CTL_TABLE_ADD_VAR(to,m);
to->make_table_and_out();

在终端里,输出为下:

image-20230611000045496

当然我们可以对这个表做一些定制化的调整:

struct color_table_scheme{
    enum COLOR front_color =CRUNUSED;
    enum COLOR back_color = CRUNUSED;
    enum CTRL ctl = CLUNUSED;
};

color_table_scheme line_color;//线框颜色
color_table_scheme title_color;//表标题颜色
color_table_scheme head_color;//表头的颜色
color_table_scheme context_color;//字体颜色
int padding ;
std::string table_name;//表的名字

table有以上的一些属性,通过调整这些属性得到一个你自己喜欢的表格

4 设置测试点

有些时候我们需要debug一些消息时,或者要判断某一个地方会不会执行到,往往只需要输出一些提示性信息即可,这个时候可以使用CTL_AUTO_TP,TPtest point的缩写:

CTL_AUTOTP(TAG);

TAG可以是int、float、string等类型的数据。

输出如下:[]中的是这行代码所在的文件名,()里是行号

image-20230611000256192

5 输出自定义数据,需要为你的数据结构重载“<<”

以std::vector为例

template<typename T>
friend ostream &operator>>( ostream &output, vector<T> &v )
{ 
    for(T&&a:v){
        output<<a;
    }
    output<<endl;
    return output;            
}

可以专门用一个头文件来存储所有输出运算符的重定义,以方便管理。

colorful_terminal's People

Contributors

mengkai98 avatar

Stargazers

Jack_GuCJ avatar LeiZHang avatar 柒者 avatar  avatar nexzhao avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.