Beispiel #1
0
        /// <summary>
        /// Optimizes an image. Runs asynchronous in the background and it's not fast
        /// so don't rely on the file results immediately.
        /// </summary>
        /// <param name="imageFilename"></param>
        /// <param name="imageQuality">Optional image quality. If not specified auto is used</param>
        public static void OptimizeImage(string imageFilename, int imageQuality = 0, Action <bool> onComplete = null)
        {
            try
            {
                string exec = Path.Combine(App.InitialStartDirectory, "pingo.exe");
                string args;

                if (imageQuality > 0)
                {
                    args = $"-auto={imageQuality} \"" + imageFilename + "\"";
                }
                else
                {
                    args = "auto \"" + imageFilename + "\"";
                }

                if (onComplete != null)
                {
                    Task.Run(() =>
                    {
                        int result = ShellUtils.ExecuteProcess(exec, args, 30000, ProcessWindowStyle.Hidden);
                        onComplete(result == 0);
                    }).GetAwaiter();
                }
                else
                {
                    ShellUtils.ExecuteProcess(exec, args, 0, ProcessWindowStyle.Hidden);
                }
            }
            catch
            {
            }
        }
Beispiel #2
0
        /// <summary>
        /// Opens the configured Diff tool for the provided file and
        /// allows comparison.
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public bool OpenDiffTool(string filePath)
        {
            if (!File.Exists(mmApp.Configuration.Git.GitDiffExecutable))
            {
                SetError("There is no diff tool configured. Set the `GitDiffExecutable` setting to your preferred Diff tool.");
                return(false);
            }

            var fileText = GetComittedFileTextContent(filePath);

            if (fileText == null)
            {
                SetError("Unable to compare files: " + ErrorMessage);
                return(false);
            }

            var tempFile = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "mm_diff_" + System.IO.Path.GetFileName(filePath));

            File.WriteAllText(tempFile, fileText);

            // Delete files older than 5 minutes
            FileUtils.DeleteTimedoutFiles(System.IO.Path.Combine(System.IO.Path.GetTempPath(), "mm_diff_" + "*.*"), 300);

            ShellUtils.ExecuteProcess(mmApp.Configuration.Git.GitDiffExecutable, $"\"{tempFile}\" \"{filePath}\"");

            return(true);
        }
        public void ExecuteProcessAndGetOutput()
        {
            int result = ShellUtils.ExecuteProcess("ipconfig.exe", null, 3000, out string output,
                                                   ProcessWindowStyle.Normal);

            Assert.IsTrue(result == 0, "Process should exit with 0");
            Assert.IsNotNull(output, "Output should not be empty");
        }
Beispiel #4
0
        public void ExecuteProcessAndGetOutput()
        {
            int result = ShellUtils.ExecuteProcess("ipconfig.exe", null, 3000,
                                                   out StringBuilder output);

            Assert.IsTrue(result == 0, "Process should exit with 0");
            Assert.IsNotNull(output.Length > 0, "Output should not be empty");

            Console.WriteLine("Complete output:");
            Console.WriteLine("----------------");
            Console.WriteLine(output.ToString());
        }
Beispiel #5
0
 /// <summary>
 /// Shows external browser that's been configured in the MM Configuration.
 /// Defaults to Chrome
 /// </summary>
 /// <param name="url"></param>
 public static void ShowExternalBrowser(string url)
 {
     if (string.IsNullOrEmpty(mmApp.Configuration.WebBrowserPreviewExecutable) ||
         !File.Exists(mmApp.Configuration.WebBrowserPreviewExecutable))
     {
         mmApp.Configuration.WebBrowserPreviewExecutable = null;
         ShellUtils.GoUrl(url);
     }
     else
     {
         ShellUtils.ExecuteProcess(mmApp.Configuration.WebBrowserPreviewExecutable, $"\"{url}\"");
     }
 }
Beispiel #6
0
        public void ExecuteProcessAndCaptureOutputAction()
        {
            var action = new Action <string>((s) => Console.WriteLine(s));

            Console.WriteLine("Output Captured from Action:");
            Console.WriteLine("----------------------------");

            int result = ShellUtils.ExecuteProcess("ipconfig.exe", null,
                                                   3000,
                                                   action);

            Assert.IsTrue(result == 0, "Process should exit with 0");
        }
Beispiel #7
0
        /// <summary>
        /// Shows external browser that's been configured in the MM Configuration.
        /// Defaults to Chrome
        /// </summary>
        /// <param name="url"></param>
        public static void ShowExternalBrowser(string url)
        {
            try
            {
                if (string.IsNullOrEmpty(mmApp.Configuration.WebBrowserPreviewExecutable) ||
                    !File.Exists(mmApp.Configuration.WebBrowserPreviewExecutable))
                {
                    mmApp.Configuration.WebBrowserPreviewExecutable = null;

                    //ShellUtils.GoUrl(url);

                    WindowsUtils.TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice", "ProgId", out dynamic value, Registry.CurrentUser);
                    var progId = value as string;
                    Console.WriteLine(progId);

                    WindowsUtils.TryGetRegistryKey(@"MSEdgeHTM\shell\open\command", "", out value, Registry.ClassesRoot);

                    var exe = value as string;
                    if (exe == null)
                    {
                        ShellUtils.GoUrl(url); // truy
                        return;
                    }
                    ShellUtils.ExecuteCommandLine(exe);

                    //exe = exe.Replace("%1", $"{url}");

                    //var tokens = exe.Split(new string[] { "\" " },StringSplitOptions.RemoveEmptyEntries);


                    //tokens[0] = tokens[0].Replace("\"","");
                    //Process.Start(tokens[0],tokens[1]);
                }
                else
                {
                    ShellUtils.ExecuteProcess(mmApp.Configuration.WebBrowserPreviewExecutable, $"\"{url}\"");
                }
            }
            catch (Exception ex)
            {
                mmApp.Log($"External Preview failed: {url}", ex, logLevel: LogLevels.Warning);
            }
        }
Beispiel #8
0
 /// <summary>
 /// Shows external browser that's been configured in the MM Configuration.
 /// Defaults to Chrome
 /// </summary>
 /// <param name="url"></param>
 public static void ShowExternalBrowser(string url)
 {
     try
     {
         if (string.IsNullOrEmpty(mmApp.Configuration.WebBrowserPreviewExecutable) ||
             !File.Exists(mmApp.Configuration.WebBrowserPreviewExecutable))
         {
             mmApp.Configuration.WebBrowserPreviewExecutable = null;
             ShellUtils.GoUrl(url);
         }
         else
         {
             ShellUtils.ExecuteProcess(mmApp.Configuration.WebBrowserPreviewExecutable, $"\"{url}\"");
         }
     }
     catch (Exception ex)
     {
         mmApp.Log($"External Preview failed: {url}", ex, logLevel: LogLevels.Warning);
     }
 }
Beispiel #9
0
 /// <summary>
 /// Shows external browser that's been configured in the MM Configuration.
 /// Defaults to Chrome
 /// </summary>
 /// <param name="url"></param>
 public static void ShowExternalBrowser(string url)
 {
     try
     {
         if (string.IsNullOrEmpty(mmApp.Configuration.WebBrowserPreviewExecutable) ||
             !File.Exists(mmApp.Configuration.WebBrowserPreviewExecutable))
         {
             OpenBrowser(url);
         }
         else
         {
             ShellUtils.ExecuteProcess(mmApp.Configuration.WebBrowserPreviewExecutable, $"\"{url}\"");
         }
     }
     catch (Exception ex)
     {
         try
         {
             ShellUtils.GoUrl(url);
         }catch {}
     }
 }
        public void ExecuteProcess()
        {
            int result = ShellUtils.ExecuteProcess("ipconfig.exe", null, 3000, ProcessWindowStyle.Normal);

            Assert.IsTrue(result == 0, "Process should exit with 0");
        }
        /// <summary>
        ///
        /// mm htmltomarkdown [inputfile] [outputFile] -open
        /// </summary>
        /// <param name="inputFile"></param>
        /// <param name="outputFile"></param>
        /// <param name="openOutputFile"></param>
        public void HtmlToMarkdown()
        {
            Processor.ConsoleHeader();
            string inputFile  = Arguments.InputFile;
            string outputFile = Arguments.OutputFile;

            if (string.IsNullOrEmpty(inputFile) || !File.Exists(inputFile))
            {
                var fd = new OpenFileDialog
                {
                    DefaultExt = ".html",
                    Filter     = "HTML files (*.html, *.htm)|*.html;*.htm|" +
                                 "All files (*.*)|*.*",
                    CheckFileExists  = true,
                    RestoreDirectory = true,
                    Title            = "Open HTML File",
                    InitialDirectory = Environment.CurrentDirectory
                };
                var res = fd.ShowDialog();
                if (res == null)
                {
                    return;
                }
                inputFile = fd.FileName;
            }

            if (string.IsNullOrEmpty(outputFile))
            {
                var fd = new SaveFileDialog
                {
                    DefaultExt = ".md",
                    Filter     = "Markdown files (*.md,*.markdown,*.mdcrypt)|*.md;*.markdown;*.mdcrypt|" +
                                 "All files (*.*)|*.*",
                    CheckFileExists  = false,
                    RestoreDirectory = true,
                    Title            = "Save as Markdown File",
                    InitialDirectory = Path.GetDirectoryName(inputFile),
                    FileName         = Path.ChangeExtension(Path.GetFileName(inputFile), "md")
                };
                var res = fd.ShowDialog();
                if (res == null)
                {
                    return;
                }
                outputFile = fd.FileName;
            }


            string md;

            try
            {
                var html = File.ReadAllText(inputFile);
                md = MarkdownUtilities.HtmlToMarkdown(html, true);
            }
            catch
            {
                ColorConsole.WriteError("Failed: Couldn't read input file.");
                Processor.ConsoleFooter();
                return;
            }


            if (!string.IsNullOrEmpty(outputFile))
            {
                try
                {
                    File.WriteAllText(outputFile, md);
                }
                catch
                {
                    ColorConsole.WriteError("Failed: Couldn't write output file.");
                    Processor.ConsoleFooter();
                    return;
                }

                if (Arguments.OpenOutputFile)
                {
                    ShellUtils.ExecuteProcess("markdownmonster.exe", $"'{outputFile}'");
                }


                ColorConsole.WriteSuccess($"Created Markdown file: {outputFile}");

                Processor.ConsoleFooter();
            }
        }