Ejemplo n.º 1
0
        public void TestDeleteFiles()
        {
            string rootPath = Path.Combine(Path.GetTempPath(), "folder");

            if (Directory.Exists(rootPath))
            {
                Directory.Delete(rootPath, true);
            }

            Directory.CreateDirectory(rootPath);

            string path1 = Path.Combine(rootPath, "file1.txt");

            CFileUtils.Write(path1, "text");

            string path2 = Path.Combine(rootPath, "file2.txt");

            CFileUtils.Write(path2, "text");

            Assert.IsTrue(CFileUtils.FileExists(path1));
            Assert.IsTrue(CFileUtils.FileExists(path2));

            Assert.IsTrue(CFileUtils.Delete(path1));
            Assert.IsFalse(CFileUtils.FileExists(path1));
            Assert.IsFalse(CFileUtils.Delete(path1));

            Assert.IsTrue(CFileUtils.Delete(rootPath));
            Assert.IsFalse(CFileUtils.FileExists(rootPath));
            Assert.IsFalse(CFileUtils.Delete(rootPath));

            Assert.IsFalse(CFileUtils.FileExists(path2));
            Assert.IsFalse(CFileUtils.Delete(path2));
        }
Ejemplo n.º 2
0
        public void TestFileEmptyExt()
        {
            string path = "/usr/tmp/file";

            Assert.AreEqual("", CFileUtils.GetExtension(path));
            Assert.AreEqual("file", CFileUtils.GetFileNameWithoutExtension(path));
        }
Ejemplo n.º 3
0
        bool Execute(string filename)
        {
            try
            {
                if (edit)
                {
                    string configPath = CConfigHelper.GetConfigPath(filename);
                    if (!CFileUtils.FileExists(configPath))
                    {
                        PrintError("File does not exist: {0}", configPath);
                        return(false);
                    }

                    EditorUtility.OpenWithDefaultApp(configPath);
                    return(true);
                }

                IList <string> lines = CConfigHelper.ReadConfig(filename);
                foreach (string line in lines)
                {
                    PrintIndent(line);
                }

                return(true);
            }
            catch (Exception e)
            {
                PrintIndent("Can't read config: {0}", e.Message);
                return(false);
            }
        }
Ejemplo n.º 4
0
        public static string GetConfigPath(string filename)
        {
            string path = CFileUtils.ChangeExtension(filename, ".cfg");

            if (CFileUtils.IsPathRooted(path))
            {
                return(path);
            }

            return(Path.Combine(ConfigPath, path));
        }
Ejemplo n.º 5
0
        public static IList <string> ReadConfig(string filename)
        {
            if (filename == null)
            {
                throw new ArgumentNullException("filename");
            }

            string path = GetConfigPath(filename);

            return(CFileUtils.Read(path));
        }
Ejemplo n.º 6
0
        public void TestFileReplaceExt()
        {
            string path = "autoexec.cfg";

            Assert.AreEqual("autoexec.cfg", CFileUtils.ChangeExtension(path, ".cfg"));

            path = "usr/bin/autoexec.cfg";
            Assert.AreEqual("usr/bin/autoexec.cfg", CFileUtils.ChangeExtension(path, ".cfg"));

            path = "usr/bin/autoexec.CFG";
            Assert.AreEqual("usr/bin/autoexec.cfg", CFileUtils.ChangeExtension(path, ".cfg"));

            path = "usr/bin/autoexec";
            Assert.AreEqual("usr/bin/autoexec.cfg", CFileUtils.ChangeExtension(path, ".cfg"));
        }
Ejemplo n.º 7
0
        public static void WriteConfig(string filename, IList <string> lines)
        {
            if (filename == null)
            {
                throw new ArgumentNullException("filename");
            }

            if (lines == null)
            {
                throw new ArgumentNullException("lines");
            }

            string path = GetConfigPath(filename);

            CFileUtils.Write(path, lines);
        }
