• Welcome to Valhalla Legends Archive.
 

Console tab stops?

Started by brew, October 23, 2007, 06:50 AM

Previous topic - Next topic

brew


How would I fix the shwiconEM.exe and vmnetdhcp.exe lines' disalignment? I figure I can do it by setting my own tab stops, but how? The output looks so messy. Right now I'm using two \t's.
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

rabbit

It ain't dynamic.  Get over it.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

devcode

Quote from: rabbit on October 23, 2007, 07:03 AM
It ain't dynamic.  Get over it.
Use CSuperBrewProcessListerTabs, shits fyre

iago

The best way is probably to use printf()-style formatting parameters (you can do it in C++ too, but I don't know how)

printf("%32s %d\n");

That'll print the string in a 32-character column. I forget whether it aligns the string left or right, but you can probably play around with it and get it right.

The other option is to manually do it:
int i;
printf("%s", str);
for(i = 0; i < 32 - strlen(str); i++)
  printf(" ");
printf("\n");

There's probably a better way to do that, but whatever. :)
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


brew

Quote from: iago on October 23, 2007, 08:52 AM
The best way is probably to use printf()-style formatting parameters (you can do it in C++ too, but I don't know how)

printf("%32s %d\n");

That'll print the string in a 32-character column. I forget whether it aligns the string left or right, but you can probably play around with it and get it right.

The other option is to manually do it:
int i;
printf("%s", str);
for(i = 0; i < 32 - strlen(str); i++)
  printf(" ");
printf("\n");

There's probably a better way to do that, but whatever. :)
You were right-- it alligns to the right, i wonder how i would get it to the left? (actually i dont think i can :/) So yeah, padding it with spaces for every char it doesn't take up is the best idea. Thanks for helping.
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

Joe[x86]

C# was the last language I remember you to brown-nose. If not, port it yourself.

using System;
using System.Collections.Generic;
using System.Text;

namespace StringPadding
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("String padder");
            Console.WriteLine("A Joe[x86] Production!");
            Console.WriteLine();
            Console.WriteLine(padStringRight("Brew is an idiot.", 'x', 32));
            Console.WriteLine();
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }

        private static string padStringRight(string data, char padding, int length)
        {
            if (data.Length > length)
                throw new ArgumentOutOfRangeException("length", "length parameter is shorter than data's length.");

            char[] ret = new char[length];
            Array.Copy(data.ToCharArray(), ret, data.Length);
            for (int i = data.Length; i < length; i++)
            {
                ret[i] = padding;
            }
            return new String(ret);
        }

    }
}
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

brew

Quote from: Joex86] link=topic=17131.msg174150#msg174150 date=1193158913]
C# was the last language I remember you to brown-nose. If not, port it yourself.
What? I can't port that, I don't know C# at all. I never coded in C# or anything .NET for that matter, and I intend to never code in any .NET languages in my lifetime. And if you had read my last post you would have found that I've already got it figured out-- I just used a space padder like iago said to. I first learned about a space padder long ago, back when I was making columns for a listbox control in vb. I just never thought of using it for this (i figured sprintf would have something i didn't know about that does this, apparently not)

Also, nice coding practice:

return new String(ret);

Allocating heap memory for a string? And you don't even free it in main(). So what if the program is going to end and it wouldn't matter, it's just not a good idea to do that IMO.
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

K

#7
Quote from: brew on October 23, 2007, 12:27 PM
Allocating heap memory for a string? And you don't even free it in main(). So what if the program is going to end and it wouldn't matter, it's just not a good idea to do that IMO.

C# is a garbage collected language.  All classes are allocated using the new keyword, and are disposed of automatically when they are no longer referenced.

Quote from: iago on October 23, 2007, 08:52 AM
The best way is probably to use printf()-style formatting parameters (you can do it in C++ too, but I don't know how)

In C++ you use the stream operators found in the <iomanip> header.  In this case, probably something like:


using namespace std;
cout << setw(32) << left << "foo" << setw(32) << left << "bar" << endl;

devcode

Quote from: brew on October 23, 2007, 09:59 AM
Quote from: iago on October 23, 2007, 08:52 AM
The best way is probably to use printf()-style formatting parameters (you can do it in C++ too, but I don't know how)

printf("%32s %d\n");

That'll print the string in a 32-character column. I forget whether it aligns the string left or right, but you can probably play around with it and get it right.

The other option is to manually do it:
int i;
printf("%s", str);
for(i = 0; i < 32 - strlen(str); i++)
  printf(" ");
printf("\n");

There's probably a better way to do that, but whatever. :)
You were right-- it alligns to the right, i wonder how i would get it to the left? (actually i dont think i can :/) So yeah, padding it with spaces for every char it doesn't take up is the best idea. Thanks for helping.

np looking to help anytime!

brew

Quote from: devcode on October 23, 2007, 01:01 PM
np looking to help anytime!
I'm pretty sure I was talking to iago
@K: I don't want to use any C++. I , personally, hate classes or anything OO of that sort.
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

Warrior

Quote from: brew on October 23, 2007, 01:42 PM
Quote from: devcode on October 23, 2007, 01:01 PM
np looking to help anytime!
I'm pretty sure I was talking to iago
@K: I don't want to use any C++. I , personally, hate classes or anything OO of that sort.

what
Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

K

Quote from: brew on October 23, 2007, 09:59 AM
Quote from: iago on October 23, 2007, 08:52 AM
The best way is probably to use printf()-style formatting parameters (you can do it in C++ too, but I don't know how)

printf("%32s %d\n");

That'll print the string in a 32-character column. I forget whether it aligns the string left or right, but you can probably play around with it and get it right.

The other option is to manually do it:
int i;
printf("%s", str);
for(i = 0; i < 32 - strlen(str); i++)
  printf(" ");
printf("\n");

There's probably a better way to do that, but whatever. :)
You were right-- it alligns to the right, i wonder how i would get it to the left? (actually i dont think i can :/) So yeah, padding it with spaces for every char it doesn't take up is the best idea. Thanks for helping.

To align to the left I believe you specify a negative padding number:

printf("%-32s", s);

MyndFyre

Quote from: Joex86] link=topic=17131.msg174150#msg174150 date=1193158913]
C# was the last language I remember you to brown-nose. If not, port it yourself.
Joe, first of all that's not the right approach, and even if it was, C# already has String.PadLeft and String.PadRight methods that work on the instances of the strings they're changing.

The proper way to do it would be to use formatting strings.  Format strings allow an alignment property to be set:

using System;
using System.Diagnostics;
using System.IO;
using System.ComponentModel;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine();
        Console.WriteLine("            ===={ Process Viewer }====");
        Console.WriteLine();
        Console.WriteLine("  {0,24}{1,10}", "Image Name", "  ID ");
        Console.WriteLine();
        Process[] processes = Process.GetProcesses();
        int threads = 0;
        foreach (Process p in processes)
        {
            try
            {
                Console.WriteLine("  {0,24}{1,10}", Path.GetFileName(p.MainModule.FileName), p.Id);
            }
            catch (Win32Exception)
            {
                Console.WriteLine("  {0,23}*{1,10}", p.ProcessName, p.Id);
            }
            threads += p.Threads.Count;
        }

        Console.WriteLine();
        Console.WriteLine("  Total number of processes: {0}", processes.Length);
        Console.WriteLine("  Total number of threads: {0}", threads);

        Console.ReadLine();
    }
}


Note: for processes that do not expose a main module (I was receiving ACCESS_DENIED errors), I have marked them with a * at the end of the process name.

Result:


K: that (left alignment) is likely correct.  .NET's formatting was based on C, and that's how you would left-align.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

brew

Quote from: K on October 23, 2007, 02:37 PM
To align to the left I believe you specify a negative padding number:

printf("%-32s", s);
Oh wow, that's just what i was looking for. :D
Mynd, I've noticed that people call blank writelines in order to space their lines out. Is there no '\n' in C# or something? That'd be pretty sad if there weren't
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

iago

"\n" isn't a standard. Windows, for example, uses \r\n. Linux uses \n. iirc, Mac uses \r. So using println() guarantees, in a platform-independent way, that you'll get the proper newline character.

That may seem silly in C#, which is only typically run on Windows, but it's still a good habit. Plus, people might make silly mistakes like using "\n" instead of "\r\n" :P
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*