highLight.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # -*- coding: utf-8 -*-
  2. import wx
  3. import re
  4. import sys
  5. if sys.platform != "darwin":
  6. font_mono = wx.Font(10, wx.ROMAN, wx.NORMAL, wx.NORMAL, faceName="Courier New")
  7. else:
  8. # 系统是 Mac OS X
  9. font_mono = wx.Font(12, wx.ROMAN, wx.NORMAL, wx.NORMAL, faceName="Monaco")
  10. def __highLightOneLine(txtctrl, ln, start_pos, styles):
  11. ln_content, t, ln_comment = ln.partition("#")
  12. end_pos = start_pos + len(ln)
  13. txtctrl.SetStyle(start_pos, end_pos, wx.TextAttr(styles["color_normal"], "#ffffff", styles["font_mono"]))
  14. # 行正文部分
  15. re_ip = re.match(r"^(\s*[\da-f\.:]+[\da-f]+)\s+\w", ln_content)
  16. if re_ip:
  17. s_ip = re_ip.group(1)
  18. pos2 = start_pos + len(s_ip)
  19. pos = pos2 - len(s_ip.lstrip())
  20. txtctrl.SetStyle(pos, pos2, wx.TextAttr(styles["color_ip"], "#ffffff", styles["font_mono"]))
  21. elif len(ln_content.strip()) > 0:
  22. pos2 = start_pos + len(ln_content)
  23. txtctrl.SetStyle(start_pos, pos2, wx.TextAttr(styles["color_error"], "#ffffff", styles["font_mono"]))
  24. # 行注释部分
  25. if t:
  26. pos = start_pos + len(ln_content)
  27. txtctrl.SetStyle(pos, end_pos, wx.TextAttr(styles["color_comment"], "#ffffff", styles["font_mono"]))
  28. def highLight(txtctrl, styles=None, old_content=None):
  29. default_style = {
  30. "color_normal": "#000000",
  31. "color_bg": "#ffffff",
  32. "color_comment": "#339933",
  33. "color_ip": "#0000cc",
  34. "color_error": "#ff0000",
  35. "font_mono": font_mono,
  36. }
  37. if styles:
  38. default_style.update(styles)
  39. styles = default_style
  40. content = txtctrl.Value.replace("\r", "")
  41. lns = content.split("\n")
  42. if old_content:
  43. old_content = old_content.replace("\r", "")
  44. lns_old = old_content.split("\n")
  45. else:
  46. lns_old = None
  47. pos = 0
  48. for idx, ln in enumerate(lns):
  49. ln_old = None
  50. if lns_old and idx < len(lns_old):
  51. ln_old = lns_old[idx]
  52. if not ln_old or ln != ln_old:
  53. __highLightOneLine(txtctrl, ln, pos, styles)
  54. pos += len(ln) + 1