Exemple #1
0
        public static void RegisterMatchRegex(CmdlineParser parser)
        {
            var cmdRegex = parser.AddCommand("match-regex", "match regex groups", (parserRegex) =>
            {
                var _regex = parserRegex.AddMandatoryParameter("regex", "c# regex");
                var _fmt   = parserRegex.AddMandatoryParameter("fmt", "format string ( use \\N to print Nth group in place )");

                parserRegex.OnCmdlineMatch(() =>
                {
                    var regexStr = (string)_regex;
                    var fmt      = Regex.Unescape((string)_fmt);
                    using (var sr = new StreamReader(Console.OpenStandardInput()))
                    {
                        var s   = sr.ReadToEnd();
                        var rgx = new Regex(regexStr /*, RegexOptions.Singleline*/).Match(s);
                        if (rgx.Success)
                        {
                            for (int i = 1; i < rgx.Groups.Count; ++i)
                            {
                                fmt = fmt.Replace($"\\{i}", rgx.Groups[i].Value);
                            }
                            System.Console.WriteLine(fmt);
                            Environment.Exit(0);
                        }
                        Environment.Exit(1);
                    }
                });
            });
        }
Exemple #2
0
        public static void RegisterLogic2FreqGraph(CmdlineParser parser)
        {
            var cmdReplaceToken = parser.AddCommand("logic2-freq-graph", "display pulse freq graph", (logic2FreqGraph) =>
            {
                var _filename      = logic2FreqGraph.AddMandatoryParameter("filename", "Logic2 exported csv file");
                var _columnsFilter = logic2FreqGraph.AddMandatoryParameter("columnsFilter", "filter for csv columns (eg. \"Channel 0, Channel 2\")");

                logic2FreqGraph.OnCmdlineMatch(() =>
                {
                    var pathfilename = (string)_filename;
                    if (!File.Exists(pathfilename))
                    {
                        System.Console.WriteLine($"can't find file [{pathfilename}]");
                        return;
                    }

                    var columnsFilter = (string)_columnsFilter;

                    MainWindow.csv_pathfilename = pathfilename;
                    MainWindow.chansFilter      = columnsFilter;

                    GuiToolkit.CreateGui <MainWindow>();
                });
            });
        }
Exemple #3
0
        public static void RegisterReplaceToken(CmdlineParser parser)
        {
            var cmdReplaceToken = parser.AddCommand("replace-token", "replace token from given standard input (not optimized for huge files)", (parserReplaceToken) =>
            {
                var flagCSharpRegex = parserReplaceToken.AddShort("csregex", "token will treated as csharp regex");
                var _token          = parserReplaceToken.AddMandatoryParameter("token", "token to search for");
                var _replacement    = parserReplaceToken.AddMandatoryParameter("replacement", "text to replace where token was found");

                parserReplaceToken.OnCmdlineMatch(() =>
                {
                    var token       = (string)_token;
                    var replacement = Regex.Unescape((string)_replacement);
                    using (var sr = new StreamReader(Console.OpenStandardInput()))
                    {
                        var s = sr.ReadToEnd();
                        if (flagCSharpRegex)
                        {
                            var rgx = new Regex(token, RegexOptions.Singleline);
                            System.Console.Write(rgx.Replace(s, replacement));
                        }
                        else
                        {
                            System.Console.Write(s.Replace(token, replacement));
                        }
                    }
                });
            });
        }
Exemple #4
0
        public static void RegisterAutoCPULimiter(CmdlineParser parser)
        {
            var DEFAULT_mincpu_find = 50d;
            var DEFAULT_maxcpu_set  = 20d;

            var cmdRegex = parser.AddCommand("cpu-autolimiter", "autolimit cpu usage for processes that uses more than mincpu-find", (parserCpuLimiter) =>
            {
                var _mincpu_find = parserCpuLimiter.AddShort("mincpu-find",
                                                             Invariant($"minimum cpu load to match process [default={DEFAULT_mincpu_find}]"), "val");

                var _maxcpu_set = parserCpuLimiter.AddShort("maxcpu-set",
                                                            Invariant($"maximum cpu load to set on matched processes [default={DEFAULT_maxcpu_set}]"), "val");

                parserCpuLimiter.OnCmdlineMatch(() =>
                {
                    var mincpu_find = _mincpu_find.ArgValues.Count == 0 ? DEFAULT_mincpu_find : ((string)_mincpu_find).InvDoubleParse();
                    var maxcpu_set  = _maxcpu_set.ArgValues.Count == 0 ? DEFAULT_maxcpu_set : ((string)_maxcpu_set).InvDoubleParse();

                    if (UtilToolkit.SearchInPath("cpustat") == null)
                    {
                        System.Console.WriteLine($"requirements: apt install cpustat");
                        return;
                    }

                    if (UtilToolkit.SearchInPath("cpulimit") == null)
                    {
                        System.Console.WriteLine($"requirements: apt install cpulimit");
                        return;
                    }

                    var task = Exec("cpustat", new[] { "1", "1" }, CancellationToken.None, false);
                    task.Wait();
                    var pid_cpu_lines = task.Result.Output.Lines().Skip(1)
                                        .Select(line =>
                    {
                        var ss      = line.Split(" ").Select(w => w.Trim()).Where(r => r.Length > 0).ToArray();
                        var pid     = ss[3];
                        var cpu     = ss[0].InvDoubleParse();
                        var cmdline = ss[7];

                        return(new
                        {
                            pid = pid,
                            cpu = cpu,
                            cmdline = cmdline
                        });
                    })
                                        .Where(r => r.cpu >= mincpu_find)
                                        .ToList();

                    foreach (var x in pid_cpu_lines)
                    {
                        Console.WriteLine($"process pid={x.pid} with cpu={x.cpu}% will limited to cpu={maxcpu_set}% [{x.cmdline}]");

                        Exec("cpulimit", new[] { "--background", "--pid", $"{x.pid}", "--limit", $"{maxcpu_set}" }, CancellationToken.None);
                    }
                });
            });
        }
