snakesay.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/env python3
  2. import sys
  3. import textwrap
  4. import random
  5. import json
  6. import os
  7. import platform
  8. import logging
  9. if os.getenv('DEBUG'):
  10. logging.basicConfig(level=logging.DEBUG)
  11. snake = """
  12. \\ ___
  13. \\ \\__| o \\
  14. / \\ |
  15. | | o
  16. __| |__ //
  17. |_______|//
  18. \\_______//
  19. """
  20. cow = """
  21. \\ ^__^
  22. \\ (oo)\\_______
  23. (__)\\ )\\/\\
  24. ||----w |
  25. || ||
  26. """
  27. tux = """
  28. \\
  29. \\
  30. _--_
  31. (o_ o)
  32. |<_) |
  33. _// \\\\
  34. (_| |_)
  35. | \___/ |
  36. \\__/---\\__/
  37. """
  38. share_dir = '/usr/local/share' if platform.system() == 'Darwin' else '/usr/share'
  39. if os.path.exists(f"{share_dir}/fortunes.txt") == True:
  40. with open(f"{share_dir}/fortunes.txt", "r") as f:
  41. fortunes = f.read()
  42. fortunes = fortunes.splitlines()
  43. else:
  44. logging.debug('Not using fortunes.txt from share dir')
  45. with open("fortunes.txt", "r") as f:
  46. fortunes = f.read()
  47. fortunes = fortunes.splitlines()
  48. def error():
  49. print("ERROR: GIVE ME TEXT!")
  50. quit()
  51. if len(sys.argv) < 2:
  52. error()
  53. args = sys.argv[1:]
  54. text = ''
  55. thing = snake
  56. if "-fortune" in args:
  57. text = fortunes[random.randint(0, len(fortunes))]
  58. thing = snake
  59. if "-r" in args:
  60. rand = random.randint(1, 3)
  61. if rand == 1:
  62. thing = snake
  63. if rand == 2:
  64. thing = cow
  65. if rand == 3:
  66. thing = tux
  67. index = args.index('-r')
  68. if not '-fortune' in args:
  69. text = ' '.join(args[index+1:])
  70. elif "-s" in args:
  71. thing = snake
  72. index = args.index('-s')
  73. if not '-fortune' in args:
  74. text = ' '.join(args[index+1:])
  75. elif "-c" in args:
  76. thing = cow
  77. index = args.index('-c')
  78. if not '-fortune' in args:
  79. text = ' '.join(args[index+1:])
  80. elif "-t" in args:
  81. thing = tux
  82. index = args.index('-t')
  83. if not '-fortune' in args:
  84. text = ' '.join(args[index+1:])
  85. elif "-h" in args or "--help" in args:
  86. print(f"""This is a less feature rewrite of cowsay in python.
  87. python {sys.argv[0]} QUERY - make a snake say something
  88. python {sys.argv[0]} -fortune - make a snake say a random fortune. Requires requests installed.
  89. python {sys.argv[0]} -r QUERY - randomly selected a tux, cow or snake will say something
  90. python {sys.argv[0]} -t QUERY - a tux will say something
  91. python {sys.argv[0]} -s QUERY - a snake will say something
  92. python {sys.argv[0]} -c QUERY - a cow will say something""")
  93. quit()
  94. else:
  95. if not text:
  96. text = ' '.join(args)
  97. if text == "":
  98. error()
  99. dashes = len(text) + 2
  100. if dashes > 30:
  101. dashes = 30
  102. print(" " + "-"*dashes)
  103. text = textwrap.fill(text, dashes-2).split("\n")
  104. for enter in text:
  105. stuff = "| " + enter
  106. length = dashes - len(stuff)
  107. print(stuff + " "*length+" |")
  108. print(" "+"-"*dashes)
  109. else:
  110. print(" "+"-"*dashes)
  111. print("| "+text+" |")
  112. print(" "+"-"*dashes)
  113. print(thing)