Ejemplo n.º 8
0
        public static IList <string> ListConfigs(string token = null)
        {
            List <string> result = new List <string>();

            string configsPath = ConfigPath;

            if (Directory.Exists(configsPath))
            {
                string[] files = Directory.GetFiles(configsPath, "*.cfg");
                foreach (string file in files)
                {
                    string filename = CFileUtils.GetFileName(file);
                    if (token == null || CStringUtils.StartsWithIgnoreCase(filename, token))
                    {
                        result.Add(filename);
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 9
0
 public static void DeleteConfigs()
 {
     CFileUtils.Delete(ConfigPath);
 }
Ejemplo n.º 10
0
        private void CreateUI()
        {
            this.AutoresizeMask = CViewAutoresizing.FlexibleWidth | CViewAutoresizing.FlexibleHeight;

            CToolBar toolbar = new CToolBar(this.Width);

            toolbar.Width = this.Width;

            toolbar.AddButton("Clear", delegate(CButton button)
            {
                Terminal.Clear();
            });

            // copy to clipboard
            toolbar.AddButton("Copy", delegate(CButton button)
            {
                string text = GetText();
                CEditor.CopyToClipboard(text);
            });

            // save to file
            toolbar.AddButton("Save", delegate(CButton button)
            {
                string title       = "Console log";
                string directory   = CFileUtils.DataPath;
                string defaultName = string.Format("console");
                string filename    = CEditor.SaveFilePanel(title, directory, defaultName, "log");
                if (!string.IsNullOrEmpty(filename))
                {
                    string text = GetText();
                    CFileUtils.Write(filename, text);
                }
            });

            m_infoLabel = toolbar.AddLabel("");

            toolbar.AddFlexibleSpace();

            AddSubview(toolbar);

            m_commandField       = new CTextField();
            m_commandField.Width = Width;
            AddSubview(m_commandField);
            m_commandField.AlignX(CView.AlignCenter);
            m_commandField.AlignBottom(0);
            m_commandField.AutoresizeMask = CViewAutoresizing.FlexibleTopMargin | CViewAutoresizing.FlexibleWidth;

            m_commandField.TextKeyDelegate = delegate(CTextField tf, KeyCode code, bool pressed)
            {
                if (pressed)
                {
                    switch (code)
                    {
                    case KeyCode.Return:
                    case KeyCode.KeypadEnter:
                    {
                        string commandLine = tf.Text.Trim();
                        if (commandLine.Length > 0)
                        {
                            HistoryPush(commandLine);
                            ExecCommand(commandLine);
                        }
                        tf.Text = "";
                        HistoryReset();

                        return(true);
                    }

                    case KeyCode.Tab:
                    {
                        string line = Terminal.DoAutoComplete(tf.Text, tf.CaretPos, IsDoubleTab());
                        if (line != null)
                        {
                            tf.Text = line;
                        }
                        m_lastTabTimestamp = Time.realtimeSinceStartup;
                        return(true);
                    }

                    case KeyCode.Escape:
                    {
                        tf.Text = "";
                        HistoryReset();
                        return(true);
                    }

                    case KeyCode.C:
                    {
                        if (tf.IsCtrlPressed)
                        {
                            tf.Text = "";
                            HistoryReset();
                            return(true);
                        }
                        break;
                    }

                    case KeyCode.K:
                    {
                        if (tf.IsCtrlPressed)
                        {
                            Terminal.Clear();
                            return(true);
                        }
                        break;
                    }

                    case KeyCode.DownArrow:
                    {
                        if (HistoryNext(tf))
                        {
                            return(true);
                        }

                        if (m_lastUserInput != null)
                        {
                            tf.Text = m_lastUserInput;
                            HistoryReset();
                            return(true);
                        }

                        return(true);
                    }

                    case KeyCode.UpArrow:
                    {
                        // keep user input to restore it
                        if (m_lastUserInput == null)
                        {
                            m_lastUserInput = tf.Text;
                        }

                        if (HistoryPrev(tf))
                        {
                            return(true);
                        }

                        return(true);
                    }
                    }
                }

                return(false);
            };

            m_commandField.TextChangedDelegate = delegate(CTextField field) {
                HistoryReset();
            };

            m_consoleView   = new CConsoleView(Terminal, m_commandField.Width, this.Height - (toolbar.Height + m_commandField.Height));
            m_consoleView.Y = toolbar.Bottom;
            m_consoleView.IsScrollLocked = true;
            m_consoleView.AutoresizeMask = CViewAutoresizing.FlexibleWidth | CViewAutoresizing.FlexibleHeight;
            AddSubview(m_consoleView);

            m_lastUserInput = null;
        }