Cleanly formatted INI File without rewriting the wheel...

Post your working scripts, libraries and tools
Posts: 37
Joined: 10 Jan 2016, 18:25
Facebook: Acecool
Google: Acecool
GitHub: Acecool

Cleanly formatted INI File without rewriting the wheel...

04 Jan 2017, 11:54

I've seen a few people asking how to add spaces between =s and indent the keys to make the ini / config file easier to read... I was having some issues because if you add a tab to the key then Read will think it doesn't exist because, for some reason even with AutoTrim set to off, it automatically trims the data and still looks for the tab . key... Reading the key, even with the tab there without using tab in the call reads everything properly.. Setting without the tab on something already tabbed works perfectly.. If you try to Write / Set a key with the added tab.. the opposite happens as what happens with read; it still works perfectly but you don't need to add the tab after the initial setup...

So.. In short.. The trick is to initialize with formatting and then read / write as normal... I'm posting the script here separately ( it is part of my ahk framework: ... k/overview ) but you'll still need the string.Column code which is used to set up nice looking text-based columns separated by tabs / spaces and which always lines up - AcecoolAHK_Framework\classes\library_string.ahk for the function( s ) required. If you want to have spaces so all equals are lined up you need to set the value with formatting and add a second =s ( It needs to be = from what I've tried out... all other characters [ even if you try substr to remove the last char, etc.. it doesn't retain formatting or the keys double up, or whatever... with = it stays and works ] ) at the end of all the spaces so they aren't auto trimmed.. then after you call IniWrite with the formatted key, you call it right away again without formatting and the equals will be removed but the spacing will remain the same...

This is a very simple yet very effective trick which improves readability of the ini file 10 fold all without having to re-invent the wheel but I do add helper functions ( because you can't use expressions / function-calls / etc.. in the 1 line AHK commands ) for Set / SetDefault / Get / Delete / Exists

Here's what my old config.ini looked like:

Code: Select all

Author=Josh 'Acecool' Moser
NUMLOCK_ON=C:\WINDOWS\Media\Speech On.wav
NUMLOCK_OFF=C:\WINDOWS\Media\Speech Off.wav
CAPSLOCK_ON=C:\WINDOWS\Media\Speech On.wav
CAPSLOCK_OFF=C:\WINDOWS\Media\Speech Off.wav
Mic_1=Headset Microphone
And this is what it looks like now:

Code: Select all

	__this__                 = config.ini
	Author                   = Josh 'Acecool' Moser
	Website                  =
	version                  = 0.4.20
	NirCmd                   = _assets\bin\NirCmd_x64\nircmd.exe
	ToggleCapsLockViaShift   = 150
	NUMLOCK_ON               = C:\WINDOWS\Media\Speech On.wav
	NUMLOCK_OFF              = C:\WINDOWS\Media\Speech Off.wav
	SCROLLLOCK_ON            = C:\WINDOWS\Media\Speech On.wav
	SCROLLLOCK_OFF           = C:\WINDOWS\Media\Speech Off.wav
	CAPSLOCK_ON              = C:\WINDOWS\Media\Speech On.wav
	CAPSLOCK_OFF             = C:\WINDOWS\Media\Speech Off.wav
	OnSwitchSound            = *64
	OnSwitchTextToSpeech     = yes
	Selected                 = 0
	Devices                  = 2
	Out_0                    = Rosewill Headset
	Mic_0                    = Rosewill Headset Mic
	Out_1                    = Desktop Speakers
	Mic_1                    = Webcam Mic
	DropBox_UserID           = 12345678
	PrivateScreenshotsPath   = C:\Users\Acecool\Dropbox\Screenshots\
	PublicScreenshotsPath    = C:\Users\Acecool\Dropbox\Public\Screenshots\
For people that use scripts and aren't into the coding side of things ( even know it is a config file ), some people get confused with all of the jumble.. But with it organized like this, nice and clean... Everything is easily identifiable and easy to manage...

If you want to try my framework which lets you drag and drop your scripts into various folders and have them automatically run / included when the framework loads as part of the framework or standalone ( you can even drop your favorite executables in the addons_exe directory and whitelist them and have them launch when the framework launches; use _assets\_setup\setup.bat in admin mode to point AHK\Lib to AcecoolAHK_Framework\libraries\ as symbolic link and replace Documents\AutoHotkey.ahk with the launcher so AutoHotkey.exe will launch the framework - note: a backup is made of your Lib\ dir and AutoHotkey.ahk in Documents if you have one -- ... k/overview )

Here's the config script: ... config.ahk
which needs functions from: ... ry_ini.ahk ... y_file.ahk ... string.ahk

And possibly a few others...But the basic logic is in the library_config and library_ini files but you will need string.FormatColumn ( which basically adds spaces to line up text and truncates what goes over so all of the config.ini vars will line up properly )...
-Josh 'Acecool' Moser
I teach various programming languages, game programming, etc... I am also taking on programming jobs as my health allows.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 16 guests