diff --git a/http_prompt/cli.py b/http_prompt/cli.py index f464a3f..087487e 100644 --- a/http_prompt/cli.py +++ b/http_prompt/cli.py @@ -122,7 +122,7 @@ def cli(url, http_options): text = prompt('%s> ' % context.url, completer=completer, lexer=lexer, style=style, history=history, auto_suggest=AutoSuggestFromHistory(), - on_abort=AbortAction.RETRY) + on_abort=AbortAction.RETRY, vi_mode=cfg['vi']) except EOFError: break # Control-D pressed else: diff --git a/http_prompt/defaultconfig.py b/http_prompt/defaultconfig.py index da3cc07..c45e1fd 100644 --- a/http_prompt/defaultconfig.py +++ b/http_prompt/defaultconfig.py @@ -19,3 +19,8 @@ # 'ask': ask the user if they want to set the cookie # 'off': do nothing with the 'Set-Cookie' header set_cookies = 'auto' + +# Enable Vi editor mode? Available values: True / False. +# When Vi mode is enabled, you use Vi-like keybindings to edit your commands. +# When it is disabled, you use Emacs keybindings. +vi = False diff --git a/tests/test_interaction.py b/tests/test_interaction.py index d71eeef..4acc892 100644 --- a/tests/test_interaction.py +++ b/tests/test_interaction.py @@ -6,10 +6,38 @@ from .base import TempAppDirTestCase from .utils import get_http_prompt_path +from http_prompt import config class TestInteraction(TempAppDirTestCase): + def setUp(self): + super(TestInteraction, self).setUp() + + # Use temporary directory as user config home. + # Will restore it in tearDown(). + self.orig_config_home = os.getenv('XDG_CONFIG_HOME') + os.environ['XDG_CONFIG_HOME'] = self.temp_dir + + # Make sure pexpect uses the same terminal environment + self.orig_term = os.getenv('TERM') + os.environ['TERM'] = 'screen-256color' + + def tearDown(self): + super(TestInteraction, self).tearDown() + + os.environ['XDG_CONFIG_HOME'] = self.orig_config_home + + if self.orig_term: + os.environ['TERM'] = self.orig_term + else: + os.environ.pop('TERM', None) + + def write_config(self, content): + config_path = config.get_user_config_path() + with open(config_path, 'a') as f: + f.write(content) + @pytest.mark.skipif(sys.platform == 'win32', reason="pexpect doesn't work well on Windows") @pytest.mark.slow @@ -22,3 +50,28 @@ def test_interaction(self): child.sendline('exit') child.expect_exact('Goodbye!', timeout=20) child.close() + + @pytest.mark.slow + def test_vi_mode(self): + self.write_config('vi = True\n') + + bin_path = get_http_prompt_path() + child = pexpect.spawn(bin_path, env=os.environ) + + child.expect_exact('http://localhost:8000> ') + + # Enter 'htpie', switch to command mode (ESC), + # move two chars left (hh), and insert (i) a 't' + child.send('htpie') + child.send('\x1b') + child.sendline('hhit') + + child.expect_exact('http http://localhost:8000') + + # Insert 'exit' + child.send('\x1b') + child.send('i') + child.sendline('exit') + + child.expect_exact('Goodbye!', timeout=20) + child.close()