Skip to content

Instantly share code, notes, and snippets.

@Fity
Created August 19, 2014 16:41
Show Gist options
  • Select an option

  • Save Fity/bf44615d4fb6f26a10ea to your computer and use it in GitHub Desktop.

Select an option

Save Fity/bf44615d4fb6f26a10ea to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''Change the order of the last command and excute again
Not completed, canceled.
@Author [email protected]
'''
import logging
import os
import subprocess
import sys
#
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
def zsh_processor():
logger.debug('this is in the zsh processor')
whole_cmd = last_zsh_cmd()
pass
def bash_processor():
logger.debug('this is in the bash processor')
whole_cmd = last_bash_cmd()
logger.debug('last bash command: ' + whole_cmd)
def last_zsh_cmd():
shell_cmd = '''zsh -c -i "fc -R; history | grep -v '\d\*' | tail"'''
last_cmd = subprocess.check_output(shell_cmd, shell=True)
# last_cmd = last_cmd.split('\n')[0]
logger.debug('last zsh cmd is:\n' + last_cmd)
return last_cmd.split()
def last_bash_cmd():
shell_cmd = '''history | tail -2'''
last_cmd = subprocess.check_output(shell_cmd)
return last_cmd.split()
SHELLS = {'zsh': zsh_processor,
'bash': bash_processor}
def get_shell_name():
cmd = '''ps -ef | grep {0} | grep -v grep | grep -v ps\
| grep -v python'''.format(os.getppid())
logger.debug(cmd)
cmd_out = subprocess.check_output(cmd, shell=True)
logger.debug(cmd_out)
shell_str = os.path.basename(cmd_out.split()[-1])
return shell_str
def main():
global SHELLS
# shell = os.path.basename(os.environ['SHELL'])
shell = get_shell_name()
logger.debug(shell)
if shell in SHELLS:
logger.debug('Current shell is: ' + shell)
SHELLS[shell]()
else:
print >> sys.stderr, (
'Your current shell[%s] is not supported.\n' % shell,
'Supported shells are ',
'/'.join(SHELLS.keys()))
sys.exit(1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment