예제 #1
0
        public static string formatOut(lsFileInfo cf, string format = "%m %A %O %G %d %t %s %f")
        {
            string output = format;

            output = output.Replace("%m", Color(cs.Color.mode, cf.mode)); //could've chained, but this looks better IMO and allows me to format and seperate

            output = output.Replace("%An", Color(cs.Color.amount, (cf.amount == -1 ? "?" : cf.amount.ToString())));
            output = output.Replace("%Al", Color(cs.Color.amount, (cf.amount == -1 ? "?" : cf.amount.ToString()).PadLeft(Globals.longestAmount)));
            output = output.Replace("%A", Color(cs.Color.amount, (cf.amount == -1 ? "?" : cf.amount.ToString()).PadRight(Globals.longestAmount)));

            output = output.Replace("%On", Color(cs.Color.owner, cf.owner));
            output = output.Replace("%Ol", Color(cs.Color.owner, cf.owner.PadLeft(Globals.longestOwn)));
            output = output.Replace("%O", Color(cs.Color.owner, cf.owner.PadRight(Globals.longestOwn)));

            output = output.Replace("%Gn", Color(cs.Color.group, cf.group));
            output = output.Replace("%Gl", Color(cs.Color.group, cf.group.PadLeft(Globals.longestGrp)));
            output = output.Replace("%G", Color(cs.Color.group, cf.group.PadRight(Globals.longestGrp)));

            output = output.Replace("%d", Color(cs.Color.date, cf.udate));
            output = output.Replace("%t", Color(cs.Color.time, cf.utime));

            output = output.Replace("%sn", Color(cs.Color.size, (cf.size == -1 ? "---" : cf.size.ToString())));
            output = output.Replace("%sl", Color(cs.Color.size, (cf.size == -1 ? "---" : cf.size.ToString()).PadLeft(Globals.longestSize)));
            output = output.Replace("%s", Color(cs.Color.size, (cf.size == -1 ? "---" : cf.size.ToString()).PadRight(Globals.longestSize)));

            output = output.Replace("%fn", Color(cs.Color.file, cf.name));
            output = output.Replace("%fl", Color(cs.Color.file, cf.name.PadLeft(Globals.longestName)));
            output = output.Replace("%f", Color(cs.Color.file, cf.name.PadRight(Globals.longestName)));

            output = output.Replace("%p", Color(cs.Color.path, cf.path));

            string perms = "";

            foreach (Rule r in cf.permissions)
            {
                perms += r.User + " " + r.PermString;
            }
            output = output.Replace("%P", Color(cs.Color.path, perms));
            return(output);
        }
