Friday, 15 December 2017

Ubuntu - what do the colours in the console mean - Blue is Directory etc.

So I chanced upon a lovely Unix script which prints out a colour key in case a newbie is bamboozled by the coloured items. Copy the following in your terminal


eval $(echo "no:global default;fi:normal file;di:directory;ln:symbolic link;pi:named pipe;so:socket;do:door;bd:block device;cd:character device;or:orphan symlink;mi:missing file;su:set uid;sg:set gid;tw:sticky other writable;ow:other writable;st:sticky;ex:executable;"|sed -e 's/:/="/g; s/\;/"\n/g')           
{      
  IFS=:     
  for i in $LS_COLORS     
  do        
    echo -e "\e[${i#*=}m$( x=${i%=*}; [ "${!x}" ] && echo "${!x}" || echo "$x" )\e[m" 
  done       
} 

You will dark blue is a directory, normal file is white plus some others (run the script!)

Reproduced from Ask Ubuntu

Changing the bash prompt

So I really do not like so many colours in my bash prompt, here is an economical bash prompt that does not have colours and also does not have username and hostname.

PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}bash :\w\$'

To ensure this happens on each logon I changed my .bashrc file using nano. It lives in the home directory ~ and is hidden by default.

cd ~
nano .bashrc

In nano add the PS1= line to the end, then attempt to exit, it will prompt for a save before exiting to which you say yes.

If you forget your password

Thankfully there is askubuntu.com and this answer works.

Changing the Ubuntu's Window Caption

So if I am making a video I do not want my user name and hostname appearing in the bash prompt (see above) or in the Ubuntu window's title. I'm sure it is configurable but I knew how to use a Windows API call to set the window caption and this code below will suffice. It's for a C# console.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;

namespace UbuntuWindowTitleRenamer
{
    class Program
    {
        [DllImport("user32.dll")]
        static extern bool SetWindowText(IntPtr hWnd, string text);

        [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern int GetWindowTextLength(IntPtr hWnd);

        static void Main(string[] args)
        {
            Process[] processes = null;
            string bashPrompt = Environment.GetEnvironmentVariable("BASH_WINDOW_TITLE_PREFIX");
            while (!Console.KeyAvailable)
            {
                processes = Process.GetProcessesByName("ubuntu");
                LoopThruProcesses(processes, bashPrompt);
                processes = Process.GetProcessesByName("bash");
                LoopThruProcesses(processes, bashPrompt);
            }
        }

        static void LoopThruProcesses(Process[] processes, string bashPrompt)
        {
            foreach (var procLoop in processes)
            {
                long ProcId = procLoop.Id;
                string hexProcId = ProcId.ToString("x8");

                {
                    List consoleWindows = Accessibility.GetConsoleWindowsByProcessId(ProcId);
                    foreach (IntPtr consoleWindow in consoleWindows)
                    {
                        int textLength = GetWindowTextLength(consoleWindow);

                        StringBuilder sb = new StringBuilder(textLength+1);
                        GetWindowText(consoleWindow, sb, sb.Capacity);

                        string windowCaption = sb.ToString();

                        if (windowCaption.StartsWith(bashPrompt))
                        {
                            string newCaption = "bash " + windowCaption.Substring(bashPrompt.Length);
                            SetWindowText(consoleWindow, newCaption);
                            Console.WriteLine("changing window caption:" + newCaption);
                        }
                    }
                }

                {
                    string originalCaption = procLoop.MainWindowTitle;
                    if (originalCaption.StartsWith(bashPrompt))
                    {
                        string newCaption = "bash " + originalCaption.Substring(bashPrompt.Length);
                        SetWindowText(procLoop.MainWindowHandle, newCaption);
                        Console.WriteLine("changing window caption:" + newCaption);
                    }
                }
            }
        }
    }

    public class Accessibility
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll")]
        static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int processId);

        public static List GetConsoleWindowsByProcessId(long processId)
        {
            List retVal = new List();

            IntPtr hWnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "ConsoleWindowClass", null);
            while (hWnd != IntPtr.Zero)
            {
                //Console.WriteLine("Window handle:" + hWnd.ToString("x8"));
                retVal.Add(hWnd);
                hWnd = FindWindowEx(IntPtr.Zero, hWnd, "ConsoleWindowClass", null);
            }
            return retVal;
        }
    }
}

No comments:

Post a Comment