当前位置: 首页 > news >正文

java-推荐一个控制台输出颜色ANSI字符的类

java-推荐一个控制台输出颜色ANSI字符的类

    • 背景
    • 代码
    • 调用
    • 输出

背景

这个类是来自hive的一段代码,大家可以参考一下,这个类名是ColorBuffer

代码

/** Licensed to the Apache Software Foundation (ASF) under one* or more contributor license agreements.  See the NOTICE file* distributed with this work for additional information* regarding copyright ownership.  The ASF licenses this file* to you 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.*//** This source file is based on code taken from SQLLine 1.0.2* See SQLLine notice in LICENSE*/
package MyCollections;import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;/*** A buffer that can output segments using ANSI color.**/
final class ColorBuffer implements Comparable<Object> {private static final ColorAttr BOLD = new ColorAttr("\033[1m");private static final ColorAttr NORMAL = new ColorAttr("\033[m");private static final ColorAttr REVERS = new ColorAttr("\033[7m");private static final ColorAttr LINED = new ColorAttr("\033[4m");private static final ColorAttr GREY = new ColorAttr("\033[1;30m");private static final ColorAttr RED = new ColorAttr("\033[1;31m");private static final ColorAttr GREEN = new ColorAttr("\033[1;32m");private static final ColorAttr BLUE = new ColorAttr("\033[1;34m");private static final ColorAttr CYAN = new ColorAttr("\033[1;36m");private static final ColorAttr YELLOW = new ColorAttr("\033[1;33m");private static final ColorAttr MAGENTA = new ColorAttr("\033[1;35m");private static final ColorAttr INVISIBLE = new ColorAttr("\033[8m");private final List<Object> parts = new LinkedList<Object>();private int visibleLength = 0;private final boolean useColor;public ColorBuffer(boolean useColor) {this.useColor = useColor;append("");}public ColorBuffer(String str, boolean useColor) {this.useColor = useColor;append(str);}/*** Pad the specified String with spaces to the indicated length** @param str*          the String to pad* @param len*          the length we want the return String to be* @return the passed in String with spaces appended until the*         length matches the specified length.*/ColorBuffer pad(ColorBuffer str, int len) {while (str.getVisibleLength() < len) {str.append(" ");}return append(str);}ColorBuffer center(String str, int len) {StringBuilder buf = new StringBuilder(str);while (buf.length() < len) {buf.append(" ");if (buf.length() < len) {buf.insert(0, " ");}}return append(buf.toString());}ColorBuffer pad(String str, int len) {if (str == null) {str = "";}return pad(new ColorBuffer(str, false), len);}public String getColor() {return getBuffer(useColor);}public String getMono() {return getBuffer(false);}String getBuffer(boolean color) {StringBuilder buf = new StringBuilder();for (Object part : parts) {if (!color && part instanceof ColorAttr) {continue;}buf.append(part.toString());}return buf.toString();}/*** Truncate the ColorBuffer to the specified length and return* the new ColorBuffer. Any open color tags will be closed.* Do nothing if the specified length is <= 0.*/public ColorBuffer truncate(int len) {if (len <= 0) {return this;}ColorBuffer cbuff = new ColorBuffer(useColor);ColorAttr lastAttr = null;for (Iterator<Object> i = parts.iterator(); cbuff.getVisibleLength() < len && i.hasNext();) {Object next = i.next();if (next instanceof ColorAttr) {lastAttr = (ColorAttr) next;cbuff.append((ColorAttr) next);continue;}String val = next.toString();if (cbuff.getVisibleLength() + val.length() > len) {int partLen = len - cbuff.getVisibleLength();val = val.substring(0, partLen);}cbuff.append(val);}// close off the buffer with a normal tagif (lastAttr != null && lastAttr != NORMAL) {cbuff.append(NORMAL);}return cbuff;}@Overridepublic String toString() {return getColor();}public ColorBuffer append(String str) {parts.add(str);visibleLength += str.length();return this;}public ColorBuffer append(ColorBuffer buf) {parts.addAll(buf.parts);visibleLength += buf.getVisibleLength();return this;}private ColorBuffer append(ColorAttr attr) {parts.add(attr);return this;}public int getVisibleLength() {return visibleLength;}private ColorBuffer append(ColorAttr attr, String val) {parts.add(attr);parts.add(val);parts.add(NORMAL);visibleLength += val.length();return this;}public ColorBuffer bold(String str) {return append(BOLD, str);}public ColorBuffer lined(String str) {return append(LINED, str);}public ColorBuffer grey(String str) {return append(GREY, str);}public ColorBuffer red(String str) {return append(RED, str);}public ColorBuffer blue(String str) {return append(BLUE, str);}public ColorBuffer green(String str) {return append(GREEN, str);}public ColorBuffer cyan(String str) {return append(CYAN, str);}public ColorBuffer yellow(String str) {return append(YELLOW, str);}public ColorBuffer magenta(String str) {return append(MAGENTA, str);}private static class ColorAttr {private final String attr;public ColorAttr(String attr) {this.attr = attr;}@Overridepublic String toString() {return attr;}}public int compareTo(Object other) {return getMono().compareTo(((ColorBuffer) other).getMono());}
}

调用

ColorBuffer color = new ColorBuffer("sdfsdf", true);    
ColorBuffer color1 = new ColorBuffer( true);    
System.out.println(color.yellow("234234").magenta("77777").bold("55555").center("66666",9));
System.out.println(color1.yellow("234234").magenta("77777").bold("55555").center("66666",9));

输出

直接复制的文本,颜色显示不出来,自己可以尝试

sdfsdf2342347777755555  66666  

相关文章:

java-推荐一个控制台输出颜色ANSI字符的类

java-推荐一个控制台输出颜色ANSI字符的类 背景代码调用输出 背景 这个类是来自hive的一段代码&#xff0c;大家可以参考一下&#xff0c;这个类名是ColorBuffer 代码 /** Licensed to the Apache Software Foundation (ASF) under one* or more contributor license agreem…...

关于定义结构体别名时 是否加*

在C语言中&#xff0c;使用typedef来定义结构体类型及其指针的别名时&#xff0c;Node和LinkList的声明方式有所不同&#xff0c;这是因为你对它们的目的和用途有不同的设定。 首先&#xff0c;看一下你的代码&#xff1a; typedef struct { int data; int lenght; // 注意&am…...

成语积累学习

识文断字&#xff1a;有一点文化知识 雨后春笋&#xff1a;春雨过后快速生长的竹笋&#xff1b;比喻大量涌现的新生事物 味同嚼蜡&#xff1a;如同咀嚼白蜡一样&#xff0c;毫无味道。形容文章或言辞枯燥乏味。 差强人意&#xff1a;大体上让人满意 八面玲珑&#xff1a;处…...

基于Java的茶叶商城设计与实现(源码+定制+开发)茶叶电商系统开发、茶叶电商平台开发、茶叶在线销售平台设计与开发

博主介绍&#xff1a; ✌我是阿龙&#xff0c;一名专注于Java技术领域的程序员&#xff0c;全网拥有10W粉丝。作为CSDN特邀作者、博客专家、新星计划导师&#xff0c;我在计算机毕业设计开发方面积累了丰富的经验。同时&#xff0c;我也是掘金、华为云、阿里云、InfoQ等平台…...

桥接、NAT和仅主机三种网络模式对虚拟机IP地址分配的影响

在虚拟机中&#xff0c;桥接、NAT和仅主机&#xff08;Host-Only&#xff09;这三种网络模式会给虚拟机带来不同的IP地址分配方式及相应的网络连接特性&#xff0c;从而产生不同的影响&#xff0c;具体如下&#xff1a; 桥接模式 IP地址分配特点&#xff1a;在桥接模式下&…...

音乐播放器-0.专栏介绍​

1.简介 本专栏使用Qt QWidget作为显示界面&#xff0c;你将会学习到以下内容&#xff1a; 1.大量ui美化的实例。 2.各种复杂ui布局。 3.常见显示效果实现。 4.大量QSS实例。 5.Qt音频播放&#xff0c;音乐歌词文件加载&#xff0c;展示。 6.播放器界面换肤。 相信学习了本专栏…...

单月变现3W!AI助力沙雕图文爆红小绿书,12篇阅读量破10万+!

最近有没有小伙伴注意到&#xff0c;在各大社交平台上&#xff0c;那些温馨治愈、搞笑沙雕的图文内容&#xff0c;能吸引大量的目光和流量&#xff0c;不久前&#xff0c;我也曾分享过这类内容&#xff0c;比如让人眼前一亮的人间清醒老奶奶&#xff0c;她的图文就属于这类流行…...

C语言复习第4章 数组

目录 一、一维数组的创建和初始化1.1数组的创建1.2 变长数组1.3 数组的初始化1.4 全局数组默认初始化为01.5 区分两种字符数组1.6 用sizeof计算数组元素个数1.7 如何访问数组元素1.8 一维数组在内存中的存储(连续存储)1.9 访问数组元素的另一种方式:指针变量1.10 数组越界是运行…...

大数据研究实训室建设方案

一、概述 本方案旨在提出一套全面的大数据研究实训室建设策略&#xff0c;旨在为学生打造一个集理论学习与实践操作于一体的高端教育环境。实训室将专注于培养学生在大数据处理、分析及应用领域的专业技能&#xff0c;通过先进的设施配置、科学的课程体系和实用的实训模式&…...

Unity3D 观察者模式

Unity3D 泛型事件系统 观察者模式 观察者模式是一种行为设计模式&#xff0c;通过订阅机制&#xff0c;可以让对象触发事件时&#xff0c;通知多个其他对象。 在游戏逻辑中&#xff0c;UI 界面通常会监听一些事件&#xff0c;当数据层发生变化时&#xff0c;通过触发事件&am…...

vue从0开始的项目搭建(含环境配置)

一、环境准备 下载node.js 检查node.js版本 替换npm下载源 1.下载node.js: Node.js — 在任何地方运行 JavaScript (nodejs.org) 2.查看版本: windowsr输入cmd进入输入node -v命令查看版本号是否出现确认是否安装 2.替换npm下载源: npm config set registry https://reg…...

力扣61~65题

题61&#xff08;中等&#xff09;&#xff1a; 分析&#xff1a; python代码&#xff1a; # Definition for singly-linked list. # class ListNode: # def __init__(self, val0, nextNone): # self.val val # self.next next class Solution:def rot…...

API接口开发流程与指南

API&#xff08;应用程序编程接口&#xff09;是现代软件开发中不可或缺的一部分&#xff0c;它允许不同的软件应用之间进行交互和数据交换。无论是调用第三方服务、集成内部系统还是开发微服务架构&#xff0c;API都扮演着关键角色。本文将为你提供一个API接口入门的详解&…...

如何在Android中进行日志打印和调试?

在Android开发中&#xff0c;日志打印和调试是开发者定位问题、优化性能和提升应用质量的重要手段。以下将详细阐述如何在Android中进行日志打印和调试&#xff0c;包括日志工具的使用、调试技巧以及实践中的最佳实践。 一、日志工具的使用 1. Log类 Android中的日志工具类是…...

Linux基本使用和程序部署

文章目录 一. Linux背景Linux发行版 二. Linux环境搭建Linux常见命令lspwdcdtouchcatmkdirrmcpmvtailvimgreppsnetstat管道 三. 搭建java部署环境安装jdk安装mysql部署Web项目到Linux 一. Linux背景 1969−1970年,⻉尔实验室的DennisRitchie和KenTompson开发了Unix操作系统. 他…...

照片编辑成动态视频用什么软件好

在数字时代&#xff0c;让照片动起来确实已成为一种流行的潮流和趋势。如今&#xff0c;市面上涌现出众多软件&#xff0c;它们不仅配备了丰富多样的动态效果和特效&#xff0c;还支持用户进行个性化的编辑和创作。无论你是希望将家庭合影转化为充满温情的动画&#xff0c;还是…...

JavaWeb合集-SpringBoot项目配套知识

四、SpringBoot项目配套知识 1、Springboot项目的创建 2、HTTP 概念: Hyper Text Transfer Protocol,超文本传输协议&#xff0c;规定了浏览器和服务器之间数据传输的规则。 2.1 request 请求协议 浏览器向服务器发送请求的规则&#xff08;get、post等&#xff09;。 2.1.1 请…...

Electron入门笔记

Electron入门笔记 ElectronElectron 是什么Electron流程模型创建第一个Electron项目配置自动重启主进程和渲染进程通信打包应用 Electron Electron 是什么 跨平台的桌面应用开发框架使用 JavaScript、HTML 和 CSS 构建桌面应用程序的框架。 嵌入 Chromium和 Node.js Electro…...

python 不相交集简介(并查集算法)【Introduction to Disjoint Set (Union-Find Algorithm)】

什么是不相交集数据结构&#xff1f; 如果两个集合没有任何共同元素&#xff0c;则它们被称为不相交集&#xff0c;集合的交集为空集。 存储不重叠或不相交元素子集的数据结构称为不相交集合数据结构。不相交集合数据结构支持以下操作&#xff1a; 1、将新集合添加到不相交集合…...

23种设计模式之工厂方法模式

文章目录 1. 简介2. 代码2.1 抽象类&#xff1a;Course.java2.2 产品A&#xff1a;JavaCourse.java2.3 产品B&#xff1a;PythonCourse.java2.4 工厂抽象类&#xff1a;CourseFactory.java2.5 产品A的工厂A&#xff1a;JavaCourseFactory.java2.6 产品B的工厂B&#xff1a;PyCo…...

云计算——弹性云计算器(ECS)

弹性云服务器&#xff1a;ECS 概述 云计算重构了ICT系统&#xff0c;云计算平台厂商推出使得厂家能够主要关注应用管理而非平台管理的云平台&#xff0c;包含如下主要概念。 ECS&#xff08;Elastic Cloud Server&#xff09;&#xff1a;即弹性云服务器&#xff0c;是云计算…...

golang循环变量捕获问题​​

在 Go 语言中&#xff0c;当在循环中启动协程&#xff08;goroutine&#xff09;时&#xff0c;如果在协程闭包中直接引用循环变量&#xff0c;可能会遇到一个常见的陷阱 - ​​循环变量捕获问题​​。让我详细解释一下&#xff1a; 问题背景 看这个代码片段&#xff1a; fo…...

【JavaEE】-- HTTP

1. HTTP是什么&#xff1f; HTTP&#xff08;全称为"超文本传输协议"&#xff09;是一种应用非常广泛的应用层协议&#xff0c;HTTP是基于TCP协议的一种应用层协议。 应用层协议&#xff1a;是计算机网络协议栈中最高层的协议&#xff0c;它定义了运行在不同主机上…...

循环冗余码校验CRC码 算法步骤+详细实例计算

通信过程&#xff1a;&#xff08;白话解释&#xff09; 我们将原始待发送的消息称为 M M M&#xff0c;依据发送接收消息双方约定的生成多项式 G ( x ) G(x) G(x)&#xff08;意思就是 G &#xff08; x ) G&#xff08;x) G&#xff08;x) 是已知的&#xff09;&#xff0…...

Element Plus 表单(el-form)中关于正整数输入的校验规则

目录 1 单个正整数输入1.1 模板1.2 校验规则 2 两个正整数输入&#xff08;联动&#xff09;2.1 模板2.2 校验规则2.3 CSS 1 单个正整数输入 1.1 模板 <el-formref"formRef":model"formData":rules"formRules"label-width"150px"…...

深度学习习题2

1.如果增加神经网络的宽度&#xff0c;精确度会增加到一个特定阈值后&#xff0c;便开始降低。造成这一现象的可能原因是什么&#xff1f; A、即使增加卷积核的数量&#xff0c;只有少部分的核会被用作预测 B、当卷积核数量增加时&#xff0c;神经网络的预测能力会降低 C、当卷…...

Linux 中如何提取压缩文件 ?

Linux 是一种流行的开源操作系统&#xff0c;它提供了许多工具来管理、压缩和解压缩文件。压缩文件有助于节省存储空间&#xff0c;使数据传输更快。本指南将向您展示如何在 Linux 中提取不同类型的压缩文件。 1. Unpacking ZIP Files ZIP 文件是非常常见的&#xff0c;要在 …...

解析奥地利 XARION激光超声检测系统:无膜光学麦克风 + 无耦合剂的技术协同优势及多元应用

在工业制造领域&#xff0c;无损检测&#xff08;NDT)的精度与效率直接影响产品质量与生产安全。奥地利 XARION开发的激光超声精密检测系统&#xff0c;以非接触式光学麦克风技术为核心&#xff0c;打破传统检测瓶颈&#xff0c;为半导体、航空航天、汽车制造等行业提供了高灵敏…...

【Kafka】Kafka从入门到实战:构建高吞吐量分布式消息系统

Kafka从入门到实战:构建高吞吐量分布式消息系统 一、Kafka概述 Apache Kafka是一个分布式流处理平台,最初由LinkedIn开发,后成为Apache顶级项目。它被设计用于高吞吐量、低延迟的消息处理,能够处理来自多个生产者的海量数据,并将这些数据实时传递给消费者。 Kafka核心特…...

规则与人性的天平——由高考迟到事件引发的思考

当那位身着校服的考生在考场关闭1分钟后狂奔而至&#xff0c;他涨红的脸上写满绝望。铁门内秒针划过的弧度&#xff0c;成为改变人生的残酷抛物线。家长声嘶力竭的哀求与考务人员机械的"这是规定"&#xff0c;构成当代中国教育最尖锐的隐喻。 一、刚性规则的必要性 …...