Raised This Month: $12 Target: $400
 3% 

Console Formatting: Creating Tables


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Zynda
Member
Join Date: Jul 2011
Old 07-11-2016 , 17:27   Console Formatting: Creating Tables
Reply With Quote #1

Introduction

I've mapped the width of the most basic characters ( Basically ASCII 32 to 126 )
and developed a function to easily calculate the amount of whitespaces
required to pad a string in order to create formatted tables, like this:

Screenshot:


Please note that because the total width of a string does not always become the equivalent of a rational number of whitespaces, it is not always possible to get the exact equivalent width.
This means that the end result can deviate between the equivalent width of 0.25 to 0.75 whitespaces.

However, if the delta ( padding - calculated string width ) is greater
than 9.75 perfect padding are always guaranteed because ot the utilization of
an horizontal tab character that is the equivalent of 3.25 whitespaces in width.

The Font Problem

Spoiler

Usage Example

PHP Code:
public TableTestidlevelcid )
{
    if ( !
cmd_accessidlevelcid) )
    {
        return 
PLUGIN_HANDLED
    
}
    
    new 
firstRowString128 ]
    new 
secondRowString32 ]
    new const 
padding 60
    
    
// ### Static first row data ###
    
    
console_printid"^nStatic:^n" )
    
    
// First line
    
    
firstRowString "Name"
    
secondRowString "Zynda"
    
    
FormatConsolePaddingfirstRowStringpaddingfirstRowStringcharsmaxfirstRowString ) )
    
    
console_printid"%s%s"firstRowStringsecondRowString )
    
    
// Second line
    
    
firstRowString "Health Value"
    
secondRowString "100.0"
    
    
FormatConsolePaddingfirstRowStringpaddingfirstRowStringcharsmaxfirstRowString ) )
    
    
console_printid"%s%s"firstRowStringsecondRowString )
    
    
// Third line
    
    
firstRowString "Really Long Example String"
    
secondRowString "Some Value"
    
    
FormatConsolePaddingfirstRowStringpaddingfirstRowStringcharsmaxfirstRowString ) )
    
    
console_printid"%s%s"firstRowStringsecondRowString )
    
    
// ### Dynamic first row data ###
    
    
console_printid"^nDynamic:^n" )
    
    
// First line
    
    
get_mapnamefirstRowStringcharsmaxfirstRowString ) )
    
secondRowString "Current Map"
    
    
FormatConsolePaddingfirstRowStringpaddingfirstRowStringcharsmaxfirstRowString ) )
    
    
console_printid"%s%s"firstRowStringsecondRowString )
    
    
// Second line
    
    
get_user_nameidfirstRowStringcharsmaxfirstRowString ) )
    
secondRowString "Your Name"
    
    
FormatConsolePaddingfirstRowStringpaddingfirstRowStringcharsmaxfirstRowString ) )
    
    
console_printid"%s%s"firstRowStringsecondRowString )
    
    return 
PLUGIN_HANDLED

Screenshot:
Spoiler


Function
PHP Code:
FormatConsolePadding( const inputString[], paddingoutputString[], outputStringLength )
{
    new 
whitespaceString512 ]
    new 
currentChar 0
    
    
// ### Calucate length equivalent
    
    
new Float:inputLength 0.0
    
    
for( new 0strleninputString ); i++ )
    {
        switch( 
inputString] )
        {
            
// ### LOWERCASE ###
            
            
case 97// a
            
{
                
inputLength += 1.75
            
}
            case 
98// b
            
{
                
inputLength += 1.75
            
}
            case 
99// c
            
{
                
inputLength += 1.5
            
}
            case 
100// d
            
{
                
inputLength += 1.75
            
}
            case 
101// e
            
{
                
inputLength += 1.75
            
}
            case 
102// f
            
{
                
inputLength += 1.0
            
}
            case 
103// g
            
{
                
inputLength += 1.75
            
}
            case 
104// h
            
{
                
inputLength += 1.75
            
}
            case 
105// i
            
{
                
inputLength += 0.75
            
}
            case 
106// j
            
{
                
inputLength += 1.0
            
}
            case 
107// k
            
{
                
inputLength += 1.5
            
}
            case 
108// l
            
{
                
inputLength += 0.75
            
}
            case 
109// m
            
{
                
inputLength += 2.75
            
}
            case 
110// n
            
{
                
inputLength += 1.75
            
}
            case 
111// o
            
{
                
inputLength += 1.75
            
}
            case 
112// p
            
{
                
inputLength += 1.75
            
}
            case 
113// q
            
{
                
inputLength += 1.75
            
}
            case 
114// r
            
{
                
inputLength += 1.25
            
}
            case 
115// s
            
{
                
inputLength += 1.5
            
}
            case 
116// t
            
{
                
inputLength += 1.0
            
}
            case 
117// u
            
{
                
inputLength += 1.75
            
}
            case 
118// v
            
{
                
inputLength += 1.5
            
}
            case 
119// w
            
{
                
inputLength += 2.5
            
}
            case 
120// x
            
{
                
inputLength += 1.5
            
}
            case 
121// y
            
{
                
inputLength += 1.5
            
}
            case 
122// z
            
{
                
inputLength += 1.5
            
}
            
            
// ### UPPERCASE ###
            
            
case 65// A
            
{
                
inputLength += 2.0
            
}
            case 
66// B
            
{
                
inputLength += 1.75
            
}
            case 
67// C
            
{
                
inputLength += 2.0
            
}
            case 
68// D
            
{
                
inputLength += 2.0
            
}
            case 
69// E
            
{
                
inputLength += 1.75
            
}
            case 
70// F
            
{
                
inputLength += 1.75
            
}
            case 
71// G
            
{
                
inputLength += 2.0
            
}
            case 
72// H
            
{
                
inputLength += 2.0
            
}
            case 
73// I
            
{
                
inputLength += 1.0
            
}
            case 
74// J
            
{
                
inputLength += 1.25
            
}
            case 
75// K
            
{
                
inputLength += 1.75
            
}
            case 
76// L
            
{
                
inputLength += 1.5
            
}
            case 
77// M
            
{
                
inputLength += 2.5
            
}
            case 
78// N
            
{
                
inputLength += 2.0
            
}
            case 
79// O
            
{
                
inputLength += 2.25
            
}
            case 
80// P
            
{
                
inputLength += 1.75
            
}
            case 
81// Q
            
{
                
inputLength += 2.25
            
}
            case 
82// R
            
{
                
inputLength += 2.0
            
}
            case 
83// S
            
{
                
inputLength += 2.0
            
}
            case 
84// T
            
{
                
inputLength += 2.0
            
}
            case 
85// U
            
{
                
inputLength += 2.0
            
}
            case 
86// V
            
{
                
inputLength += 2.0
            
}
            case 
87// W
            
{
                
inputLength += 3.0
            
}
            case 
88// X
            
{
                
inputLength += 2.0
            
}
            case 
89// Y
            
{
                
inputLength += 1.75
            
}
            case 
90// Z
            
{
                
inputLength += 1.75
            
}
            
            
// ### NUMBER ###
            
            
case 48// 0
            
{
                
inputLength += 1.75
            
}
            case 
49// 1
            
{
                
inputLength += 1.75
            
}
            case 
50// 2
            
{
                
inputLength += 1.75
            
}
            case 
51// 3
            
{
                
inputLength += 1.75
            
}
            case 
52// 4
            
{
                
inputLength += 1.75
            
}
            case 
53// 5
            
{
                
inputLength += 1.75
            
}
            case 
54// 6
            
{
                
inputLength += 1.75
            
}
            case 
55// 7
            
{
                
inputLength += 1.75
            
}
            case 
56// 8
            
{
                
inputLength += 1.75
            
}
            case 
57// 9
            
{
                
inputLength += 1.75
            
}
            
            
// ### SPECIAL ###
            
            
case 33// !
            
{
                
inputLength += 1.0
            
}
            case 
34// "
            
{
                
inputLength += 1.25
            
}
            case 
35// #
            
{
                
inputLength += 2.25
            
}
            case 
36// $
            
{
                
inputLength += 1.75
            
}
            case 
37// %
            
{
                
inputLength += 1.5
            
}
            case 
38// &
            
{
                
inputLength += 2.25
            
}
            case 
39// '
            
{
                
inputLength += 0.75
            
}
            case 
40// (
            
{
                
inputLength += 1.25
            
}
            case 
41// )
            
{
                
inputLength += 1.25
            
}
            case 
42// *
            
{
                
inputLength += 2.0
            
}
            case 
43// +
            
{
                
inputLength += 2.0
            
}
            case 
44// ,
            
{
                
inputLength += 1.0
            
}
            case 
45// -
            
{
                
inputLength += 1.25
            
}
            case 
46// .
            
{
                
inputLength += 1.0
            
}
            case 
47// /
            
{
                
inputLength += 1.25
            
}
            
            case 
58// :
            
{
                
inputLength += 1.25
            
}
            case 
59// ;
            
{
                
inputLength += 1.25
            
}
            case 
60// <
            
{
                
inputLength += 2.25
            
}
            case 
61// =
            
{
                
inputLength += 2.25
            
}
            case 
62// >
            
{
                
inputLength += 2.25
            
}
            case 
63// ?
            
{
                
inputLength += 1.5
            
}
            case 
64// @
            
{
                
inputLength += 3.0
            
}
            case 
91// [
            
{
                
inputLength += 1.25
            
}
            case 
92// ( BACKWARDS SLASH )
            
{
                
inputLength += 1.25
            
}
            case 
93// ]
            
{
                
inputLength += 1.25
            
}
            case 
94// ^
            
{
                
inputLength += 2.25
            
}
            case 
95// _
            
{
                
inputLength += 1.75
            
}
            case 
96// `
            
{
                
inputLength += 1.75
            
}

            case 
123// {
            
{
                
inputLength += 1.5
            
}
            case 
124// |
            
{
                
inputLength += 1.5
            
}
            case 
125// }
            
{
                
inputLength += 1.5
            
}
            case 
126// ~
            
{
                
inputLength += 2.25
            
}
            
            
// ### DEFAULT ###
            
            
default:
            {
                
inputLength += 1.0
            
}
        }
    }
    
    
