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