// styles things that print out to the cliimport{TerminalColors,KeyCodes}from"../types/ascii";/** * Generate link text in the terminal. * @param text the text to show for the link. * @param url the url that the link should direct the user to. */constlinkText=(text: string,url: string)=>[KeyCodes.OSC,"8",KeyCodes.SEP,KeyCodes.SEP,url,KeyCodes.BEL,text,KeyCodes.OSC,"8",KeyCodes.SEP,KeyCodes.SEP,KeyCodes.BEL,].join("");constunderlineText=(text: string)=>{return`\u001b[4m${text}\u001b[24m`;};functionboldText(text: string){return`\x1b[1m${text}\x1b[0m`;}// change the color of the text when printing it outfunctioncolorText(str: string,color: TerminalColors){return"\x1b["+TerminalColors[color]+"m"+str+"\x1b[0m";}/** * Configure the style of text to print to the console. */classPrintStyle{privateisBold=false;privateisUnderlined=false;privatecolorName: TerminalColors|undefined;privatetext="";privateurl: string|undefined;constructor(text: string){this.text=text;}// bold a string when printing it outbold(){this.isBold=true;returnthis;}/** * add a link with a url. * if a url is not provided, assume the text is also the url. */link(maybeUrl?: string){if(maybeUrl){this.url=maybeUrl;}else{this.url=this.text;}returnthis;}color(colorName: string){if(!(colorNameinTerminalColors)){thrownewError(`Tried to color a terminal value. ${colorName} is not a valid color`);}this.colorName=colorNameasanyasTerminalColors;returnthis;}underline(){this.isUnderlined=true;returnthis;}/** * Redefine the default toString behavior to pretty-print. * @returns */toString(){letstr=this.text;if(this.url){str=linkText(str,this.url);}if(this.isBold){str=boldText(str);}if(this.colorName){str=colorText(str,this.colorName);}if(this.isUnderlined){str=underlineText(str);}returnstr;}}functionstyle(text: string){returnnewPrintStyle(text);}/** * Make text bold when rendering to the terminal. * @param text * @returns */functionbold(text: string){returnnewPrintStyle(text).bold();}/** * Choose a color for text that's rendered to the terminal. * @param text the text to color * @param colorName the color to use for the text. */functioncolor(text: string,colorName: string){returnnewPrintStyle(text).color(colorName);}/** * Accepts a string and a url. Prints a link. * If no url is provided, the text is assumed to be the url. */functionlink(text: string,url?: string){returnnewPrintStyle(text).link(url);}/** * Prints an underlined version of the text. */functionunderline(text: string){returnnewPrintStyle(text).underline();}export{style,bold,color,link,underline};