TclCommandHelp.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # File Author: Marius Adrian Stanciu (c) #
  4. # Content was borrowed from FlatCAM proper #
  5. # Date: 4/22/2020 #
  6. # MIT Licence #
  7. # ##########################################################
  8. from tclCommands.TclCommand import TclCommand
  9. import collections
  10. import gettext
  11. import appTranslation as fcTranslate
  12. import builtins
  13. fcTranslate.apply_language('strings')
  14. if '_' not in builtins.__dict__:
  15. _ = gettext.gettext
  16. class TclCommandHelp(TclCommand):
  17. """
  18. Tcl shell command to show Help
  19. example:
  20. help add_circle
  21. """
  22. # List of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  23. aliases = ['help']
  24. description = '%s %s' % ("--", "PRINTS to TCL the HELP.")
  25. # Dictionary of types from Tcl command, needs to be ordered
  26. arg_names = collections.OrderedDict([
  27. ('name', str)
  28. ])
  29. # Dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  30. option_types = collections.OrderedDict([
  31. ])
  32. # array of mandatory options for current Tcl command: required = {'name','outname'}
  33. required = []
  34. # structured help for current command, args needs to be ordered
  35. help = {
  36. 'main': "Returns to TCL the value for the entered system variable.",
  37. 'args': collections.OrderedDict([
  38. ('name', 'Name of a Tcl Command for which to display the Help.'),
  39. ]),
  40. 'examples': ['help add_circle']
  41. }
  42. def execute(self, args, unnamed_args):
  43. """
  44. :param args: Without any argument will display the list of commands. Can have as a argument
  45. a tcl command name to display the help for that command.
  46. :param unnamed_args:
  47. :return:
  48. """
  49. if 'name' in args:
  50. name = args['name']
  51. if name not in self.app.shell.tcl_commands_storage:
  52. return "Unknown command: %s" % name
  53. help_for_command = self.app.shell.tcl_commands_storage[name]["help"] + '\n\n'
  54. self.app.shell.append_output(help_for_command)
  55. else:
  56. if not args:
  57. cmd_enum = '%s\n' % _("Available commands:")
  58. displayed_text = []
  59. try:
  60. # find the maximum length of a command name
  61. max_len = 0
  62. for cmd_name in self.app.shell.tcl_commands_storage:
  63. curr_len = len(cmd_name)
  64. if curr_len > max_len:
  65. max_len = curr_len
  66. h_space = " "
  67. cnt = 0
  68. for cmd_name in sorted(self.app.shell.tcl_commands_storage):
  69. cmd_description = "<span>%s</span>" % \
  70. self.app.shell.tcl_commands_storage[cmd_name]['description']
  71. curr_len = len(cmd_name)
  72. cmd_name_colored = "> <span style=\" font-weight: bold; color: red;\" >%s</span>" % \
  73. str(cmd_name)
  74. nr_chars = max_len - curr_len
  75. cmd_line_txt = '%s%s&nbsp;&nbsp;%s' % (cmd_name_colored, nr_chars * h_space, cmd_description)
  76. displayed_text.append(cmd_line_txt)
  77. # group commands by 4 adding a line break after 4 commands
  78. if cnt == 3:
  79. cnt = 0
  80. displayed_text.append(' ')
  81. else:
  82. cnt += 1
  83. except Exception as err:
  84. self.app.log.debug("tclCommands.TclCommandHelp() when run as 'help' --> %s" % str(err))
  85. displayed_text = ['> %s' % cmd for cmd in sorted(self.app.shell.tcl_commands_storage)]
  86. cmd_enum += '<br>'.join(displayed_text)
  87. cmd_enum += '<br><br>%s<br>%s<br><br>' % (
  88. _("Type help <command_name> for usage."), _("Example: help open_gerber"))
  89. self.app.shell.append_raw(cmd_enum)