使用 tcolorbox 绘制逼真的 Linux 命令 Shell 窗口

使用 tcolorbox 绘制逼真的 Linux 命令 Shell 窗口

我正在尝试制作这些小型的模拟命令 shell 窗口/框/东西。问题是我无法跳过行。提示符(又名$PS1)出现在每一行上;没有空间用于命令输出。有什么想法吗?谢谢。

PS:我使用tcolorbox来绘制方框。我通常使用minted来格式化代码(包括在同一文档中的其他地方),但这些方框使用listings,就像手册中的示例一样。


情况是这样的:

A) 在此处输入图片描述 乳胶
b) 在此处输入图片描述 乳胶


应该如何:

在此处输入图片描述 Inkscape


代码:

\documentclass[]{article}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage{tcolorbox,url}
\tcbuselibrary{skins,xparse,minted,listings}

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%% This is where it's defined %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newtcblisting{ubuntu}{colback=violet!50!black,colupper=white,
colframe=gray!65!black,listing only,listing options={style=tcblatex,language=sh},
title={\textcolor{orange}{\Huge{$\bullet$}}{\textcolor{gray}{\Huge{$\bullet\bullet$}}}},
every listing line={\textcolor{lime}{\small\ttfamily\bfseries
root@ubuntu{\textcolor{white}:}\textcolor{cyan!60}{\url{~}}{\textcolor{white}\#}}}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%% This is where it's drawn %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{ubuntu}
whoami \\ root
id \\ uid=0(root) gid=0(root) groups=0(root)
hostname \\ ubuntu
_
\end{ubuntu}    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}

答案1

您可以在列表中退出。我定义了一个命令,它可以让您进入新行而无需添加root@ubuntu:~#。(我只能在 Tiuri 编辑后运行代码。)

\documentclass[]{article}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage{tcolorbox,url}
\tcbuselibrary{skins,xparse,listings}

\begin{document}

\newtcblisting{ubuntu}{colback=violet!50!black,
colupper=white,colframe=gray!65!black,listing only,
listing options={style=tcblatex,language=sh,escapeinside=||,},
title={\textcolor{orange}{\Huge{$\bullet$}}{\textcolor{gray}{\Huge{$\bullet\bullet$}}}},
every listing line={\textcolor{lime}{\small\ttfamily\bfseries root@ubuntu{\textcolor{white}:}\textcolor{cyan!60}{\url{~}}{\textcolor{white}\#} }}}
\newlength{\Lind}
\setbox0\hbox{\small\ttfamily\bfseries root@}
\setlength{\Lind}{\the\dimexpr\wd0+1pt}
\newcommand{\LR}{\\\hspace*{-\Lind}}
\begin{ubuntu}
 whoami |\LR| root
 id |\LR| uid=0(root) gid=0(root) groups=0(root)
 hostname |\LR| ubuntu
 _
\end{ubuntu}

\end{document}

在此处输入图片描述

附录/改进. 回答后这个后续问题我觉得上面的内容没有必要那么复杂。我们可以定义一个命令,\StartConsole删除提示,以及一个命令\SU安装某个提示。此命令可以用作

\SU{user=bob,host=remotehost,color=cyan}

这将提示更改为

 bob@remotehost:~# 

更棒的是,可以为用户安装样式。例如,

\pgfkeys{/ubuntu/.cd,bob/.style={user=bob,host=remotehost,color=cyan}}

其次是

\SU{bob}

将会达到同样的效果。

\documentclass[]{article}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage{tcolorbox,url}
\tcbuselibrary{skins,xparse,listings}

\begin{document}

\newtcblisting{ubuntu}{colback=violet!50!black,
colupper=white,colframe=gray!65!black,listing only,
listing options={style=tcblatex,language=sh,escapeinside=``,},
title={\textcolor{orange}{\Huge{$\bullet$}}{\textcolor{gray}{\Huge{$\bullet\bullet$}}}},
every listing line={\MyUbuntuPrompt}}
\pgfkeys{/ubuntu/.cd,
user/.code={\gdef\MyUbuntuUser{#1}},user={},
host/.code={\gdef\MyUbuntuHost{#1}},host={},
color/.code={\gdef\MyUbuntuColor{#1}},color=white,
prompt char/.code={\gdef\MyUbuntuPromptChar{#1}},prompt char=\#,
root/.style={user=root,host=ubuntu,color=lime,prompt char=\#},
bob/.style={user=bob,host=remotehost,color=cyan},
}
\newcommand{\SU}[1]{\pgfkeys{/ubuntu/.cd,#1}%
\gdef\MyUbuntuPrompt{\textcolor{\MyUbuntuColor}{\small\ttfamily\bfseries \MyUbuntuUser@\MyUbuntuHost{\textcolor{white}:}\textcolor{cyan!60}{\url{~}}{\textcolor{white}\MyUbuntuPromptChar} }}}
\newcommand{\StartConsole}{\gdef\MyUbuntuPrompt{}}

\SU{user=root,host=ubuntu,color=lime}
\begin{ubuntu}
whoami `\StartConsole`
root `\SU{root}` 
id `\StartConsole` 
uid=0(root) gid=0(root) groups=0(root)`\SU{root}`
hostname `\StartConsole`
ubuntu`\SU{root}`
ssh bob@remotehost`\StartConsole`
bob@remotehost's`\ `password:
Linux remotehost 2.6.32-5-686 #1 SMP Sun Sep 23 09:49:36 UTC 2012 i686
You have mail.
Last login: Wed Oct 16 01:12:35 2012 from localhost
`\SU{bob}`
whoami`\StartConsole`
bob`\SU{bob}`
_
\end{ubuntu}

\end{document}

在此处输入图片描述

带有特殊字符的用户名也可以正常工作。粗下划线借用自这个答案

\documentclass[]{article}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage{tcolorbox,url,tikzlings}
\tcbuselibrary{skins,xparse,listings}

\begin{document}

\newtcblisting{ubuntu}{colback=violet!50!black,
colupper=white,colframe=gray!65!black,listing only,
listing options={style=tcblatex,language=sh,escapeinside=``,},
title={\textcolor{orange}{\Huge{$\bullet$}}{\textcolor{gray}{\Huge{$\bullet\bullet$}}}},
every listing line={\MyUbuntuPrompt},enhanced,
overlay={\begin{tcbinvclipframe}
\penguin[shift={([xshift=2cm,yshift=-0.8cm]frame.north)}]
\end{tcbinvclipframe}}}
\pgfkeys{/ubuntu/.cd,
user/.code={\gdef\MyUbuntuUser{#1}},user={},
host/.code={\gdef\MyUbuntuHost{#1}},host={},
color/.code={\gdef\MyUbuntuColor{#1}},color=white,
prompt char/.code={\gdef\MyUbuntuPromptChar{#1}},prompt char=\#,
root/.style={user=root,host=ubuntu,color=lime,prompt char=\#},
bob/.style={user=bob,host=remotehost,color=cyan},
cat/.style={user={schroedingers\char`_cat},host=burrow,color=magenta,prompt char=\$}
}
\newcommand{\SU}[1]{\pgfkeys{/ubuntu/.cd,#1}%
\gdef\MyUbuntuPrompt{\textcolor{\MyUbuntuColor}{\small\ttfamily\bfseries \MyUbuntuUser@\MyUbuntuHost{\textcolor{white}:}\textcolor{cyan!60}{\url{~}}{\textcolor{white}\MyUbuntuPromptChar} }}}
\newcommand{\StartConsole}{\gdef\MyUbuntuPrompt{}}

\SU{user=root,host=ubuntu,color=lime}
\begin{ubuntu}
whoami `\StartConsole`
root `\SU{root}` 
id `\StartConsole` 
uid=0(root) gid=0(root) groups=0(root)`\SU{root}`
hostname `\StartConsole`
ubuntu`\SU{root}`
ssh cat@burrow`\StartConsole`
bob@remotehost's`\ `password:
Linux remotehost 2.6.32-5-686 #1 SMP Sun Sep 23 09:49:36 UTC 2012 i686
You have mail.
Last login: Wed Oct 16 01:12:35 2012 from localhost
`\SU{cat}`
whoami`\StartConsole`
schroedingers_cat`\SU{cat}`
_
\end{ubuntu}

\end{document}

在此处输入图片描述

企鹅肯定看到了这里多次出现的“Ubuntu”这个词。

相关内容