Adding support for Solarized colors; use dark palette

This commit is contained in:
John Doty 2012-03-22 14:55:59 -07:00
parent 3af6307831
commit 17c5fcadef
2 changed files with 204 additions and 103 deletions

View file

@ -0,0 +1,195 @@
function Add-ConsoleHelper()
{
$code = @"
using System;
using System.Drawing;
using System.Runtime.InteropServices;
public static class ConsoleColorHelper
{
[DllImport( "kernel32.dll", SetLastError = true )]
static extern bool SetConsoleScreenBufferInfoEx(
IntPtr ConsoleOutput,
ref CONSOLE_SCREEN_BUFFER_INFO_EX ConsoleScreenBufferInfoEx
);
[DllImport( "kernel32.dll", SetLastError = true )]
static extern bool GetConsoleScreenBufferInfoEx(
IntPtr hConsoleOutput,
[In, Out] ref CONSOLE_SCREEN_BUFFER_INFO_EX ConsoleScreenBufferInfo
);
[DllImport( "kernel32.dll", SetLastError = true )]
static extern IntPtr GetStdHandle(
int nStdHandle
);
public static Color GetStandardConsoleColor( ConsoleColor color )
{
IntPtr hConsole = GetStdHandle( -11 );
CONSOLE_SCREEN_BUFFER_INFO_EX info = new CONSOLE_SCREEN_BUFFER_INFO_EX();
info.cbSize = Marshal.SizeOf( typeof( CONSOLE_SCREEN_BUFFER_INFO_EX ) );
if ( !GetConsoleScreenBufferInfoEx( hConsole, ref info ) )
{
throw Marshal.GetExceptionForHR( Marshal.GetHRForLastWin32Error() );
}
return info.ColorTable[(int)color].GetColor();
}
public static void SetStandardConsoleColor( ConsoleColor color, Color newColor )
{
IntPtr hConsole = GetStdHandle( -11 );
CONSOLE_SCREEN_BUFFER_INFO_EX info = new CONSOLE_SCREEN_BUFFER_INFO_EX();
info.cbSize = Marshal.SizeOf( typeof( CONSOLE_SCREEN_BUFFER_INFO_EX ) );
if ( !GetConsoleScreenBufferInfoEx( hConsole, ref info ) )
{
throw Marshal.GetExceptionForHR( Marshal.GetHRForLastWin32Error() );
}
info.ColorTable[(int)color] = new COLORREF( newColor );
if ( !SetConsoleScreenBufferInfoEx( hConsole, ref info ) )
{
throw Marshal.GetExceptionForHR( Marshal.GetHRForLastWin32Error() );
}
}
[StructLayout( LayoutKind.Sequential )]
internal struct COORD
{
internal short X;
internal short Y;
}
[StructLayout( LayoutKind.Sequential )]
internal struct SMALL_RECT
{
internal short Left;
internal short Top;
internal short Right;
internal short Bottom;
}
[StructLayout( LayoutKind.Sequential )]
internal struct COLORREF
{
internal uint ColorDWORD;
internal COLORREF( Color color )
{
ColorDWORD = (uint)color.R + ( ( (uint)color.G ) << 8 ) + ( ( (uint)color.B ) << 16 );
}
internal COLORREF( uint r, uint g, uint b )
{
ColorDWORD = r + ( g << 8 ) + ( b << 16 );
}
internal Color GetColor()
{
return Color.FromArgb( (int)( 0x000000FFU & ColorDWORD ),
(int)( 0x0000FF00U & ColorDWORD ) >> 8, (int)( 0x00FF0000U & ColorDWORD ) >> 16 );
}
internal void SetColor( Color color )
{
ColorDWORD = (uint)color.R + ( ( (uint)color.G ) << 8 ) + ( ( (uint)color.B ) << 16 );
}
}
[StructLayout( LayoutKind.Sequential )]
internal struct CONSOLE_SCREEN_BUFFER_INFO_EX
{
internal int cbSize;
internal COORD dwSize;
internal COORD dwCursorPosition;
internal ushort wAttributes;
internal SMALL_RECT srWindow;
internal COORD dwMaximumWindowSize;
internal ushort wPopupAttributes;
[MarshalAs( UnmanagedType.Bool )]
internal bool bFullscreenSupported;
[MarshalAs( UnmanagedType.ByValArray, SizeConst = 16 )]
internal COLORREF[] ColorTable;
}
}
"@
Add-Type -TypeDefinition $code -ReferencedAssemblies @('System.Drawing')
}
# These are the 16 colors for our palette.
Add-Type -AssemblyName System.Drawing
$base03 = [System.Drawing.Color]::FromArgb(0x002b36)
$base02 = [System.Drawing.Color]::FromArgb(0x073642)
$base01 = [System.Drawing.Color]::FromArgb(0x586e75)
$base00 = [System.Drawing.Color]::FromArgb(0x657b83)
$base0 = [System.Drawing.Color]::FromArgb(0x839496)
$base1 = [System.Drawing.Color]::FromArgb(0x93a1a1)
$base2 = [System.Drawing.Color]::FromArgb(0xeee8d5)
$base3 = [System.Drawing.Color]::FromArgb(0xfdf6e3)
$yellow = [System.Drawing.Color]::FromArgb(0xb58900)
$orange = [System.Drawing.Color]::FromArgb(0xcb4b16)
$red = [System.Drawing.Color]::FromArgb(0xdc322f)
$magenta = [System.Drawing.Color]::FromArgb(0xd33682)
$violet = [System.Drawing.Color]::FromArgb(0x6c71c4)
$blue = [System.Drawing.Color]::FromArgb(0x268bd2)
$cyan = [System.Drawing.Color]::FromArgb(0x2aa198)
$green = [System.Drawing.Color]::FromArgb(0x859900)
function Set-SolarizedColors([switch]$Light, [switch]$Dark)
{
# Redefine the standard console colors to have the Solarized-Light
# palette.
#
# Note that we're doing funny things here, like setting black to
# white, because we are trying to make it so that programs that
# don't redefine the palette still get a coherent scheme.
#
# We try to map with cognates, but it's not easy....
#
[ConsoleColorHelper]::SetStandardConsoleColor([ConsoleColor]::Black, $base03)
[ConsoleColorHelper]::SetStandardConsoleColor([ConsoleColor]::DarkGray, $base02)
[ConsoleColorHelper]::SetStandardConsoleColor([ConsoleColor]::Gray, $base01)
[ConsoleColorHelper]::SetStandardConsoleColor([ConsoleColor]::DarkBlue, $base00)
[ConsoleColorHelper]::SetStandardConsoleColor([ConsoleColor]::DarkGreen, $base0)
[ConsoleColorHelper]::SetStandardConsoleColor([ConsoleColor]::DarkCyan, $base1)
[ConsoleColorHelper]::SetStandardConsoleColor([ConsoleColor]::DarkRed, $base2)
[ConsoleColorHelper]::SetStandardConsoleColor([ConsoleColor]::White, $base3)
[ConsoleColorHelper]::SetStandardConsoleColor([ConsoleColor]::Yellow, $yellow)
[ConsoleColorHelper]::SetStandardConsoleColor([ConsoleColor]::DarkYellow, $orange)
[ConsoleColorHelper]::SetStandardConsoleColor([ConsoleColor]::Red, $red)
[ConsoleColorHelper]::SetStandardConsoleColor([ConsoleColor]::Magenta, $magenta)
[ConsoleColorHelper]::SetStandardConsoleColor([ConsoleColor]::DarkMagenta, $violet)
[ConsoleColorHelper]::SetStandardConsoleColor([ConsoleColor]::Blue, $blue)
[ConsoleColorHelper]::SetStandardConsoleColor([ConsoleColor]::Cyan, $cyan)
[ConsoleColorHelper]::SetStandardConsoleColor([ConsoleColor]::Green, $green)
if ($Light)
{
$themefg = [ConsoleColor]::White # $base3
$themebg = [ConsoleColor]::DarkBlue # $base00
}
if ($Dark)
{
$themefg = [ConsoleColor]::DarkGreen # $base0
$themebg = [ConsoleColor]::Black # $base03
}
if ($Light -or $Dark)
{
(Get-Host).UI.RawUI.BackgroundColor = $themebg
(Get-Host).UI.RawUI.ForegroundColor = $themefg
}
}
Add-ConsoleHelper

