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.

DefaultInputModel.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc;
  18. import com.dmdirc.commandparser.parsers.CommandParser;
  19. import com.dmdirc.interfaces.InputModel;
  20. import com.dmdirc.ui.input.TabCompleter;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.function.Consumer;
  24. import java.util.function.Supplier;
  25. /**
  26. * Basic implementation of {@link InputModel}.
  27. */
  28. public class DefaultInputModel implements InputModel {
  29. private final Consumer<String> lineConsumer;
  30. private final CommandParser commandParser;
  31. private final TabCompleter tabCompleter;
  32. private final Supplier<Integer> lineLengthSupplier;
  33. public DefaultInputModel(final Consumer<String> lineConsumer, final CommandParser commandParser,
  34. final TabCompleter tabCompleter, final Supplier<Integer> lineLengthSupplier) {
  35. this.lineConsumer = lineConsumer;
  36. this.commandParser = commandParser;
  37. this.tabCompleter = tabCompleter;
  38. this.lineLengthSupplier = lineLengthSupplier;
  39. }
  40. @Override
  41. public void sendLine(final String line) {
  42. lineConsumer.accept(line);
  43. }
  44. @Override
  45. public CommandParser getCommandParser() {
  46. return commandParser;
  47. }
  48. @Override
  49. public TabCompleter getTabCompleter() {
  50. return tabCompleter;
  51. }
  52. @Override
  53. public int getMaxLineLength() {
  54. return lineLengthSupplier.get();
  55. }
  56. @Override
  57. public List<String> splitLine(final String line) {
  58. final List<String> result = new ArrayList<>();
  59. if (line.indexOf('\n') > -1) {
  60. for (String part : line.split("\n")) {
  61. result.addAll(splitLine(part));
  62. }
  63. } else {
  64. final StringBuilder remaining = new StringBuilder(line);
  65. while (getMaxLineLength() > -1 && remaining.toString().getBytes().length
  66. > getMaxLineLength()) {
  67. int number = Math.min(remaining.length(), getMaxLineLength());
  68. while (remaining.substring(0, number).getBytes().length > getMaxLineLength()) {
  69. number--;
  70. }
  71. result.add(remaining.substring(0, number));
  72. remaining.delete(0, number);
  73. }
  74. result.add(remaining.toString());
  75. }
  76. return result;
  77. }
  78. @Override
  79. public final int getNumLines(final String line) {
  80. final String[] splitLines = line.split("(\n|\r\n|\r)", Integer.MAX_VALUE);
  81. int lines = 0;
  82. for (String splitLine : splitLines) {
  83. if (getMaxLineLength() <= 0) {
  84. lines++;
  85. } else {
  86. lines += (int) Math.ceil(splitLine.getBytes().length
  87. / (double) getMaxLineLength());
  88. }
  89. }
  90. return lines;
  91. }
  92. }