Format.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
  4. * Copyright (c) 2023, Shannon Booth <shannon.ml.booth@gmail.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "Format.h"
  9. #include <AK/DeprecatedString.h>
  10. #include <AK/Stream.h>
  11. #include <AK/StringBuilder.h>
  12. #include <AK/Vector.h>
  13. namespace Diff {
  14. DeprecatedString generate_only_additions(StringView text)
  15. {
  16. auto lines = text.split_view('\n', SplitBehavior::KeepEmpty);
  17. StringBuilder builder;
  18. builder.appendff("@@ -0,0 +1,{} @@\n", lines.size());
  19. for (auto const& line : lines) {
  20. builder.appendff("+{}\n", line);
  21. }
  22. return builder.to_deprecated_string();
  23. }
  24. ErrorOr<void> write_unified(Hunk const& hunk, Stream& stream, ColorOutput color_output)
  25. {
  26. TRY(stream.write_formatted("{}\n", hunk.location));
  27. if (color_output == ColorOutput::Yes) {
  28. for (auto const& line : hunk.lines) {
  29. if (line.operation == Line::Operation::Addition)
  30. TRY(stream.write_formatted("\033[32;1m{}\033[0m\n", line));
  31. else if (line.operation == Line::Operation::Removal)
  32. TRY(stream.write_formatted("\033[31;1m{}\033[0m\n", line));
  33. else
  34. TRY(stream.write_formatted("{}\n", line));
  35. }
  36. } else {
  37. for (auto const& line : hunk.lines)
  38. TRY(stream.write_formatted("{}\n", line));
  39. }
  40. return {};
  41. }
  42. ErrorOr<void> write_normal(Hunk const& hunk, Stream& stream, ColorOutput color_output)
  43. {
  44. // Source line(s)
  45. TRY(stream.write_formatted("{}", hunk.location.old_range.start_line));
  46. if (hunk.location.old_range.number_of_lines > 1)
  47. TRY(stream.write_formatted(",{}", (hunk.location.old_range.start_line + hunk.location.old_range.number_of_lines - 1)));
  48. // Action
  49. if (hunk.location.old_range.number_of_lines > 0 && hunk.location.new_range.number_of_lines > 0)
  50. TRY(stream.write_formatted("c"));
  51. else if (hunk.location.new_range.number_of_lines > 0)
  52. TRY(stream.write_formatted("a"));
  53. else
  54. TRY(stream.write_formatted("d"));
  55. // Target line(s)
  56. TRY(stream.write_formatted("{}", hunk.location.new_range.start_line));
  57. if (hunk.location.new_range.number_of_lines > 1)
  58. TRY(stream.write_formatted(",{}", (hunk.location.new_range.start_line + hunk.location.new_range.number_of_lines - 1)));
  59. TRY(stream.write_formatted("\n"));
  60. for (auto const& line : hunk.lines) {
  61. VERIFY(line.operation == Line::Operation::Removal || line.operation == Line::Operation::Addition);
  62. if (line.operation == Line::Operation::Addition) {
  63. if (color_output == ColorOutput::Yes)
  64. TRY(stream.write_formatted("\033[32;1m> {}\033[0m\n", line.content));
  65. else
  66. TRY(stream.write_formatted("> {}\n", line.content));
  67. } else {
  68. if (color_output == ColorOutput::Yes)
  69. TRY(stream.write_formatted("\033[31;1m< {}\033[0m\n", line.content));
  70. else
  71. TRY(stream.write_formatted("< {}\n", line.content));
  72. }
  73. }
  74. return {};
  75. }
  76. }