View file

@ -1,3 +1,9 @@
# Load up the colors and set the dark solarized theme.
#
$profileDir = split-path -parent $Profile
Import-Module "$profileDir\Modules\ConsoleColors.psm1"
Set-SolarizedColors -Dark
# These bits of profile courtesy of http://winterdom.com/ # These bits of profile courtesy of http://winterdom.com/
# #
# http://winterdom.com/2008/01/modifyingthehomeinpowershell # http://winterdom.com/2008/01/modifyingthehomeinpowershell
@ -21,9 +27,11 @@ function shorten-path([string] $path)
function prompt function prompt
{ {
# our theme
$ok = $? $ok = $?
# Our "theme", as it were. Note that we assume the use of the
# solarized colors.
#
$cdelim = [ConsoleColor]::DarkCyan $cdelim = [ConsoleColor]::DarkCyan
$chost = [ConsoleColor]::Green $chost = [ConsoleColor]::Green
$cloc = $csym = [ConsoleColor]::Cyan $cloc = $csym = [ConsoleColor]::Cyan
@ -37,105 +45,3 @@ function prompt
return ' ' return ' '
} }
function Get-TfsWorkspace(
[string] $path = "."
)
{
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.VersionControl.Client")
function InitServerAndWorkspaceFromWSInfo($wsInfo)
{
$tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($wsInfo.ServerUri.AbsoluteUri)
$vcs = $tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
return $vcs.GetWorkspace($wsInfo)
}
# is there only 1 workspace in our cache file? If so, use that one regardless of the hint
$workspaceInfos = [Microsoft.TeamFoundation.VersionControl.Client.Workstation]::Current.GetAllLocalWorkspaceInfo()
if ($workspaceInfos.Length -eq 1)
{
return InitServerAndWorkspaceFromWSInfo($workspaceInfos[0])
}
$workspaceInfos = [Microsoft.TeamFoundation.VersionControl.Client.Workstation]::Current.GetLocalWorkspaceInfoRecursively($path)
if ($workspaceInfos.Length -gt 1)
{
throw 'More than one workspace matches the workspace hint "{0}": {1}' -f
$path, [string]::join(', ', @($workspaceInfos | %{ $_.Name}))
}
elseif ($workspaceInfos.Length -eq 1)
{
return InitServerAndWorkspaceFromWSInfo($workspaceInfos[0])
}
else
{
throw "Could not figure out a workspace based on $path"
}
}
function Get-TfsHistory(
[string] $path,
[switch] $raw
)
{
$workspace = Get-TfsWorkspace $path
$vcs = $workspace.VersionControlServer
$serverPath = $workspace.GetServerItemForLocalItem($path)
$changes = $vcs.QueryHistory($serverPath, [Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest, 0, [Microsoft.TeamFoundation.VersionControl.Client.RecursionType]::None, $null, $null, $null, 100, $false, $false)
if ($raw)
{
foreach($cs in $changes)
{
$cs
}
}
else
{
foreach($cs in $changes)
{
new-object PSObject -Property @{
Uri = $cs.ArtifactUri;
Comment = $cs.Comment;
Committer = $cs.Committer;
Owner = $cs.Owner;
Date = $cs.CreationDate;
Id = $cs.ChangesetId;
}
}
}
}
function get-directoryOfFileAbove($file, $start = $null)
{
if (-not $start)
{
$start = resolve-path "."
}
while(($start) -and (-not (test-path "$start\$file")))
{
$start = split-path -Parent $start
}
return $start
}
function die($message)
{
write-host -Foreground "Red" $message
exit 1
}
function add-vhd($vhdfile) {
$path = resolve-path $vhdfile
$script = "SELECT VDISK FILE=`"$path`"`r`nATTACH VDISK"
$script | diskpart
}
function remove-vhd($vhdfile) {
$path = resolve-path $vhdfile
$script = "SELECT VDISK FILE=`"$path`"`r`nDETACH VDISK"
$script | diskpart
}