Skip to content

Commit b409368

Browse files
committed
Merge pull request #146 from kmactavish/feature/suppress_bash_history
Optionally disable shell history suppression
2 parents f0e5600 + 4785b5c commit b409368

File tree

7 files changed

+175
-4
lines changed

7 files changed

+175
-4
lines changed

doc/examples.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,25 @@ JSON
241241
.. literalinclude:: ../examples/focus-window-and-panes.json
242242
:language: json
243243

244+
Terminal History
245+
----------------
246+
247+
tmuxp allows ``suppress_history: false`` to override the default command /
248+
suppression when building the workspace.
249+
This will add the ``shell_command`` to the bash history in the pane.
250+
251+
YAML
252+
~~~~
253+
254+
.. literalinclude:: ../examples/suppress-history.yaml
255+
:language: yaml
256+
257+
JSON
258+
~~~~
259+
260+
.. literalinclude:: ../examples/suppress-history.json
261+
:language: json
262+
244263
Window Index
245264
------------
246265

examples/suppress-history.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"windows": [
3+
{
4+
"panes": [
5+
"echo 'window in the history!'"
6+
],
7+
"focus": true,
8+
"suppress_history": false,
9+
"window_name": "appended"
10+
},
11+
{
12+
"panes": [
13+
"echo 'window not in the history!'"
14+
],
15+
"suppress_history": true,
16+
"window_name": "suppressed"
17+
},
18+
{
19+
"panes": [
20+
"echo 'session in the history!'"
21+
],
22+
"window_name": "default"
23+
},
24+
{
25+
"panes": [
26+
{
27+
"shell_command": "echo 'command in the history!'",
28+
"suppress_history": false
29+
},
30+
{
31+
"shell_command": "echo 'command not in the history!'",
32+
"suppress_history": true
33+
},
34+
{
35+
"shell_command": "echo 'window not in the history!'"
36+
}
37+
],
38+
"suppress_history": true,
39+
"window_name": "mixed"
40+
}
41+
],
42+
"suppress_history": false,
43+
"session_name": "suppress"
44+
}

examples/suppress-history.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
session_name: suppress
2+
suppress_history: false
3+
windows:
4+
- window_name: appended
5+
focus: true
6+
suppress_history: false
7+
panes:
8+
- echo "window in the history!"
9+
10+
- window_name: suppressed
11+
suppress_history: true
12+
panes:
13+
- echo "window not in the history!"
14+
15+
- window_name: default
16+
panes:
17+
- echo "session in the history!"
18+
19+
- window_name: mixed
20+
suppress_history: false
21+
panes:
22+
- shell_command:
23+
- echo "command in the history!"
24+
suppress_history: false
25+
- shell_command:
26+
- echo "command not in the history!"
27+
suppress_history: true
28+
- shell_command:
29+
- echo "window in the history!"

tmuxp/config.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,11 @@ def trickle(sconf):
309309
else:
310310
session_start_directory = None
311311

312+
if 'suppress_history' in sconf:
313+
suppress_history = sconf['suppress_history']
314+
else:
315+
suppress_history = None
316+
312317
for windowconfig in sconf['windows']:
313318

314319
# Prepend start_directory to relative window commands
@@ -325,6 +330,11 @@ def trickle(sconf):
325330
)
326331
windowconfig['start_directory'] = window_start_path
327332

333+
# We only need to trickle to the window, workspace builder checks wconf
334+
if suppress_history is not None:
335+
if not 'suppress_history' in windowconfig:
336+
windowconfig['suppress_history'] = suppress_history
337+
328338
for paneconfig in windowconfig['panes']:
329339
commands_before = []
330340

tmuxp/pane.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def cmd(self, cmd, *args, **kwargs):
7575

7676
return self.server.cmd(cmd, *args, **kwargs)
7777

78-
def send_keys(self, cmd, enter=True):
78+
def send_keys(self, cmd, enter=True, suppress_history=True):
7979
"""``$ tmux send-keys`` to the pane.
8080
8181
A leading space character is added to cmd to avoid polluting the
@@ -85,9 +85,12 @@ def send_keys(self, cmd, enter=True):
8585
:type cmd: str
8686
:param enter: Send enter after sending the input.
8787
:type enter: bool
88+
:param suppress_history: Don't add these keys to the shell history
89+
:type suppress_history: bool
8890
8991
"""
90-
self.cmd('send-keys', ' ' + cmd)
92+
prefix = ' ' if suppress_history else ''
93+
self.cmd('send-keys', prefix + cmd)
9194

