Skip to content

Instantly share code, notes, and snippets.

@zux0x3a
Created September 1, 2022 16:28
Show Gist options
  • Select an option

  • Save zux0x3a/3f2d48be524ee0eca95ada3910d4294d to your computer and use it in GitHub Desktop.

Select an option

Save zux0x3a/3f2d48be524ee0eca95ada3910d4294d to your computer and use it in GitHub Desktop.
jQuery Terminal Tabs
<div class="tabs cloak">
<ul>
<li class="new">
<a href="#">+</a>
</li>
</ul>
<div class="content"></div>
</div>
var count = 0;
init();
function init() {
new_terminal();
$('.cloak').removeClass('cloak');
$('.tabs').on('click', 'li.tab', function() {
var li = $(this);
tab_activate(this);
return false;
}).on('click', 'li.tab .close', function() {
remove_tab(this);
return false;
}).on('click', 'li.new', () => new_terminal());
github('jcubic/jquery.terminal');
}
function remove_tab(el) {
el = $(el);
var $li = el.closest('li');
var $tabs = el.closest('.tabs');
var index = $li.index();
var $terminals = $tabs.find('.content > div');
var $term = $terminals.eq(index).terminal();
if ($terminals.length > 1 && $term.is('.active')) {
var $other = $term.prev();
if (!$other.length) {
$other = $term.next();
}
var $new_li = $tabs.find('> ul li').eq($other.index());
tab_activate($new_li);
}
$term.destroy().remove();
$li.remove();
}
function tab_activate(li) {
var $li = $(li);
var index = $li.index();
$li.addClass('active').siblings().removeClass('active');
var $content = $li.closest('ul').next();
var $tab = $content.children()
.eq(index)
.addClass('active');
$tab.siblings()
.removeClass('active');
}
function new_terminal(label, interpreter = $.noop, options = null) {
var id = ++count;
options = options || {
greetings: `Welcome to jQuery Terminal, no: ${id}.\n`
};
var $term = $('<div/>').terminal($.noop, options);
label = label || `Terminal &lt;${id}&gt;`;
var $li = $(`<li class="tab">
<a href="#">${label}</a>
<span class="close">&times</span>
</li>`);
var $content = $('.tabs .content');
if ($content.is(':empty')) {
$li.add($term).addClass('active');
}
var $add = $('.tabs > ul li.new');
$li.insertBefore($add);
$content.append($term);
tab_activate($li);
}
<script src="https://cdn.jsdelivr.net/npm/jquery"></script>
<script src="https://cdn.jsdelivr.net/gh/jcubic/static/js/wcwidth.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery.terminal/js/jquery.terminal.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/js-polyfills/keyboard.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery.terminal/js/unix_formatting.js"></script>
<script src="https://cdn.jsdelivr.net/npm/prismjs/prism.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery.terminal/js/less.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery.terminal/js/prism.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery.terminal/js/xml_formatting.js"></script>
<script src="https://codepen.io/jcubic/pen/WZjbgq.js"></script>
body {
margin: 0;
padding: 0;
background: black;
}
:root {
--size: 1.4;
--color: #18B70A;
--glow: 1;
}
.tabs.cloak {
display: none;
}
body, html {
height: 100%;
}
/* tabs */
.tabs {
--gap: 10px;
display: flex;
flex-direction: column;
--original-background: var(--backgroud, #000);
}
.tabs, .tabs .content > div {
height: 100%;
}
.tabs > ul {
list-style: none;
margin: 0;
padding: var(--gap, 10px) var(--gap, 10px) 0;
font-family: var(--font, monospace);
}
.tabs > ul > li {
border-color: var(--color, #aaa);
border-radius: 3px 3px 0 0;
border-style: solid;
border-width: 1px 1px 0;
cursor: pointer;
display: inline-block;
padding: 6px 14px;
position: relative;
text-align: center;
font-size: calc(var(--size, 1) * 12px);
}
.tabs > ul > li > * {
background: var(--background, #000);
color: var(--color, #aaa);
text-shadow: 0 0 calc(var(--glow) * 5px) var(--color, #ccc);
}
.tabs > ul > li .close, .tabs > ul > li.new a {
display: inline-block;
line-height: 0.9em;
padding: 0 2px;
border-radius: 50%;
}
.tabs > ul > li .close:hover, .tabs > ul > li.new a:hover {
--background: var(--link-color, #3377FF);
--color: var(--original-background, #000);
}
.tabs > ul > li + li {
margin-left: var(--gap, 10px);
}
.tabs > ul > li a {
text-decoration: none;
}
.tabs > ul > li.active::after {
background-color: var(--background, #000);
bottom: -1px;
content: '';
height: 1px;
left: 0;
position: absolute;
width: 100%;
}
.tabs .content {
border-top: solid 1px var(--color, #ccc);
list-style: none;
margin: 0;
padding: 0;
flex-grow: 1;
}
.tabs .content > :not(.active) {
display: none;
}
<link href="https://cdn.jsdelivr.net/npm/prismjs/themes/prism-coy.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/jquery.terminal/css/jquery.terminal.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment