当前位置: 首页 > 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…...

Redis——事务

文章目录 Redis 事务Redis 的事务和 MySQL 事务的区别:事务操作MULTIEXECDISCARDWATCHUNWATCHwatch的实现原理 总结 Redis 事务 什么是事务 Redis 的事务和 MySQL 的事务 概念上是类似的. 都是把⼀系列操作绑定成⼀组. 让这⼀组能够批量执行 Redis 的事务和 MySQL 事务的区别:…...

Redis非关系型数据库操作命令大全

以下是 Redis 的常用操作命令大全&#xff0c;涵盖了键值操作、字符串、哈希、列表、集合、有序集合、发布/订阅、事务等多个方面的操作。 1. 通用键命令 命令说明SET key value设置指定 key 的值GET key获取指定 key 的值DEL key删除指定的 keyEXISTS key检查 key 是否存在E…...

基于SpringBoot+Vue+uniapp微信小程序的澡堂预订的微信小程序的详细设计和实现

项目运行截图 技术框架 后端采用SpringBoot框架 Spring Boot 是一个用于快速开发基于 Spring 框架的应用程序的开源框架。它采用约定大于配置的理念&#xff0c;提供了一套默认的配置&#xff0c;让开发者可以更专注于业务逻辑而不是配置文件。Spring Boot 通过自动化配置和约…...

Linux mips架构链接库函数调用plt表汇编代码分析

linux调用共享库中的函数时通过plt表和got表实现位置无关代码&#xff0c;过程中涉及到lazy binding&#xff0c;即在第一调用外部函数时解析被调用的函数地址并将地址写入到got表&#xff0c;后续调用则不需要解析函数地址。这一部分和硬件架构有关&#xff0c;具体的是和cpu指…...

python 作业1

任务1: python为主的工作是很少的 学习的python的优势在于制作工具&#xff0c;制作合适的工具可以提高我们在工作中的工作效率的工具 提高我们的竞争优势。 任务2: 不换行 换行 任务3: 安装pycharm 进入相应网站Download PyCharm: The Python IDE for data science and we…...

Apache 出现 “403 forbidden“ 排查方法

1、检查运行 Apache 进程的用户没有对目录具备读取权限 如果该用户没有对 Directory 指定的目录具备适当的读取权限&#xff0c;就会导致 403 错误。 ​​例如&#xff1a;使用用户apache启动Apache进程&#xff0c;但是apache用户对 Directory 指定的目录没有读取权限 2、检查…...

vue video播放m3u8监控视频

很关键的问题 vite创建的项目不需要import ‘videojs-contrib-hls’ 导入就报错 直接添加如下代码即可 html5: {vhs: {overrideNative: true},nativeVideoTracks: false,nativeAudioTracks: false,nativeTextTracks: false} 下面是完整组件示例 <template><div>…...

uniapp 获取签名证书 SHA1 自有证书签名打包

1.登录你的Dcloud 账户 2.找到我的应用菜单 3.点开某个应用 4.查看证书详情&#xff0c;里面有SHA1 和别名&#xff0c;密码&#xff0c;下载证书用于云打包&#xff0c;可以选择自有证书&#xff0c;输入别名&#xff0c;密码打包...

Open3d开发点云标注工具问题总结(二)

前面我们介绍了使用AABB方式来框选点云&#xff0c;但这种方式还是不够直观&#xff0c;我们的构想是设计一个和o3d.visualization.VisualizerWithEditing的点云框选方法一样的软件&#xff0c;因此&#xff0c;博主想到利用投影的形式进行解决&#xff1a; 具体的&#xff0c;…...

【FreeRTOS】

报错&#xff1a; 使用STM32cubemx自动生成freertos选项V2报错&#xff0c;V1不报错 …/Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2/freertos_os2.h(31): 解决 修改cubemx配置&#xff0c;将V1.8.6改选为V1.8.5后编译不再报错...