@@ -325,6 +325,37 @@ exports.decorateConfig = (config) => {
325
325
} ) ;
326
326
} ;
327
327
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
+
328
359
// Current process icon
329
360
const getIcon = ( title ) => {
330
361
const process = title . match (
@@ -333,34 +364,63 @@ const getIcon = (title) => {
333
364
return process ? process [ 0 ] . trim ( ) . toLowerCase ( ) : "shell" ;
334
365
} ;
335
366
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 ] ;
347
394
}
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 ;
349
406
} ;
350
407
351
408
exports . decorateTabs = ( Tabs , { React } ) => {
352
409
return class extends Tabs {
353
410
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
+ } ) ;
364
424
}
365
425
return React . createElement ( Tabs , Object . assign ( { } , this . props , { } ) ) ;
366
426
}
0 commit comments