TclCommandHelp.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 FlatCAMTranslation 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.tcl_commands_storage:
  52. return "Unknown command: %s" % name
  53. self.app.shell.append_output(self.app.tcl_commands_storage[name]["help"])
  54. else:
  55. if not args:
  56. cmd_enum = '%s\n' % _("Available commands:")
  57. displayed_text = []
  58. try:
  59. # find the maximum length of a command name
  60. max_len = 0
  61. for cmd_name in self.app.tcl_commands_storage:
  62. curr_len = len(cmd_name)
  63. if curr_len > max_len:
  64. max_len = curr_len
  65. h_space = " "
  66. cnt = 0
  67. for cmd_name in sorted(self.app.tcl_commands_storage):
  68. cmd_description = "<span>%s</span>" % self.app.tcl_commands_storage[cmd_name]['description']
  69. curr_len = len(cmd_name)
  70. cmd_name_colored = "<span style=\" font-weight: bold; color: red;\" >%s</span>" % str(cmd_name)
  71. nr_chars = max_len - curr_len
  72. cmd_line_txt = '%s%s&nbsp;&nbsp;%s' % (cmd_name_colored, nr_chars * h_space, cmd_description)
  73. displayed_text.append(cmd_line_txt)
  74. # group commands by 4 adding a line break after 4 commands
  75. if cnt == 3:
  76. cnt = 0
  77. displayed_text.append(' ')
  78. else:
  79. cnt += 1
  80. except Exception as err:
  81. self.app.log.debug("App.setup_shell.shelp() when run as 'help' --> %s" % str(err))
  82. displayed_text = ['> %s\n' % cmd for cmd in sorted(self.app.tcl_commands_storage)]
  83. cmd_enum += '<br>'.join(displayed_text)
  84. cmd_enum += '<br><br>%s<br>%s<br>' % (
  85. _("Type help <command_name> for usage."), _("Example: help open_gerber"))
  86. self.app.shell.append_raw(cmd_enum)