Skip to content

Commit 429d5b9

Browse files
committed
feat: customize tab titles
1 parent 24cb78c commit 429d5b9

File tree

1 file changed

+82
-22
lines changed

1 file changed

+82
-22
lines changed

index.js

Lines changed: 82 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,37 @@ exports.decorateConfig = (config) => {
325325
});
326326
};
327327

328+
const getTitle = (tabTitle, numOpenTabs) => {
329+
let currentTitle = undefined;
330+
331+
// tabTitle can be a string or a React element
332+
if (typeof tabTitle === "string") {
333+
currentTitle = tabTitle;
334+
}
335+
336+
if (typeof tabTitle === "object") {
337+
currentTitle = tabTitle?.props?.children;
338+
}
339+
340+
return getModifiedTitle(currentTitle, numOpenTabs);
341+
};
342+
343+
const getIconClassName = (tabTitle) => {
344+
// tabTitle can be a string or a React element
345+
if (typeof tabTitle === "string") {
346+
const icon = getIcon(tabTitle);
347+
return `tab_process process_${icon}`;
348+
}
349+
if (typeof tabTitle === "object") {
350+
// implies that we have already set the className
351+
return tabTitle?.props?.className;
352+
}
353+
console.error(
354+
"[hyper-tabs-enhanced-titles] tabTitle is not a string or a React element"
355+
);
356+
return null;
357+
};
358+
328359
// Current process icon
329360
const getIcon = (title) => {
330361
const process = title.match(
@@ -333,34 +364,63 @@ const getIcon = (title) => {
333364
return process ? process[0].trim().toLowerCase() : "shell";
334365
};
335366

336-
// Tab process icons
337-
exports.decorateTab = (Tab, { React }) => {
338-
return class extends Tab {
339-
render() {
340-
const icon = getIcon(this.props.text);
341-
this.props.text = React.createElement(
342-
"span",
343-
{ className: `tab_process process_${icon}` },
344-
this.props.text
345-
);
346-
return React.createElement(Tab, Object.assign({}, this.props, {}));
367+
const getModifiedTitle = (currentTitle, numOpenTabs) => {
368+
if (currentTitle === undefined) {
369+
console.error(
370+
"[hyper-tabs-titles] tabTitle is not a string or a React element"
371+
);
372+
return currentTitle;
373+
}
374+
375+
// check if the tab title is of the pattern "user@host:dir".
376+
// if it is not, then directly return currentTitle. if it is, we want to modify it.
377+
378+
if (currentTitle.match(/^[^@]+@[^:]+:.+$/)) {
379+
// ^ - match the beginning of the string
380+
// [^@]+ - match one or more characters that are not the "@" symbol
381+
// @ - match the "@" symbol
382+
// [^:]+ - match one or more characters that are not the ":" symbol
383+
// : - match the ":" symbol
384+
// .+ - match one or more characters (any character except newline)
385+
// $ - match the end of the string
386+
387+
const cwd = currentTitle.split(":")[1];
388+
const threshold = getThreshold();
389+
390+
if (numOpenTabs > threshold) {
391+
// if there are more than 3 tabs open, then we want to show only the last directory in the path
392+
const dirs = cwd.split("/");
393+
return dirs[dirs.length - 1];
347394
}
348-
};
395+
396+
return cwd;
397+
}
398+
399+
return currentTitle;
400+
};
401+
402+
// helper for getting number of open tabs threshold
403+
const getThreshold = () => {
404+
const { hyperTabs } = config.getConfig();
405+
return hyperTabs?.openTabsThreshold ?? 3;
349406
};
350407

351408
exports.decorateTabs = (Tabs, { React }) => {
352409
return class extends Tabs {
353410
render() {
354-
if (
355-
this.props.tabs.length === 1 &&
356-
typeof this.props.tabs[0].title === "string"
357-
) {
358-
const icon = getIcon(this.props.tabs[0].title);
359-
this.props.tabs[0].title = React.createElement(
360-
"span",
361-
{ className: `tab_process process_${icon}` },
362-
this.props.tabs[0].title
363-
);
411+
if (this.props.tabs.length > 0) {
412+
// Add icon to all tabs
413+
this.props.tabs = this.props.tabs.map((tab) => {
414+
const iconClassName = getIconClassName(tab.title);
415+
const title = getTitle(tab.title, this.props.tabs.length);
416+
tab.title = React.createElement(
417+
"span",
418+
{ className: iconClassName },
419+
title
420+
);
421+
422+
return tab;
423+
});
364424
}
365425
return React.createElement(Tabs, Object.assign({}, this.props, {}));
366426
}

0 commit comments

Comments
 (0)