You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Command.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.commandparser.commands;
  23. import com.dmdirc.commandparser.CommandManager;
  24. import com.dmdirc.ui.interfaces.InputWindow;
  25. import com.dmdirc.ui.messages.Styliser;
  26. /**
  27. * Represents a generic command.
  28. *
  29. * @author chris
  30. */
  31. public abstract class Command {
  32. /** The format name used for command output. */
  33. protected static final String FORMAT_OUTPUT = "commandOutput";
  34. /** The format name used for command errors. */
  35. protected static final String FORMAT_ERROR = "commandError";
  36. /**
  37. * Implodes the given list of arguments.
  38. * @param offset The index to start at
  39. * @param args The arguments to implode
  40. * @return A string containing each argument seperated by a space
  41. */
  42. protected static final String implodeArgs(final int offset, final String... args) {
  43. String res = "";
  44. for (int i = offset; i < args.length; i++) {
  45. if (res.isEmpty()) {
  46. res = args[i];
  47. } else {
  48. res = res.concat(" " + args[i]);
  49. }
  50. }
  51. return res;
  52. }
  53. /**
  54. * Implodes the given list of arguments.
  55. * @param args The arguments to implode
  56. * @return A string containing each argument seperated by a space
  57. */
  58. protected static final String implodeArgs(final String... args) {
  59. return implodeArgs(0, args);
  60. }
  61. /**
  62. * Sends a line, if appropriate, to the specified target.
  63. * @param target The command window to send the line to
  64. * @param isSilent Whether this command is being silenced or not
  65. * @param type The type of message to send
  66. * @param args The arguments of the message
  67. */
  68. protected final void sendLine(final InputWindow target,
  69. final boolean isSilent, final String type, final Object ... args) {
  70. if (!isSilent && target != null) {
  71. target.addLine(type, args);
  72. }
  73. }
  74. /**
  75. * Sends a usage line, if appropriate, to the specified target.
  76. *
  77. * @param target The command window to send the line to
  78. * @param isSilent Whether this command is being silenced or not
  79. * @param name The name of the command that's raising the error
  80. * @param args The arguments that the command accepts or expects
  81. */
  82. protected final void showUsage(final InputWindow target,
  83. final boolean isSilent, final String name, final String args) {
  84. sendLine(target, isSilent, "commandUsage", CommandManager.getCommandChar(),
  85. name, args);
  86. }
  87. /**
  88. * Formats the specified data into a table suitable for output in the
  89. * textpane. It is expected that each String[] in data has the same number
  90. * of elements as the headers array.
  91. *
  92. * @param headers The headers of the table.
  93. * @param data The contents of the table.
  94. * @return A string containing an ASCII table
  95. */
  96. protected static String doTable(final String[] headers, final String[][] data) {
  97. final StringBuilder res = new StringBuilder();
  98. res.append(Styliser.CODE_FIXED);
  99. res.append(Styliser.CODE_BOLD);
  100. int[] maxsizes = new int[headers.length];
  101. for (int i = 0; i < headers.length; i++) {
  102. maxsizes[i] = headers[i].length() + 3;
  103. for (int j = 0; j < data.length; j++) {
  104. maxsizes[i] = Math.max(maxsizes[i], data[j][i].length() + 3);
  105. }
  106. doPadding(res, headers[i], maxsizes[i]);
  107. }
  108. for (String[] source : data) {
  109. res.append('\n');
  110. res.append(Styliser.CODE_FIXED);
  111. for (int i = 0; i < source.length; i++) {
  112. doPadding(res, source[i], maxsizes[i]);
  113. }
  114. }
  115. return res.toString();
  116. }
  117. /**
  118. * Adds the specified data to the stringbuilder, padding with spaces to
  119. * the specified size.
  120. *
  121. * @param builder The stringbuilder to append data to
  122. * @param data The data to be added
  123. * @param size The minimum size that should be used
  124. */
  125. private static void doPadding(final StringBuilder builder, final String data,
  126. final int size) {
  127. builder.append(data);
  128. for (int i = 0; i < size - data.length(); i++) {
  129. builder.append(' ');
  130. }
  131. }
  132. }