A script for centering "fold comments"

Post your working scripts, libraries and tools for AHK v1.1 and older
Kotte
Posts: 46
Joined: 03 May 2021, 08:33

A script for centering "fold comments"

Post by Kotte » 22 Jan 2022, 02:54

I don't know what the proper name is for the following, so I'll call it fold comments for now.

Code: Select all

{ ;~---------------------------------------------- TEXT --------------------------------------------------
Some code...
}
When trying to organize my code, I'll usually group things together and enclose it in curly brackets so I can fold it and get an overview of the entire script. As I would just copy these lines and change the text (TEXT), they would go from pretty centered to looking messy pretty fast. Today I could no longer stand how ugly my code was getting, so I made a function that centers these comments. Now I just have to highlight a string (or have one in the clipboard) and hit the hotkey and the fold comment gets created according to my needs.
If I want the line comment above, I just type TEXT on a new line, highlight it and hit the hotkey (AltGR+C) and then it is centered. Since my preferred width of these lines is 106 characters (even number) odd length strings can't be centered. I decided to put odd strings one character to the left of the center.

Here is the script. Hope that someone finds some use for it.

Code: Select all

#Warn  ; Enable warnings to assist with detecting common errors.
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance,Force ;Only launch 1 instance of this script


{ ;~ ------------------------------------------ FUNCTIONS ------------------------------------------------

; Takes highlighted text and "centers" it (for naming code blocks). If nothing is highlighted, it centers
; whatever is in the Clipboard.

center_func(x){
    temp := ClipboardAll                    ; Saves clipboard, in case highlighted text is not copied
    Sleep 100
    Send ^c
    desired_width := 106                    ; How many characters the entire line should be
    line_center := desired_width/2
    Sleep 100
    Clipboard_length := StrLen(Clipboard)
    L_side := line_center-Ceil(StrLen(Clipboard)/2)-5   ; Calculates how many "-" to put on left side
    R_side := line_center-Floor(StrLen(Clipboard)/2)-1  ; Calculates how many "-" to put on right side
    Send, {{} {;}{~} {- %L_side%} %Clipboard% {-  %R_side%} ;} 
    Clipboard := temp                       ; Restores clipboard
    temp := ""                              ; Free memory (Don't think this is necessary in Ahk)
    return
    }



highlight_length(){ ; MsgBox with length of highlighted text or length of Clipboard
    temp := ClipboardAll
    Send, ^c
    Sleep 80
    high_length := StrLen(Clipboard)
    MsgBox, Length of highlighted string = %high_length%.
    Clipboard := temp
    temp := ""
    return
    }
}

{ ;~------------------------------------------- HOTKEYS -------------------------------------------------
<^>!c::center_func(Clipboard)   ; AltGr + C - Calls function center_func() with inputvariable = Clipboard
<^>!l::highlight_length()       ; AltGr + L - Returns a MsgBox with length of highlighted string.
<^>!x::Clipboard:=""            ; AltGr + X - Empties Clipboard
}
I'll throw in some examples, so you can see how the centering behaves.

Code: Select all

{ ;~---------------------------------------------- HELP --------------------------------------------------
{ ;~------------------------------------------- COMMENT 1 ------------------------------------------------
{ ;~----------------------------------------- COMMENT 10000 ----------------------------------------------
{ ;~---------------------------------------- SOME DESCRIPTION --------------------------------------------

DaveT1
Posts: 224
Joined: 07 Oct 2014, 11:23

Re: A script for centering "fold comments"

Post by DaveT1 » 22 Jan 2022, 06:22

Kotte wrote:
22 Jan 2022, 02:54
When trying to organize my code, I'll usually group things together and enclose it in curly brackets so I can fold it and get an overview of the entire script.
I do this all the time - nice to find someone else doing the same :dance:

Kotte
Posts: 46
Joined: 03 May 2021, 08:33

Re: A script for centering "fold comments"

Post by Kotte » 22 Jan 2022, 13:11

@DaveT1 Nice! :thumbup: Got any other good habits that you want to share? I'm a beginner when it comes to coding and even more so when it comes to organizing code. I stumbled upon functions yesterday, so now I'm leaning towards rewriting all of my long and messy hotkeys as functions instead. Seems more reasonable to have a bunch of semi short functions instead of one 200+ line hockey :headwall:

gya
Posts: 25
Joined: 04 Nov 2021, 01:22

Re: A script for centering "fold comments"

Post by gya » 23 Jan 2022, 00:51

Hello @Kotte

Very nice idea and it works well.
I take this opportunity to remind you the existence of this script :
Simple Commenting Tool - AutoHotkey Community @ viewtopic.php?f=6&t=62026

Sincerely.

DaveT1
Posts: 224
Joined: 07 Oct 2014, 11:23

Re: A script for centering "fold comments"

Post by DaveT1 » 23 Jan 2022, 05:19

Kotte wrote:
22 Jan 2022, 13:11
@DaveT1 Nice! :thumbup: Got any other good habits that you want to share? I'm a beginner when it comes to coding and even more so when it comes to organizing code. I stumbled upon functions yesterday, so now I'm leaning towards rewriting all of my long and messy hotkeys as functions instead. Seems more reasonable to have a bunch of semi short functions instead of one 200+ line hockey :headwall:
I'm a very visual learner. I need code laid out in a way that I spend less time seeing how code is related and more time working out where my bugs are ;) . This is coding style. It's taken me ages to refine my own style to where I'm content with it. And there are as many views on style as there are coders! Lots and lots of threads on this forum about it. But the key is to pick a style (/develop your own) that works well for you (probably it'll never be 100% perfect, but 80% or 90% is pretty good), and then stick to it for consistency.

Using functions is very good, but you only need them (a) for code that's being repeated, and/or (b) to put in a library (and used in a script via #Include) when you want to share the same code across many different scripts.

Good naming of variables is also a powerful technique, and I'm still learning this. Code should, as far as possible, be self documenting and good variable (and function) naming goes a long way to this.

Mainly use comments to explain why something is happening rather than re-express in words what the code is doing. Nonetheless, I still have comments explaining what code is doing so that I can protect me in 3 months comming back and better understanding what I wrote (or borrowed from the forums)!

I also find a good IDE is invaluable in helping to understand how code is laid out. I started with Scite4AHK, then AHKStudio and am now using VSCode. They've all got pros and cons from my perspective and have their own learning curves. But am currently pretty happy with VSCode.

HTHs.

Kotte
Posts: 46
Joined: 03 May 2021, 08:33

Re: A script for centering "fold comments"

Post by Kotte » 24 Jan 2022, 13:41

Yeah, a neat layout makes it so much easier to find the bugs. I have developed the base of a style at least :) There are so many things I don't know about yet so it will probably change a lot, but it's a start.

Libraries and GUI's are two things I gotta start fiddling with soon. I made a small GUI menu the other day and realized that I could free over 50% of my hotkeys by making a simple conditional GUI instead. I made a small GUI menu the other day and realized that I could free over 50% of my hotkeys by making a simple conditional GUI instead. I also find functions useful for tidying up my scripts. I think I'd prefer to have all my hotkeys sorted alphabetically in one place. My hotkeys vary from single line to 200+ lines, so by just changing the long ones to functions I could have something like

Code: Select all

<^>!a::simple_hotkey1
<^>!b::200_line_function()
<^>!c::simple_hotkey2
instead of folds. I think that would make everything so much tidier.

My current use of comments is mainly for future reference. Most of my time is spent figuring out how to do something, rather than implementing it, so I try to document everything I learn. I know I will probably have forgotten how to do something the next time I need it, so those comments are big time savers still. I use SciTE4AutoHotkey, Pycharm, Matlab and Texstudio mostly. I'm pretty organized in Pycharm and Matlab and make a small help text at the top of each module/script. In Texstudio I rarely need any documentation, but in SciTE, I'm quite lost :? I really need to switch to something else but I don't have the time to start over at the moment, so I'm stuck with it a while longer. I'll have to check out AHKStudio and VSCode. I have tried Visual Studio, but it didn't agree with me. I guess VSCode is not that similar though. Thanks for the tips! :thumbup:

Post Reply

Return to “Scripts and Functions (v1)”