TclCommandHelp.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 math
  11. import gettext
  12. import FlatCAMTranslation as fcTranslate
  13. import builtins
  14. fcTranslate.apply_language('strings')
  15. if '_' not in builtins.__dict__:
  16. _ = gettext.gettext
  17. class TclCommandHelp(TclCommand):
  18. """
  19. Tcl shell command to get the value of a system variable
  20. example:
  21. get_sys excellon_zeros
  22. """
  23. # List of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  24. aliases = ['help']
  25. description = '%s %s' % ("--", "PRINTS to TCL the HELP.")
  26. # Dictionary of types from Tcl command, needs to be ordered
  27. arg_names = collections.OrderedDict([
  28. ('name', str)
  29. ])
  30. # Dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  31. option_types = collections.OrderedDict([
  32. ])
  33. # array of mandatory options for current Tcl command: required = {'name','outname'}
  34. required = []
  35. # structured help for current command, args needs to be ordered
  36. help = {
  37. 'main': "Returns to TCL the value for the entered system variable.",
  38. 'args': collections.OrderedDict([
  39. ('name', 'Name of a Tcl Command for which to display the Help.'),
  40. ]),
  41. 'examples': ['help add_circle']
  42. }
  43. def execute(self, args, unnamed_args):
  44. """
  45. :param args:
  46. :param unnamed_args:
  47. :return:
  48. """
  49. print(self.app.tcl_commands_storage)
  50. if 'name' in args:
  51. name = args['name']
  52. if name not in self.app.tcl_commands_storage:
  53. return "Unknown command: %s" % name
  54. print(self.app.tcl_commands_storage[name]["help"])
  55. else:
  56. if args is None:
  57. cmd_enum = _("Available commands:\n")
  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.tcl_commands_storage:
  63. curr_len = len(cmd_name)
  64. if curr_len > max_len:
  65. max_len = curr_len
  66. max_tabs = math.ceil(max_len / 8)
  67. for cmd_name in sorted(self.app.tcl_commands_storage):
  68. cmd_description = self.app.tcl_commands_storage[cmd_name]['description']
  69. curr_len = len(cmd_name)
  70. tabs = '\t'
  71. cmd_name_colored = "<span style=\" color:#ff0000;\" >"
  72. cmd_name_colored += str(cmd_name)
  73. cmd_name_colored += "</span>"
  74. # make sure to add the right number of tabs (1 tab = 8 spaces) so all the commands
  75. # descriptions are aligned
  76. if curr_len == max_len:
  77. cmd_line_txt = ' %s%s%s' % (cmd_name_colored, tabs, cmd_description)
  78. else:
  79. nr_tabs = 0
  80. for x in range(max_tabs):
  81. if curr_len <= (x * 8):
  82. nr_tabs += 1
  83. # nr_tabs = 2 if curr_len <= 8 else 1
  84. cmd_line_txt = ' %s%s%s' % (cmd_name_colored, nr_tabs * tabs, cmd_description)
  85. displayed_text.append(cmd_line_txt)
  86. except Exception as err:
  87. self.app.log.debug("App.setup_shell.shelp() when run as 'help' --> %s" % str(err))
  88. displayed_text = [' %s' % cmd for cmd in sorted(self.app.tcl_commands_storage)]
  89. cmd_enum += '\n'.join(displayed_text)
  90. cmd_enum += '\n\n%s\n%s' % (_("Type help <command_name> for usage."), _("Example: help open_gerber"))
  91. print(cmd_enum)