예제 #2
0
        static void Main(string[] args)
        {
            var handle = GetStdHandle(-11);
            int mode;

            GetConsoleMode(handle, out mode);
            SetConsoleMode(handle, mode | 0x4);


            //using home dir for debugging
            string wd  = "C:\\Users\\Username";
            string cwd = Directory.GetCurrentDirectory();

            string[] files = Directory.GetFileSystemEntries(wd);

            Options           opt   = Options.parse(Globals.testArgs);
            List <lsFileInfo> infos = new List <lsFileInfo>();

            foreach (string file in files)
            {
                FileInfo   cFI = new FileInfo(file);
                lsFileInfo f   = new lsFileInfo();

                //no need to get info on a hidden file if you dont wanna have all
                if (cFI.Attributes.HasFlag(FileAttributes.Hidden) && !opt.all)
                {
                    continue;
                }


                f.mode = Util.GetMode(cFI.Attributes, opt.modeformat);
                if (!cFI.Attributes.HasFlag(FileAttributes.Directory))
                {
                    f.size = cFI.Length;
                }

                f.udate = cFI.LastWriteTime.ToString(opt.dateFormat);
                f.utime = cFI.LastWriteTime.ToString(opt.timeFormat);
                f.path  = cFI.DirectoryName;
                f.name  = cFI.Name;

                f.isDir  = cFI.Attributes.HasFlag(FileAttributes.Directory);
                f.isLink = cFI.Attributes.HasFlag(FileAttributes.ReparsePoint);
                f.hidden = cFI.Attributes.HasFlag(FileAttributes.Hidden);

                f.owner = cFI.GetAccessControl().GetOwner(typeof(NTAccount)).Value; //does #define exist in C#?
                if (f.owner.StartsWith(Environment.MachineName) && !opt.showMachine)
                {
                    f.owner = f.owner.Remove(0, Environment.MachineName.Length + 1);
                }

                f.group = cFI.GetAccessControl().GetGroup(typeof(NTAccount)).Value;
                if (f.group.StartsWith(Environment.MachineName) && !opt.showMachine)
                {
                    f.group = f.group.Remove(0, Environment.MachineName.Length + 1);
                }

                f.permissions = Util.GetPermissions(cFI.GetAccessControl().GetAccessRules(true, true, typeof(NTAccount)), opt.permformat, opt.permrwformat);

                infos.Add(f);

                if (f.isDir)
                { //could be better
                    DirectoryInfo cDI = new DirectoryInfo(file);
                    try
                    {
                        if (cDI.GetFileSystemInfos().Length.ToString().Length > Globals.longestAmount)
                        {
                            Globals.longestAmount = cDI.GetFileSystemInfos().Length.ToString().Length;
                        }
                        f.amount = cDI.GetFileSystemInfos().Length;
                    }
                    catch (UnauthorizedAccessException)
                    {
                        f.amount = -1;
                    };
                }
                else
                {
                    if (cFI.Length.ToString().Length > Globals.longestSize)
                    {
                        Globals.longestSize = cFI.Length.ToString().Length;
                    }
                }
                if (f.owner.Length > Globals.longestOwn)
                {
                    Globals.longestOwn = f.owner.Length;
                }
                if (f.group.Length > Globals.longestGrp)
                {
                    Globals.longestGrp = f.group.Length;
                }
                if (f.name.Length > Globals.longestName)
                {
                    Globals.longestName = f.name.Length;
                }
            }

            if (opt.reverse)
            {
                infos.Reverse();
            }

            foreach (lsFileInfo cf in infos)
            {                     //could be done in the upper loop maybe?
                string line = ""; //people told me to name shit better, like "fileMeta" instead of line, even tho this is only here for outputting the line

                if (opt.filter.Count > 0)
                {
                    if (opt.filter.IndexOf(cf.name) == -1)
                    {
                        continue;
                    }
                }

                if (cf.hidden && !opt.all)
                {
                    continue;
                }

                if (opt.list)
                {
                    line = Util.formatOut(cf, opt.listformat);
                }
                else
                {
                    foreach (string listmode in opt.order)
                    { //could use dictionary
                        if (listmode == "mode")
                        {
                            line += Util.Color(Color.mode, cf.mode + " ");
                        }
                        if (listmode == "amount")
                        {
                            line += Util.Color(Color.amount, (cf.amount == -1 ? "?" : cf.amount.ToString()).PadRight(Globals.longestAmount) + " ");
                        }
                        if (listmode == "owner")
                        {
                            line += Util.Color(Color.owner, cf.owner.PadRight(Globals.longestOwn) + " ");
                        }
                        if (listmode == "group")
                        {
                            line += Util.Color(Color.group, cf.group.PadRight(Globals.longestGrp) + " ");
                        }
                        if (listmode == "date")
                        {
                            line += Util.Color(Color.date, cf.udate + " ");
                        }
                        if (listmode == "time")
                        {
                            line += Util.Color(Color.time, cf.utime + " ");
                        }
                        if (listmode == "size")
                        {
                            line += Util.Color(Color.size, (cf.size == -1 ? "---" : cf.size.ToString()).PadRight(Globals.longestSize) + " ");
                        }
                        if (listmode == "perm")
                        {
                            foreach (Rule r in cf.permissions)
                            {
                                line += Util.Color(Color.perm, r.User + " " + r.PermString);
                            }
                        }
                    }
                    if (opt.fullpath)
                    {
                        line += Util.Color(Color.file, cf.path + "\\");
                    }
                    line += Util.Color(Color.file, cf.name);

                    if (opt.indicator)
                    {
                        Util.GetIndicator(cf.mode);
                    }
                }

                Console.WriteLine(line);
            }
        }