console_shell.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # ##### BEGIN GPL LICENSE BLOCK #####
  2. #
  3. # This program is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License
  5. # as published by the Free Software Foundation; either version 2
  6. # of the License, or (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software Foundation,
  15. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. #
  17. # ##### END GPL LICENSE BLOCK #####
  18. # <pep8 compliant>
  19. import os
  20. import bpy
  21. language_id = "shell"
  22. def add_scrollback(text, text_type):
  23. for l in text.split("\n"):
  24. bpy.ops.console.scrollback_append(text=l.replace("\t", " "),
  25. type=text_type)
  26. def shell_run(text):
  27. import subprocess
  28. val, output = subprocess.getstatusoutput(text)
  29. if not val:
  30. style = 'OUTPUT'
  31. else:
  32. style = 'ERROR'
  33. add_scrollback(output, style)
  34. PROMPT = "$ "
  35. def execute(context, _is_interactive):
  36. sc = context.space_data
  37. try:
  38. line = sc.history[-1].body
  39. except:
  40. return {'CANCELLED'}
  41. bpy.ops.console.scrollback_append(text=sc.prompt + line, type='INPUT')
  42. shell_run(line)
  43. # insert a new blank line
  44. bpy.ops.console.history_append(text="", current_character=0,
  45. remove_duplicates=True)
  46. sc.prompt = os.getcwd() + PROMPT
  47. return {'FINISHED'}
  48. def autocomplete(_context):
  49. # sc = context.space_data
  50. # TODO
  51. return {'CANCELLED'}
  52. def banner(context):
  53. sc = context.space_data
  54. shell_run("bash --version")
  55. sc.prompt = os.getcwd() + PROMPT
  56. return {'FINISHED'}