9295
if enter:
9396
self.enter()

tmuxp/testsuite/workspacebuilder.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
import tempfile
1515
import time
1616
import unittest
17+
import subprocess
1718

1819
import kaptan
1920

2021
from .helpers import TmuxTestCase
2122
from .. import Window, config, exc
22-
from .._compat import text_type
23+
from .._compat import text_type, console_to_str
2324
from ..workspacebuilder import WorkspaceBuilder
2425

2526
logger = logging.getLogger(__name__)
@@ -214,6 +215,63 @@ def test_focus_pane_index(self):
214215
self.assertEqual(p.get('pane_current_path'), pane_path)
215216

216217

218+
class SuppressHistoryTest(TmuxTestCase):
219+
yaml_config = """
220+
session_name: sampleconfig
221+
start_directory: '~'
222+
suppress_history: false
223+
windows:
224+
- window_name: inHistory
225+
panes:
226+
- echo inHistory
227+
- window_name: isMissing
228+
suppress_history: true
229+
panes:
230+
- echo isMissing
231+
"""
232+
def test_suppress_history(self):
233+
s = self.session
234+
sconfig = kaptan.Kaptan(handler='yaml')
235+
sconfig = sconfig.import_config(self.yaml_config).get()
236+
sconfig = config.expand(sconfig)
237+
sconfig = config.trickle(sconfig)
238+
239+
builder = WorkspaceBuilder(sconf=sconfig)
240+
builder.build(session=self.session)
241+
time.sleep(0.2) # give .bashrc, etc. time to load
242+
243+
s.server._update_windows()
244+
for w in s.windows:
245+
w.server._update_panes()
246+
w.select_window()
247+
for p in w.panes:
248+
p.select_pane()
249+
250+
# Print the last-in-history command in the pane
251+
self.session.cmd('send-keys', ' fc -ln -1')
252+
self.session.cmd('send-keys', 'Enter')
253+
time.sleep(0.01) # give fc time to run
254+
255+
# Get the contents of the pane
256+
self.session.cmd('capture-pane')
257+
captured_pane = self.session.cmd('show-buffer')
258+
self.session.cmd('delete-buffer')
259+
260+
# Parse the sent and last-in-history commands
261+
sent_cmd = captured_pane.stdout[0].strip()
262+
history_cmd = captured_pane.stdout[-2].strip()
263+
264+
# If it was in the history, sent == history
265+
if 'inHistory' in sent_cmd:
266+
self.assertEqual(sent_cmd, history_cmd)
267+
# Otherwise, sent != history
268+
elif 'isMissing' in sent_cmd:
269+
self.assertNotEqual(sent_cmd, history_cmd)
270+
# Something went wrong
271+
else:
272+
self.assertTrue(False)
273+
274+
217275
class WindowOptions(TmuxTestCase):
218276

219277
yaml_config = """
@@ -895,5 +953,6 @@ def suite():
895953
suite.addTest(unittest.makeSuite(WindowAutomaticRename))
896954
suite.addTest(unittest.makeSuite(WindowIndexTest))
897955
suite.addTest(unittest.makeSuite(WindowOptions))
956+
suite.addTest(unittest.makeSuite(SuppressHistoryTest))
898957
suite.addTest(unittest.makeSuite(EnvironmentVariables))
899958
return suite

tmuxp/workspacebuilder.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,15 @@ def get_pane_start_directory():
261261
if 'layout' in wconf:
262262
w.select_layout(wconf['layout'])
263263

264+
if 'suppress_history' in pconf:
265+
suppress = pconf['suppress_history']
266+
elif 'suppress_history' in wconf:
267+
suppress = wconf['suppress_history']
268+
else:
269+
suppress = True
270+
264271
for cmd in pconf['shell_command']:
265-
p.send_keys(cmd)
272+
p.send_keys(cmd, suppress_history=suppress)
266273

267274
if 'focus' in pconf and pconf['focus']:
268275
w.select_pane(p['pane_id'])

0 commit comments

Comments
 (0)