Skip to content

Commit

Permalink
Add support for Vi mode, close #85
Browse files Browse the repository at this point in the history
  • Loading branch information
eliangcs committed Nov 27, 2016
1 parent 6156659 commit ac1bb2a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion http_prompt/cli.py
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions http_prompt/defaultconfig.py
Expand Up @@ -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
53 changes: 53 additions & 0 deletions tests/test_interaction.py
Expand Up @@ -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
Expand All @@ -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()

0 comments on commit ac1bb2a

Please sign in to comment.