Advertisement
Lateralus138

BoxComment() For AutoHotkey

Jun 24th, 2019
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;┌──────────────────────────────────────────────────────────────────────┐
  2. ;│ BoxComment Function - AutoHotkey                                     │
  3. ;│ Format comments in a box style                                       │
  4. ;│ for any comment type from the Clipboard                              │
  5. ;│ or a passed comment.                                                 │
  6. ;│ Default comment = ';'                                                │
  7. ;│ Default source     = Clipboard                                       │
  8. ;│     with the option to return results to Clipboard                   │
  9. ;│     or just use Send <SendRaw?>, % BoxComment()                      │
  10. ;│ Default tabconv = '4'                                                │
  11. ;│     Converts tabs to spaces - if you know a better way               │
  12. ;│     please let me know                                               │
  13. ;│ E.g.: BoxComment(,"This`nis`na`nmultiline`ncomment")                 │
  14. ;│ Returns:                                                             │
  15. ;│ ;┌───────────┐                                                       │
  16. ;│ ;│ This      │                                                       │
  17. ;│ ;│ is        │                                                       │
  18. ;│ ;│ a         │                                                       │
  19. ;│ ;│ multiline │                                                       │
  20. ;│ ;│ comment   │                                                       │
  21. ;│ ;└───────────┘                                                       │
  22. ;│ E.g.: BoxComment(,"This`n`tcomment`n`t`thas`n`t`ttabs")              │
  23. ;│ Returns:                                                             │
  24. ;│ ;┌─────────────┐                                                     │
  25. ;│ ;│ This        │                                                     │
  26. ;│ ;│     comment │                                                     │
  27. ;│ ;│         has │                                                     │
  28. ;│ ;│     tabs    │                                                     │
  29. ;│ ;└─────────────┘                                                     │
  30. ;│ This works for:                                                      │
  31. ;│     Notepad                                                          │
  32. ;│     Notepad                                                            │
  33. ;│     Sublime                                                          │
  34. ;│ Doesn't seem to work well with: (you can use notepad and paste back) │
  35. ;│     Atom                                                             │
  36. ;│     AHK studio                                                       │
  37. ;└──────────────────────────────────────────────────────────────────────┘
  38. BoxComment(source := "clip",tabconv := 4,comment := ";",returnClip := false)
  39. {   tabconv := IncChar(A_Space,tabconv)
  40.     source  :=  (   (source="clip")
  41.                 ?   StrSplit(Clipboard,"`r`n",tabconv)
  42.                 :   StrSplit(source,"`r`n",tabconv)     )
  43.     lenArray := {}
  44.     for idx, line in source
  45.     {   source[idx] := StrReplace(line,A_Tab,tabconv)
  46.         lenArray[StrLen(source[idx])] := source[idx]   
  47.     }
  48.     line := idx := ""
  49.     if ! (maxLen := lenArray.MaxIndex())
  50.         return
  51.     lenArray := ""
  52.     padding :=  {   "Left"  :   comment "│ "
  53.                 ,   "Right" :   " │`n"            }
  54.     corners :=  {   "TL"    :   comment "┌─"
  55.                 ,   "TR"    :   "─┐`n"
  56.                 ,   "BL"    :   comment "└─"
  57.                 ,   "BR"    :   "─┘"            }
  58.     loop,%maxLen%
  59.     {   line.="─"
  60.     }
  61.     top := corners.TL line corners.TR
  62.     bot := corners.BL line corners.BR
  63.     line := ""
  64.     frmt .= top
  65.     for idx, line in source
  66.     {   frmt .= padding.Left line IncChar(A_Space,maxLen-StrLen(line)) padding.Right
  67.     }
  68.     frmt .= bot
  69.     Clipboard := returnClip?frmt:Clipboard
  70.     return frmt
  71. }
  72. IncChar(char,inc:=2)
  73. {   loop,%inc%
  74.         ret.=char
  75.     return ret
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement