makes an ASCII-art comic in the style of webcomicname.com
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.

135 lines
4.0 KiB

5 years ago
  1. #!/usr/bin/env python
  2. # Copyright 2019 Colin McMillen (https://twitter.com/mcmillen).
  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
  6. # deal in the Software without restriction, including without limitation the
  7. # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. # sell 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
  19. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. # IN THE SOFTWARE.
  21. """makes an ASCII-art comic in the style of webcomicname.com."""
  22. import re
  23. import sys
  24. USAGE = """Usage:
  25. $ ohnosay.py "you can make a single panel of text"
  26. $ ohnosay.py "you can also make two panels" "but three isn't supported"
  27. """
  28. TEMPLATE = r'''
  29. /--------------------------\ |
  30. | aaaaaaaaaaaaaaaaaaaaaaaa | |
  31. \_ ________________________/ |
  32. v |
  33. _____ |
  34. / . . \ |
  35. \ | _ | / |
  36. v v |'''
  37. TO_REPLACE = 'aaaaaaaaaaaaaaaaaaaaaaaa'
  38. OH_NO = r'''
  39. /------\
  40. | ohno |
  41. \_ ____/
  42. v
  43. _____
  44. / . . \
  45. _ | _ | _
  46. v v'''
  47. def chunk_lines(s):
  48. """Splits a string into a list of lines that fit inside the speech bubble."""
  49. if len(s) <= len(TO_REPLACE):
  50. return [s]
  51. try:
  52. split_position = s[:len(TO_REPLACE) + 1].rindex(' ')
  53. except ValueError: # some word is too big to fit! oh no
  54. split_position = len(TO_REPLACE)
  55. return [s[:split_position]] + chunk_lines(s[split_position + 1:])
  56. def main(args):
  57. """does some things. oh no"""
  58. if not args:
  59. print USAGE
  60. return
  61. single_panel = False
  62. if len(args) == 1:
  63. single_panel = True
  64. args.append('')
  65. lines_1 = chunk_lines(args[0])
  66. lines_2 = chunk_lines(args[1])
  67. # the user input has to be at least two lines tall, because the final 'oh no'
  68. # panel is two lines tall and cutting out the second line would be... oh no
  69. if len(lines_1) == 1:
  70. lines_1.append('')
  71. # incredibly cheesy vertical centering
  72. while len(lines_1) < len(lines_2):
  73. if len(lines_1) == len(lines_2) - 1:
  74. lines_1.append('')
  75. else:
  76. lines_1 = [''] + lines_1 + ['']
  77. while len(lines_2) < len(lines_1):
  78. if len(lines_2) == len(lines_1) - 1:
  79. lines_2.append('')
  80. else:
  81. lines_2 = [''] + lines_2 + ['']
  82. # now both lines are the same length
  83. num_lines = len(lines_1)
  84. comic = re.sub('-', u'\u203e', TEMPLATE).split('\n')
  85. oh_no = re.sub('-', u'\u203e', OH_NO).split('\n')
  86. # this got hacky and gross. oh no
  87. for i, _ in enumerate(comic):
  88. if i == 2:
  89. for j in xrange(num_lines):
  90. left_centered = lines_1[j].center(len(TO_REPLACE))
  91. left = re.sub(TO_REPLACE, left_centered, comic[2])
  92. middle_centered = lines_2[j].center(len(TO_REPLACE))
  93. middle = re.sub(TO_REPLACE, middle_centered, comic[2])
  94. if single_panel:
  95. middle = ''
  96. if j == num_lines - 3:
  97. print left + middle + oh_no[1]
  98. elif j == num_lines - 2:
  99. print left + middle + ' | oh |'
  100. elif j == num_lines - 1:
  101. print left + middle + ' | no |'
  102. else:
  103. print left + middle
  104. else:
  105. oh_no_line = oh_no[i]
  106. if num_lines > 2 and i == 1:
  107. oh_no_line = ''
  108. if single_panel:
  109. print comic[i] + oh_no_line
  110. else:
  111. print comic[i] + comic[i] + oh_no_line
  112. if __name__ == '__main__':
  113. main(sys.argv[1:])