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#include <Catalog.h>
10
11#include "TermConst.h"
12
13
14// #pragma mark - TitlePlaceholderMapper
15
16
17TitlePlaceholderMapper::TitlePlaceholderMapper(const ShellInfo& shellInfo,
18	const ActiveProcessInfo& processInfo)
19	:
20	fShellInfo(shellInfo),
21	fProcessInfo(processInfo)
22{
23}
24
25
26bool
27TitlePlaceholderMapper::MapPlaceholder(char placeholder, int64 number,
28	bool numberGiven, BString& _string)
29{
30	switch (placeholder) {
31		case 'd':
32		{
33			// current working directory
34
35			// If a number is given, extract the respective number of rightmost
36			// components.
37			BString directory(fProcessInfo.CurrentDirectory());
38			if (numberGiven && number > 0) {
39				int32 index = directory.Length();
40				while (number > 0 && index > 0) {
41					index = directory.FindLast('/', index - 1);
42					number--;
43				}
44
45				if (number == 0 && index >= 0 && index + 1 < directory.Length())
46					directory.Remove(0, index + 1);
47			}
48
49			_string = directory;
50			return true;
51		}
52
53		case 'e':
54			if (fShellInfo.Encoding() != M_UTF8) {
55				_string.Truncate(0);
56				_string << "[" << fShellInfo.EncodingName() << "]";
57			}
58			return true;
59
60		case 'p':
61			// process name -- use "--", if the shell is active and it is the
62			// default shell
63			if (fProcessInfo.ID() == fShellInfo.ProcessID()
64				&& fShellInfo.IsDefaultShell()) {
65				_string = "--";
66			} else
67				_string = fProcessInfo.Name();
68			return true;
69	}
70
71	return false;
72}
73
74
75// #pragma mark - WindowTitlePlaceholderMapper
76
77
78WindowTitlePlaceholderMapper::WindowTitlePlaceholderMapper(
79	const ShellInfo& shellInfo, const ActiveProcessInfo& processInfo,
80	int32 windowIndex, const BString& tabTitle)
81	:
82	TitlePlaceholderMapper(shellInfo, processInfo),
83	fWindowIndex(windowIndex),
84	fTabTitle(tabTitle)
85{
86}
87
88
89bool
90WindowTitlePlaceholderMapper::MapPlaceholder(char placeholder, int64 number,
91	bool numberGiven, BString& _string)
92{
93	switch (placeholder) {
94		case 'T':
95			// The Terminal application name for the current locale
96			_string = B_TRANSLATE_SYSTEM_NAME("Terminal");
97			return true;
98
99		case 'i':
100			// window index
101			_string.Truncate(0);
102			if (fWindowIndex != 0)
103				_string << fWindowIndex;
104			return true;
105
106		case 't':
107			// the tab title
108			_string = fTabTitle;
109			return true;
110	}
111
112	return TitlePlaceholderMapper::MapPlaceholder(placeholder, number,
113		numberGiven, _string);
114}
115
116
117// #pragma mark - TabTitlePlaceholderMapper
118
119
120TabTitlePlaceholderMapper::TabTitlePlaceholderMapper(const ShellInfo& shellInfo,
121	const ActiveProcessInfo& processInfo, int32 tabIndex)
122	:
123	TitlePlaceholderMapper(shellInfo, processInfo),
124	fTabIndex(tabIndex)
125{
126}
127
128
129bool
130TabTitlePlaceholderMapper::MapPlaceholder(char placeholder, int64 number,
131	bool numberGiven, BString& _string)
132{
133	switch (placeholder) {
134		case 'i':
135			// tab index
136			_string.Truncate(0);
137			_string << fTabIndex;
138			return true;
139	}
140
141	return TitlePlaceholderMapper::MapPlaceholder(placeholder, number,
142		numberGiven, _string);
143}
144