Exemple #5
0
        public static void RegisterGraphArea(CmdlineParser parser)
        {
            var cmdReplaceToken = parser.AddCommand("graph-area", "compute area under graph XY", (parserGraphArea) =>
            {
                var _decimalDelimiter = parserGraphArea.AddShort("d", "decimal separator (default=.)", "VAL");
                var _fieldDelimiter   = parserGraphArea.AddShort("f", "field separator (default=,)", "VAL");
                var _genDxf           = parserGraphArea.AddShort("x", "generate dxf lwpolyline output");
                var _filename         = parserGraphArea.AddMandatoryParameter("filename", "simple XY column file");

                parserGraphArea.OnCmdlineMatch(() =>
                {
                    var genDxf = (bool)_genDxf;

                    var pathfilename = (string)_filename;
                    if (!File.Exists(pathfilename))
                    {
                        System.Console.WriteLine($"can't find file [{pathfilename}]");
                        return;
                    }

                    string dxfPathfilename = null;
                    netDxf.DxfDocument dxf = null;
                    if (genDxf)
                    {
                        dxfPathfilename = Path.Combine(
                            Path.GetDirectoryName(pathfilename), Path.GetFileNameWithoutExtension(pathfilename) + ".dxf");
                        dxf = new netDxf.DxfDocument();
                    }

                    var decDelim   = (string)_decimalDelimiter;
                    var fieldDelim = (string)_fieldDelimiter;

                    if (decDelim == null)
                    {
                        decDelim = ".";
                    }
                    if (fieldDelim == null)
                    {
                        fieldDelim = ",";
                    }

                    var ni = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
                    ni.NumberDecimalSeparator = decDelim;

                    var area = 0d;

                    var pts = new List <XYPt>();

                    using (var sr = new StreamReader(pathfilename))
                    {
                        while (!sr.EndOfStream)
                        {
                            var line = sr.ReadLine().Trim();
                            if (line.Length != 0)
                            {
                                var ss = line.Split(fieldDelim);
                                var x  = double.Parse(ss[0], ni);
                                var y  = double.Parse(ss[1], ni);
                                pts.Add(new XYPt()
                                {
                                    x = x, y = y
                                });
                            }
                        }
                    }

                    pts = pts.OrderBy(w => w.x).ToList();

                    var MINY = pts.Min(w => w.y);

                    var prevx = 0d;
                    var prevy = 0d;
                    int cnt   = 0;

                    netDxf.Entities.LwPolyline lw = null;
                    if (dxf != null)
                    {
                        lw = new netDxf.Entities.LwPolyline();
                    }

                    foreach (var pt in pts)
                    {
                        if (dxf != null)
                        {
                            lw.Vertexes.Add(new netDxf.Entities.LwPolylineVertex(pt.x, pt.y));
                        }
                        if (cnt > 0)
                        {
                            var maxy = Max(prevy, pt.y);
                            var miny = Min(prevy, pt.y);
                            var b    = (pt.x - prevx);
                            area    += b * (miny - MINY) + b * (maxy - miny) / 2;
                        }

                        prevx = pt.x;
                        prevy = pt.y;

                        ++cnt;
                    }

                    System.Console.WriteLine(Invariant($"area: {area}"));

                    if (dxf != null)
                    {
                        dxf.AddEntity(lw);
                        dxf.Save(dxfPathfilename, true);
                        System.Console.WriteLine($"[{dxfPathfilename}] written.");
                    }
                });
            });
        }