xref: /haiku/src/apps/terminal/TitlePlaceholderMapper.cpp (revision 02ee32bb0d3cc6680c421177bb490dba84230081)
1*02ee32bbSIngo Weinhold /*
2*02ee32bbSIngo Weinhold  * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3*02ee32bbSIngo Weinhold  * Distributed under the terms of the MIT License.
4*02ee32bbSIngo Weinhold  */
5*02ee32bbSIngo Weinhold 
6*02ee32bbSIngo Weinhold 
7*02ee32bbSIngo Weinhold #include "TitlePlaceholderMapper.h"
8*02ee32bbSIngo Weinhold 
9*02ee32bbSIngo Weinhold 
10*02ee32bbSIngo Weinhold // #pragma mark - TitlePlaceholderMapper
11*02ee32bbSIngo Weinhold 
12*02ee32bbSIngo Weinhold 
13*02ee32bbSIngo Weinhold TitlePlaceholderMapper::TitlePlaceholderMapper(
14*02ee32bbSIngo Weinhold 	const ActiveProcessInfo& processInfo)
15*02ee32bbSIngo Weinhold 	:
16*02ee32bbSIngo Weinhold 	fProcessInfo(processInfo)
17*02ee32bbSIngo Weinhold {
18*02ee32bbSIngo Weinhold }
19*02ee32bbSIngo Weinhold 
20*02ee32bbSIngo Weinhold 
21*02ee32bbSIngo Weinhold #include <stdio.h>
22*02ee32bbSIngo Weinhold bool
23*02ee32bbSIngo Weinhold TitlePlaceholderMapper::MapPlaceholder(char placeholder, int64 number,
24*02ee32bbSIngo Weinhold 	bool numberGiven, BString& _string)
25*02ee32bbSIngo Weinhold {
26*02ee32bbSIngo Weinhold 	switch (placeholder) {
27*02ee32bbSIngo Weinhold 		case 'd':
28*02ee32bbSIngo Weinhold 		{
29*02ee32bbSIngo Weinhold 			// current working directory
30*02ee32bbSIngo Weinhold 
31*02ee32bbSIngo Weinhold 			// If a number is given, extract the respective number of rightmost
32*02ee32bbSIngo Weinhold 			// components.
33*02ee32bbSIngo Weinhold 			BString directory(fProcessInfo.CurrentDirectory());
34*02ee32bbSIngo Weinhold 			if (numberGiven && number > 0) {
35*02ee32bbSIngo Weinhold 				int32 index = directory.Length();
36*02ee32bbSIngo Weinhold 				while (number > 0 && index > 0) {
37*02ee32bbSIngo Weinhold 					 index = directory.FindLast('/', index - 1);
38*02ee32bbSIngo Weinhold 					 number--;
39*02ee32bbSIngo Weinhold 				}
40*02ee32bbSIngo Weinhold 
41*02ee32bbSIngo Weinhold 				if (index >= 0 && index + 1 < directory.Length())
42*02ee32bbSIngo Weinhold 					directory.Remove(0, index + 1);
43*02ee32bbSIngo Weinhold 			}
44*02ee32bbSIngo Weinhold 
45*02ee32bbSIngo Weinhold 			_string = directory;
46*02ee32bbSIngo Weinhold 			return true;
47*02ee32bbSIngo Weinhold 		}
48*02ee32bbSIngo Weinhold 
49*02ee32bbSIngo Weinhold 		case 'p':
50*02ee32bbSIngo Weinhold 			// process name
51*02ee32bbSIngo Weinhold 			_string = fProcessInfo.Name();
52*02ee32bbSIngo Weinhold 			return true;
53*02ee32bbSIngo Weinhold 	}
54*02ee32bbSIngo Weinhold 
55*02ee32bbSIngo Weinhold 	return false;
56*02ee32bbSIngo Weinhold }
57*02ee32bbSIngo Weinhold 
58*02ee32bbSIngo Weinhold 
59*02ee32bbSIngo Weinhold // #pragma mark - WindowTitlePlaceholderMapper
60*02ee32bbSIngo Weinhold 
61*02ee32bbSIngo Weinhold 
62*02ee32bbSIngo Weinhold WindowTitlePlaceholderMapper::WindowTitlePlaceholderMapper(
63*02ee32bbSIngo Weinhold 	const ActiveProcessInfo& processInfo, int32 windowIndex,
64*02ee32bbSIngo Weinhold 	const BString& tabTitle)
65*02ee32bbSIngo Weinhold 	:
66*02ee32bbSIngo Weinhold 	TitlePlaceholderMapper(processInfo),
67*02ee32bbSIngo Weinhold 	fWindowIndex(windowIndex),
68*02ee32bbSIngo Weinhold 	fTabTitle(tabTitle)
69*02ee32bbSIngo Weinhold {
70*02ee32bbSIngo Weinhold }
71*02ee32bbSIngo Weinhold 
72*02ee32bbSIngo Weinhold 
73*02ee32bbSIngo Weinhold bool
74*02ee32bbSIngo Weinhold WindowTitlePlaceholderMapper::MapPlaceholder(char placeholder, int64 number,
75*02ee32bbSIngo Weinhold 	bool numberGiven, BString& _string)
76*02ee32bbSIngo Weinhold {
77*02ee32bbSIngo Weinhold 	switch (placeholder) {
78*02ee32bbSIngo Weinhold 		case 'i':
79*02ee32bbSIngo Weinhold 			// window index
80*02ee32bbSIngo Weinhold 			_string.Truncate(0);
81*02ee32bbSIngo Weinhold 			_string << fWindowIndex;
82*02ee32bbSIngo Weinhold 			return true;
83*02ee32bbSIngo Weinhold 
84*02ee32bbSIngo Weinhold 		case 't':
85*02ee32bbSIngo Weinhold 			// the tab title
86*02ee32bbSIngo Weinhold 			_string = fTabTitle;
87*02ee32bbSIngo Weinhold 			return true;
88*02ee32bbSIngo Weinhold 	}
89*02ee32bbSIngo Weinhold 
90*02ee32bbSIngo Weinhold 	return TitlePlaceholderMapper::MapPlaceholder(placeholder, number,
91*02ee32bbSIngo Weinhold 		numberGiven, _string);
92*02ee32bbSIngo Weinhold }
93*02ee32bbSIngo Weinhold 
94*02ee32bbSIngo Weinhold 
95*02ee32bbSIngo Weinhold // #pragma mark - TabTitlePlaceholderMapper
96*02ee32bbSIngo Weinhold 
97*02ee32bbSIngo Weinhold 
98*02ee32bbSIngo Weinhold TabTitlePlaceholderMapper::TabTitlePlaceholderMapper(
99*02ee32bbSIngo Weinhold 	const ActiveProcessInfo& processInfo, int32 tabIndex)
100*02ee32bbSIngo Weinhold 	:
101*02ee32bbSIngo Weinhold 	TitlePlaceholderMapper(processInfo),
102*02ee32bbSIngo Weinhold 	fTabIndex(tabIndex)
103*02ee32bbSIngo Weinhold {
104*02ee32bbSIngo Weinhold }
105*02ee32bbSIngo Weinhold 
106*02ee32bbSIngo Weinhold 
107*02ee32bbSIngo Weinhold bool
108*02ee32bbSIngo Weinhold TabTitlePlaceholderMapper::MapPlaceholder(char placeholder, int64 number,
109*02ee32bbSIngo Weinhold 	bool numberGiven, BString& _string)
110*02ee32bbSIngo Weinhold {
111*02ee32bbSIngo Weinhold 	switch (placeholder) {
112*02ee32bbSIngo Weinhold 		case 'i':
113*02ee32bbSIngo Weinhold 			// tab index
114*02ee32bbSIngo Weinhold 			_string.Truncate(0);
115*02ee32bbSIngo Weinhold 			_string << fTabIndex;
116*02ee32bbSIngo Weinhold 			return true;
117*02ee32bbSIngo Weinhold 	}
118*02ee32bbSIngo Weinhold 
119*02ee32bbSIngo Weinhold 	return TitlePlaceholderMapper::MapPlaceholder(placeholder, number,
120*02ee32bbSIngo Weinhold 		numberGiven, _string);
121*02ee32bbSIngo Weinhold }
122