demo_logger.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Part of ImGui Bundle - MIT License - Copyright (c) 2022-2026 Pascal Thomet - https://github.com/pthom/imgui_bundle
  2. import random
  3. from imgui_bundle import imgui, hello_imgui, imgui_md, immapp
  4. @immapp.static(idx_fortune=0, added_logs=False)
  5. def demo_gui():
  6. static = demo_gui
  7. fortunes = [
  8. "If at first you don't succeed, skydiving is not for you.",
  9. "You will be a winner today. Pick a fight.",
  10. "The world may be your oyster, but it doesn't mean you'll get its pearl.",
  11. "Borrow money from a pessimist, they don't expect it back.",
  12. "You will be hungry again in an hour.",
  13. "A closed mouth gathers no foot.",
  14. "Today, you will invent the wheel...again.",
  15. "If you can't convince them, confuse them.",
  16. "The journey of a thousand miles begins with a single step, or a really good map.",
  17. "You will find a pot of gold at the end of a rainbow, but it'll be someone else's.",
  18. "Opportunities will knock on your door, but don't worry, they'll be gone by the time you get up to answer.",
  19. "You will have a long and healthy life...and a very boring one.",
  20. "A wise man once said nothing.",
  21. "You will have a great day...tomorrow.",
  22. "The only thing constant in life is change, except for death and taxes, those are pretty constant too.",
  23. ]
  24. def add_logs():
  25. for _i in range(10):
  26. log_level = random.choice(
  27. [
  28. hello_imgui.LogLevel.debug,
  29. hello_imgui.LogLevel.info,
  30. hello_imgui.LogLevel.warning,
  31. hello_imgui.LogLevel.error,
  32. ]
  33. )
  34. hello_imgui.log(log_level, fortunes[static.idx_fortune])
  35. static.idx_fortune += 1
  36. if static.idx_fortune >= len(fortunes):
  37. static.idx_fortune = 0
  38. if not static.added_logs:
  39. add_logs()
  40. static.added_logs = True
  41. imgui_md.render_unindented(
  42. """
  43. # Graphical logger for ImGui
  44. This logger is adapted from [ImGuiAl](https://github.com/leiradel/ImGuiAl)
  45. Its colors are computed automatically from the WindowBg color, in order to remain readable when the theme is changed.
  46. """
  47. )
  48. imgui.separator()
  49. if imgui.button("Add logs"):
  50. for _i in range(10):
  51. add_logs()
  52. imgui.separator()
  53. hello_imgui.log_gui()
  54. def main():
  55. immapp.run(demo_gui, "Log", with_markdown=True)
  56. if __name__ == "__main__":
  57. main()