xref: /haiku/src/apps/terminal/TitlePlaceholderMapper.cpp (revision 25a7b01d15612846f332751841da3579db313082)
102ee32bbSIngo Weinhold /*
202ee32bbSIngo Weinhold  * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de.
302ee32bbSIngo Weinhold  * Distributed under the terms of the MIT License.
402ee32bbSIngo Weinhold  */
502ee32bbSIngo Weinhold 
602ee32bbSIngo Weinhold 
7*19bfeaa7SSiarzhuk Zharski #include "TitlePlaceholderMapper.h"
8*19bfeaa7SSiarzhuk Zharski 
9a5b3caa2SSiarzhuk Zharski #include <Catalog.h>
10a5b3caa2SSiarzhuk Zharski 
11*19bfeaa7SSiarzhuk Zharski #include "TermConst.h"
1202ee32bbSIngo Weinhold 
1302ee32bbSIngo Weinhold 
1402ee32bbSIngo Weinhold // #pragma mark - TitlePlaceholderMapper
1502ee32bbSIngo Weinhold 
1602ee32bbSIngo Weinhold 
TitlePlaceholderMapper(const ShellInfo & shellInfo,const ActiveProcessInfo & processInfo)17ca598670SIngo Weinhold TitlePlaceholderMapper::TitlePlaceholderMapper(const ShellInfo& shellInfo,
1802ee32bbSIngo Weinhold 	const ActiveProcessInfo& processInfo)
1902ee32bbSIngo Weinhold 	:
20ca598670SIngo Weinhold 	fShellInfo(shellInfo),
2102ee32bbSIngo Weinhold 	fProcessInfo(processInfo)
2202ee32bbSIngo Weinhold {
2302ee32bbSIngo Weinhold }
2402ee32bbSIngo Weinhold 
2502ee32bbSIngo Weinhold 
2602ee32bbSIngo Weinhold bool
MapPlaceholder(char placeholder,int64 number,bool numberGiven,BString & _string)2702ee32bbSIngo Weinhold TitlePlaceholderMapper::MapPlaceholder(char placeholder, int64 number,
2802ee32bbSIngo Weinhold 	bool numberGiven, BString& _string)
2902ee32bbSIngo Weinhold {
3002ee32bbSIngo Weinhold 	switch (placeholder) {
3102ee32bbSIngo Weinhold 		case 'd':
3202ee32bbSIngo Weinhold 		{
3302ee32bbSIngo Weinhold 			// current working directory
3402ee32bbSIngo Weinhold 
3502ee32bbSIngo Weinhold 			// If a number is given, extract the respective number of rightmost
3602ee32bbSIngo Weinhold 			// components.
3702ee32bbSIngo Weinhold 			BString directory(fProcessInfo.CurrentDirectory());
3802ee32bbSIngo Weinhold 			if (numberGiven && number > 0) {
3902ee32bbSIngo Weinhold 				int32 index = directory.Length();
4002ee32bbSIngo Weinhold 				while (number > 0 && index > 0) {
4102ee32bbSIngo Weinhold 					index = directory.FindLast('/', index - 1);
4202ee32bbSIngo Weinhold 					number--;
4302ee32bbSIngo Weinhold 				}
4402ee32bbSIngo Weinhold 
453c905542SIngo Weinhold 				if (number == 0 && index >= 0 && index + 1 < directory.Length())
4602ee32bbSIngo Weinhold 					directory.Remove(0, index + 1);
4702ee32bbSIngo Weinhold 			}
4802ee32bbSIngo Weinhold 
4902ee32bbSIngo Weinhold 			_string = directory;
5002ee32bbSIngo Weinhold 			return true;
5102ee32bbSIngo Weinhold 		}
5202ee32bbSIngo Weinhold 
53*19bfeaa7SSiarzhuk Zharski 		case 'e':
54*19bfeaa7SSiarzhuk Zharski 			if (fShellInfo.Encoding() != M_UTF8) {
55*19bfeaa7SSiarzhuk Zharski 				_string.Truncate(0);
56*19bfeaa7SSiarzhuk Zharski 				_string << "[" << fShellInfo.EncodingName() << "]";
57*19bfeaa7SSiarzhuk Zharski 			}
58*19bfeaa7SSiarzhuk Zharski 			return true;
59*19bfeaa7SSiarzhuk Zharski 
6002ee32bbSIngo Weinhold 		case 'p':
61ca598670SIngo Weinhold 			// process name -- use "--", if the shell is active and it is the
62ca598670SIngo Weinhold 			// default shell
63ca598670SIngo Weinhold 			if (fProcessInfo.ID() == fShellInfo.ProcessID()
64ca598670SIngo Weinhold 				&& fShellInfo.IsDefaultShell()) {
652e394a47SIngo Weinhold 				_string = "--";
66ca598670SIngo Weinhold 			} else
6702ee32bbSIngo Weinhold 				_string = fProcessInfo.Name();
6802ee32bbSIngo Weinhold 			return true;
6902ee32bbSIngo Weinhold 	}
7002ee32bbSIngo Weinhold 
7102ee32bbSIngo Weinhold 	return false;
7202ee32bbSIngo Weinhold }
7302ee32bbSIngo Weinhold 
7402ee32bbSIngo Weinhold 
7502ee32bbSIngo Weinhold // #pragma mark - WindowTitlePlaceholderMapper
7602ee32bbSIngo Weinhold 
7702ee32bbSIngo Weinhold 
WindowTitlePlaceholderMapper(const ShellInfo & shellInfo,const ActiveProcessInfo & processInfo,int32 windowIndex,const BString & tabTitle)7802ee32bbSIngo Weinhold WindowTitlePlaceholderMapper::WindowTitlePlaceholderMapper(
79ca598670SIngo Weinhold 	const ShellInfo& shellInfo, const ActiveProcessInfo& processInfo,
80ca598670SIngo Weinhold 	int32 windowIndex, const BString& tabTitle)
8102ee32bbSIngo Weinhold 	:
82ca598670SIngo Weinhold 	TitlePlaceholderMapper(shellInfo, processInfo),
8302ee32bbSIngo Weinhold 	fWindowIndex(windowIndex),
8402ee32bbSIngo Weinhold 	fTabTitle(tabTitle)
8502ee32bbSIngo Weinhold {
8602ee32bbSIngo Weinhold }
8702ee32bbSIngo Weinhold 
8802ee32bbSIngo Weinhold 
8902ee32bbSIngo Weinhold bool
MapPlaceholder(char placeholder,int64 number,bool numberGiven,BString & _string)9002ee32bbSIngo Weinhold WindowTitlePlaceholderMapper::MapPlaceholder(char placeholder, int64 number,
9102ee32bbSIngo Weinhold 	bool numberGiven, BString& _string)
9202ee32bbSIngo Weinhold {
9302ee32bbSIngo Weinhold 	switch (placeholder) {
94a5b3caa2SSiarzhuk Zharski 		case 'T':
95a5b3caa2SSiarzhuk Zharski 			// The Terminal application name for the current locale
96a5b3caa2SSiarzhuk Zharski 			_string = B_TRANSLATE_SYSTEM_NAME("Terminal");
97a5b3caa2SSiarzhuk Zharski 			return true;
98a5b3caa2SSiarzhuk Zharski 
9902ee32bbSIngo Weinhold 		case 'i':
10002ee32bbSIngo Weinhold 			// window index
10102ee32bbSIngo Weinhold 			_string.Truncate(0);
10215e83c45SSiarzhuk Zharski 			if (fWindowIndex != 0)
10302ee32bbSIngo Weinhold 				_string << fWindowIndex;
10402ee32bbSIngo Weinhold 			return true;
10502ee32bbSIngo Weinhold 
10602ee32bbSIngo Weinhold 		case 't':
10702ee32bbSIngo Weinhold 			// the tab title
10802ee32bbSIngo Weinhold 			_string = fTabTitle;
10902ee32bbSIngo Weinhold 			return true;
11002ee32bbSIngo Weinhold 	}
11102ee32bbSIngo Weinhold 
11202ee32bbSIngo Weinhold 	return TitlePlaceholderMapper::MapPlaceholder(placeholder, number,
11302ee32bbSIngo Weinhold 		numberGiven, _string);
11402ee32bbSIngo Weinhold }
11502ee32bbSIngo Weinhold 
11602ee32bbSIngo Weinhold 
11702ee32bbSIngo Weinhold // #pragma mark - TabTitlePlaceholderMapper
11802ee32bbSIngo Weinhold 
11902ee32bbSIngo Weinhold 
TabTitlePlaceholderMapper(const ShellInfo & shellInfo,const ActiveProcessInfo & processInfo,int32 tabIndex)120ca598670SIngo Weinhold TabTitlePlaceholderMapper::TabTitlePlaceholderMapper(const ShellInfo& shellInfo,
12102ee32bbSIngo Weinhold 	const ActiveProcessInfo& processInfo, int32 tabIndex)
12202ee32bbSIngo Weinhold 	:
123ca598670SIngo Weinhold 	TitlePlaceholderMapper(shellInfo, processInfo),
12402ee32bbSIngo Weinhold 	fTabIndex(tabIndex)
12502ee32bbSIngo Weinhold {
12602ee32bbSIngo Weinhold }
12702ee32bbSIngo Weinhold 
12802ee32bbSIngo Weinhold 
12902ee32bbSIngo Weinhold bool
MapPlaceholder(char placeholder,int64 number,bool numberGiven,BString & _string)13002ee32bbSIngo Weinhold TabTitlePlaceholderMapper::MapPlaceholder(char placeholder, int64 number,
13102ee32bbSIngo Weinhold 	bool numberGiven, BString& _string)
13202ee32bbSIngo Weinhold {
13302ee32bbSIngo Weinhold 	switch (placeholder) {
13402ee32bbSIngo Weinhold 		case 'i':
13502ee32bbSIngo Weinhold 			// tab index
13602ee32bbSIngo Weinhold 			_string.Truncate(0);
13702ee32bbSIngo Weinhold 			_string << fTabIndex;
13802ee32bbSIngo Weinhold 			return true;
13902ee32bbSIngo Weinhold 	}
14002ee32bbSIngo Weinhold 
14102ee32bbSIngo Weinhold 	return TitlePlaceholderMapper::MapPlaceholder(placeholder, number,
14202ee32bbSIngo Weinhold 		numberGiven, _string);
14302ee32bbSIngo Weinhold }
144