// ### Calculate delta ###
    
    
new Float:delta floatpadding ) - inputLength
    
    
if( delta 0.0 // Insufficent space
    
{
        
copyoutputString outputStringLengthinputString )
        return
    }
    
    
// ### Get rid of any decimals where possible ###


    
while( floatrounddelta ) != delta )
    {
        if( ( 
delta -= 3.25 ) >= 0.0 )
        {
            if( 
currentChar 511 )
            {
                
whitespaceStringcurrentChar ] = 9
                currentChar
++
            }
        }
        else
        {
            break
        }
    }
    
    
// ### Add remaining whitespaces ###
    
    
while( ( delta -= 1.0 ) > 0.0 )
    {
        if( 
currentChar 511 )
        {
            
whitespaceStringcurrentChar ] = 32
            currentChar
++
        }
    }
    
    
formatoutputStringoutputStringLength"%s%s"inputStringwhitespaceString )

Attached Files
File Type: sma Get Plugin or Get Source (padding_test.sma - 397 views - 8.2 KB)
Zynda is offline
PRoSToTeM@
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 07-11-2016 , 22:23   Re: Console Formatting: Creating Tables
Reply With Quote #2

Good job!
__________________
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
meTaLiCroSS
Gaze Upon My Hat
Join Date: Feb 2009
Location: Viña del Mar, Chile
Old 07-12-2016 , 02:28   Re: Console Formatting: Creating Tables
Reply With Quote #3

It would be nice to reduce all that switch conditional with an array. It's trivial but reminds a good coding style. Remember that you must priorize CPU over Memory.
__________________
Quote:
Originally Posted by joropito View Post
You're right Metalicross
meTaLiCroSS is offline
Old 07-12-2016, 03:33
ddhoward
This message has been deleted by ddhoward. Reason: nevermind, apparently Source uses a monospaced font; this is still good for AMX Mod X tho
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 07-12-2016 , 03:57   Re: Console Formatting: Creating Tables
Reply With Quote #4

Aren't game fonts changeable? Ubuntu users by default have a diffrrent font than Windows users.
klippy is offline
Zynda
Member
Join Date: Jul 2011
Old 07-12-2016 , 05:18   Re: Console Formatting: Creating Tables
Reply With Quote #5

Quote:
Originally Posted by KliPPy View Post
Aren't game fonts changeable? Ubuntu users by default have a diffrrent font than Windows users.
Initially i did look into changing the font as a user but i remeber not finding much. As a mod developer it is possible for sure, Sven-Coop got an option to use an monospaced font for example.
I'll also admit that i put little thought into linux versions, odds are my method will fail completely for linux users.
Zynda is offline
Zynda
Member
Join Date: Jul 2011
Old 07-12-2016 , 05:43   Re: Console Formatting: Creating Tables
Reply With Quote #6

Quote:
Originally Posted by meTaLiCroSS View Post
It would be nice to reduce all that switch conditional with an array. It's trivial but reminds a good coding style. Remember that you must priorize CPU over Memory.
I'm not sure i follow your thinking, could you enlighten me with an example of how that would be done?
Zynda is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-12-2016 , 09:23   Re: Console Formatting: Creating Tables
Reply With Quote #7

Quote:
Originally Posted by KliPPy View Post
Aren't game fonts changeable? Ubuntu users by default have a diffrrent font than Windows users.
Even if they are user-changeable, they probably are different in Linux regardless of what the user wants because most fonts on Windows® are proprietary and cannot legally be used on Linux without a license. So, Linux typically uses open-source fonts.
__________________
fysiks is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 07-12-2016 , 10:14   Re: Console Formatting: Creating Tables
Reply With Quote #8

Quote:
Originally Posted by fysiks View Post
Even if they are user-changeable, they probably are different in Linux regardless of what the user wants because most fonts on Windows® are proprietary and cannot legally be used on Linux without a license. So, Linux typically uses open-source fonts.
My point was that this solution isn't universal, not just about Linux clients using different fonts. Windows users could most likely have different fonts too.

However, the idea is cool.

Last edited by klippy; 07-12-2016 at 10:14.
klippy is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 13:56.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode