PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/atlassian/maven/plugins/jgitflow/PrettyPrompter.java

https://bitbucket.org/marcuslinke/maven-jgitflow-plugin
Java | 479 lines | 383 code | 85 blank | 11 comment | 38 complexity | 6475da2d26dd95eb259424a587230c1e MD5 | raw file
  1. package com.atlassian.maven.plugins.jgitflow;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import org.codehaus.plexus.components.interactivity.InputHandler;
  7. import org.codehaus.plexus.components.interactivity.OutputHandler;
  8. import org.codehaus.plexus.components.interactivity.Prompter;
  9. import org.codehaus.plexus.components.interactivity.PrompterException;
  10. import org.codehaus.plexus.util.StringUtils;
  11. import jline.ANSIBuffer;
  12. /**
  13. * @since version
  14. */
  15. public class PrettyPrompter implements Prompter
  16. {
  17. //maven-cli-plugin uses an old version jline that has ansi codes in package scope.
  18. //re-defining them in public here
  19. public static final int OFF = 0;
  20. public static final int BOLD = 1;
  21. public static final int UNDERSCORE = 4;
  22. public static final int BLINK = 5;
  23. public static final int REVERSE = 7;
  24. public static final int CONCEALED = 8;
  25. public static final int FG_BLACK = 30;
  26. public static final int FG_RED = 31;
  27. public static final int FG_GREEN = 32;
  28. public static final int FG_YELLOW = 33;
  29. public static final int FG_BLUE = 34;
  30. public static final int FG_MAGENTA = 35;
  31. public static final int FG_CYAN = 36;
  32. public static final int FG_WHITE = 37;
  33. public static final char ESC = 27;
  34. /**
  35. * @requirement
  36. */
  37. private OutputHandler outputHandler;
  38. /**
  39. * @requirement
  40. */
  41. private InputHandler inputHandler;
  42. private boolean useAnsiColor;
  43. public PrettyPrompter()
  44. {
  45. String mavencolor = System.getenv("MAVEN_COLOR");
  46. if (mavencolor != null && !mavencolor.equals(""))
  47. {
  48. useAnsiColor = Boolean.parseBoolean(mavencolor);
  49. } else
  50. {
  51. useAnsiColor = false;
  52. }
  53. }
  54. public String promptNotBlank(String message) throws PrompterException
  55. {
  56. return promptNotBlank(message, null);
  57. }
  58. public String promptNotBlank(String message, String defaultValue) throws PrompterException
  59. {
  60. String value;
  61. if (StringUtils.isBlank(defaultValue))
  62. {
  63. value = prompt(requiredMessage(message));
  64. } else
  65. {
  66. value = prompt(message, defaultValue);
  67. }
  68. if (StringUtils.isBlank(value))
  69. {
  70. value = promptNotBlank(message, defaultValue);
  71. }
  72. return value;
  73. }
  74. public String requiredMessage(String message)
  75. {
  76. String formattedMessage = message;
  77. if (useAnsiColor)
  78. {
  79. ANSIBuffer ansiBuffer = new ANSIBuffer();
  80. ansiBuffer.append(ANSIBuffer.ANSICodes
  81. .attrib(PrettyPrompter.BOLD))
  82. .append(ANSIBuffer.ANSICodes
  83. .attrib(PrettyPrompter.FG_RED))
  84. .append(message)
  85. .append(ANSIBuffer.ANSICodes
  86. .attrib(PrettyPrompter.OFF));
  87. formattedMessage = ansiBuffer.toString();
  88. }
  89. return formattedMessage;
  90. }
  91. public String prompt(String message)
  92. throws PrompterException
  93. {
  94. try
  95. {
  96. writePrompt(message);
  97. } catch (IOException e)
  98. {
  99. throw new PrompterException("Failed to present prompt", e);
  100. }
  101. try
  102. {
  103. return StringUtils.trim(inputHandler.readLine());
  104. } catch (IOException e)
  105. {
  106. throw new PrompterException("Failed to read user response", e);
  107. }
  108. }
  109. public String prompt(String message, String defaultReply)
  110. throws PrompterException
  111. {
  112. try
  113. {
  114. writePrompt(formatMessage(message, null, defaultReply));
  115. } catch (IOException e)
  116. {
  117. throw new PrompterException("Failed to present prompt", e);
  118. }
  119. try
  120. {
  121. String line = inputHandler.readLine();
  122. if (StringUtils.isEmpty(line))
  123. {
  124. line = defaultReply;
  125. }
  126. return StringUtils.trim(line);
  127. } catch (IOException e)
  128. {
  129. throw new PrompterException("Failed to read user response", e);
  130. }
  131. }
  132. public String prompt(String message, List possibleValues, String defaultReply)
  133. throws PrompterException
  134. {
  135. String formattedMessage = formatMessage(message, possibleValues, defaultReply);
  136. String line;
  137. do
  138. {
  139. try
  140. {
  141. writePrompt(formattedMessage);
  142. } catch (IOException e)
  143. {
  144. throw new PrompterException("Failed to present prompt", e);
  145. }
  146. try
  147. {
  148. line = inputHandler.readLine();
  149. } catch (IOException e)
  150. {
  151. throw new PrompterException("Failed to read user response", e);
  152. }
  153. if (StringUtils.isEmpty(line))
  154. {
  155. line = defaultReply;
  156. }
  157. if (line != null && !possibleValues.contains(line))
  158. {
  159. try
  160. {
  161. String invalid = "Invalid selection.";
  162. if (useAnsiColor)
  163. {
  164. ANSIBuffer ansiBuffer = new ANSIBuffer();
  165. ansiBuffer.append(ANSIBuffer.ANSICodes
  166. .attrib(FG_RED))
  167. .append(ANSIBuffer.ANSICodes
  168. .attrib(BOLD))
  169. .append("Invalid selection.")
  170. .append(ANSIBuffer.ANSICodes
  171. .attrib(OFF));
  172. invalid = ansiBuffer.toString();
  173. }
  174. outputHandler.writeLine(invalid);
  175. } catch (IOException e)
  176. {
  177. throw new PrompterException("Failed to present feedback", e);
  178. }
  179. }
  180. }
  181. while (line == null || !possibleValues.contains(line));
  182. return StringUtils.trim(line);
  183. }
  184. public String promptNumberedList(String message, List<String> possibleValues) throws PrompterException
  185. {
  186. return promptNumberedList(message,possibleValues,null);
  187. }
  188. public String promptNumberedList(String message, List<String> possibleValues, String defaultValue) throws PrompterException
  189. {
  190. MessageAndAnswers ma = formatNumberedMessage(message, possibleValues,defaultValue);
  191. String answer = prompt(ma.message, ma.answers, ma.defaultAnswer);
  192. int answerInt = Integer.parseInt(answer);
  193. return possibleValues.get((answerInt -1));
  194. }
  195. public String prompt(String message, List possibleValues)
  196. throws PrompterException
  197. {
  198. return prompt(message, possibleValues, null);
  199. }
  200. public String promptForPassword(String message)
  201. throws PrompterException
  202. {
  203. try
  204. {
  205. writePrompt(message);
  206. } catch (IOException e)
  207. {
  208. throw new PrompterException("Failed to present prompt", e);
  209. }
  210. try
  211. {
  212. return inputHandler.readPassword();
  213. } catch (IOException e)
  214. {
  215. throw new PrompterException("Failed to read user response", e);
  216. }
  217. }
  218. protected String formatMessage(String message, List possibleValues, String defaultReply)
  219. {
  220. if (useAnsiColor)
  221. {
  222. return formatAnsiMessage(message, possibleValues, defaultReply);
  223. } else
  224. {
  225. return formatPlainMessage(message, possibleValues, defaultReply);
  226. }
  227. }
  228. private MessageAndAnswers formatNumberedMessage(String message, List<String> possibleValues, String defaultValue)
  229. {
  230. if (useAnsiColor)
  231. {
  232. return formatNumberedAnsiMessage(message, possibleValues, defaultValue);
  233. }
  234. else
  235. {
  236. return formatNumberedPlainMessage(message, possibleValues, defaultValue);
  237. }
  238. }
  239. private String formatAnsiMessage(String message, List possibleValues, String defaultReply)
  240. {
  241. ANSIBuffer formatted = new ANSIBuffer();
  242. formatted.append(message);
  243. if (possibleValues != null && !possibleValues.isEmpty())
  244. {
  245. formatted.append(" (");
  246. for (Iterator it = possibleValues.iterator(); it.hasNext(); )
  247. {
  248. String possibleValue = (String) it.next();
  249. formatted.attrib(possibleValue, BOLD);
  250. if (it.hasNext())
  251. {
  252. formatted.append("/");
  253. }
  254. }
  255. formatted.append(")");
  256. }
  257. if (defaultReply != null)
  258. {
  259. formatted.append(ANSIBuffer.ANSICodes
  260. .attrib(FG_GREEN))
  261. .append(ANSIBuffer.ANSICodes
  262. .attrib(BOLD))
  263. .append(" [")
  264. .append(defaultReply)
  265. .append("]")
  266. .append(ANSIBuffer.ANSICodes
  267. .attrib(OFF));
  268. }
  269. return formatted.toString();
  270. }
  271. private MessageAndAnswers formatNumberedAnsiMessage(String message, List<String> possibleValues, String defaultValue)
  272. {
  273. ANSIBuffer formatted = new ANSIBuffer();
  274. formatted.bold(message).append("\n");
  275. List<String> answers = new ArrayList<String>();
  276. String defaultAnswer = "1";
  277. int counter = 1;
  278. for (String val : possibleValues)
  279. {
  280. String answer = String.valueOf(counter);
  281. if(val.equals(defaultValue))
  282. {
  283. formatted.bold(answer);
  284. defaultAnswer = answer;
  285. }
  286. else
  287. {
  288. formatted.append(answer);
  289. }
  290. if (counter < 10)
  291. {
  292. formatted.append(": ");
  293. } else
  294. {
  295. formatted.append(": ");
  296. }
  297. if(val.equals(defaultValue))
  298. {
  299. formatted.bold(val).append("\n");
  300. }
  301. else
  302. {
  303. formatted.append(val).append("\n");
  304. }
  305. answers.add(answer);
  306. counter++;
  307. }
  308. formatted.bold("Choose a number");
  309. return new MessageAndAnswers(formatted.toString(),answers,defaultAnswer);
  310. }
  311. private String formatPlainMessage(String message, List possibleValues, String defaultReply)
  312. {
  313. StringBuffer formatted = new StringBuffer(message.length() * 2);
  314. formatted.append(message);
  315. if (possibleValues != null && !possibleValues.isEmpty())
  316. {
  317. formatted.append(" (");
  318. for (Iterator it = possibleValues.iterator(); it.hasNext(); )
  319. {
  320. String possibleValue = (String) it.next();
  321. formatted.append(possibleValue);
  322. if (it.hasNext())
  323. {
  324. formatted.append('/');
  325. }
  326. }
  327. formatted.append(')');
  328. }
  329. if (defaultReply != null)
  330. {
  331. formatted.append(" [")
  332. .append(defaultReply)
  333. .append("]");
  334. }
  335. return formatted.toString();
  336. }
  337. private MessageAndAnswers formatNumberedPlainMessage(String message, List<String> possibleValues, String defaultValue)
  338. {
  339. StringBuffer formatted = new StringBuffer();
  340. formatted.append(message).append("\n");
  341. List<String> answers = new ArrayList<String>();
  342. int counter = 1;
  343. String defaultAnswer = "1";
  344. for (String val : possibleValues)
  345. {
  346. String answer = String.valueOf(counter);
  347. formatted.append(answer);
  348. if(val.equals(defaultValue))
  349. {
  350. defaultAnswer = answer;
  351. }
  352. if (counter < 10)
  353. {
  354. formatted.append(": ");
  355. } else
  356. {
  357. formatted.append(": ");
  358. }
  359. formatted.append(val).append("\n");
  360. answers.add(answer);
  361. counter++;
  362. }
  363. formatted.append("Choose a number");
  364. return new MessageAndAnswers(formatted.toString(),answers,defaultAnswer);
  365. }
  366. private void writePrompt(String message)
  367. throws IOException
  368. {
  369. outputHandler.write(message + ": ");
  370. }
  371. public void showMessage(String message)
  372. throws PrompterException
  373. {
  374. try
  375. {
  376. writePrompt(message);
  377. } catch (IOException e)
  378. {
  379. throw new PrompterException("Failed to present prompt", e);
  380. }
  381. }
  382. private class MessageAndAnswers
  383. {
  384. private final String message;
  385. private final List<String> answers;
  386. private final String defaultAnswer;
  387. private MessageAndAnswers(String message, List<String> answers, String defaultAnswer)
  388. {
  389. this.message = message;
  390. this.answers = answers;
  391. this.defaultAnswer = defaultAnswer;
  392. }
  393. }
  394. }