00001
00002
00003
00004
00005 using System;
00006 using System.Text;
00007 using System.Threading;
00008 using System.Runtime.InteropServices;
00009
00010 namespace Tamir.SharpSsh.jsch.examples
00011 {
00015 public class ConsoleProgressBar
00016 {
00017 private const int STD_OUTPUT_HANDLE = -11;
00018 private int mHConsoleHandle;
00019 COORD barCoord;
00020
00021 [StructLayout(LayoutKind.Sequential)]
00022 public struct COORD
00023 {
00024 public short X;
00025 public short Y;
00026 public COORD(short x, short y)
00027 {
00028 X = x;
00029 Y = y;
00030 }
00031 }
00032
00033 [StructLayout(LayoutKind.Sequential)]
00034 struct SMALL_RECT
00035 {
00036 public short Left;
00037 public short Top;
00038 public short Right;
00039 public short Bottom;
00040 }
00041
00042 [StructLayout(LayoutKind.Sequential)]
00043 struct CONSOLE_SCREEN_BUFFER_INFO
00044 {
00045 public COORD dwSize;
00046 public COORD dwCursorPosition;
00047 public int wAttributes;
00048 public SMALL_RECT srWindow;
00049 public COORD dwMaximumWindowSize;
00050 }
00051
00052 [DllImport("kernel32.dll", EntryPoint="GetStdHandle", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
00053 private static extern int GetStdHandle(int nStdHandle);
00054
00055 [DllImport("kernel32.dll", EntryPoint="GetConsoleScreenBufferInfo", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
00056 private static extern int GetConsoleScreenBufferInfo(int hConsoleOutput, out CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);
00057
00058 [DllImport("kernel32.dll", EntryPoint="SetConsoleCursorPosition", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
00059 private static extern int SetConsoleCursorPosition(int hConsoleOutput, COORD dwCursorPosition);
00060
00062 public ConsoleProgressBar()
00063 {
00064
00065
00066
00067 mHConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
00068
00069 barCoord = this.GetCursorPos();
00070 Console.WriteLine();
00071 Console.WriteLine();
00072 Console.WriteLine();
00073 }
00074
00075 public void SetCursorPos(short x, short y)
00076 {
00077 SetConsoleCursorPosition(mHConsoleHandle, new COORD(x, y));
00078 }
00079
00080 public COORD GetCursorPos()
00081 {
00082 CONSOLE_SCREEN_BUFFER_INFO res;
00083 GetConsoleScreenBufferInfo(mHConsoleHandle, out res);
00084 return res.dwCursorPosition;
00085 }
00086
00087 StringBuilder progressBar = new StringBuilder();
00094 public void Update(int transferredBytes, int totalBytes, string message)
00095 {
00096 COORD cur = this.GetCursorPos();
00097 this.SetCursorPos(barCoord.X, barCoord.Y);
00098
00099 int progress;
00100 if(totalBytes!=0)
00101 progress = (int)(transferredBytes*100.0/totalBytes);
00102 else
00103 progress = 0;
00104
00105 progressBar.Length=0;
00106 progressBar.Append( progress );
00107 progressBar.Append( "% [" );
00108
00109 for(double i=0; i<50; i++)
00110 {
00111 if (i*2<progress) progressBar.Append( "#" );
00112 else progressBar.Append( "-" );
00113 }
00114
00115 progressBar.Append( "] ");
00116
00117 if(totalBytes!=0)
00118 {
00119 int transferredKB = (int)(transferredBytes/1000.0);
00120 int totalKB = (int)(totalBytes/1000.0);
00121 progressBar.Append( (comma(transferredKB)+"K/"+comma(totalKB)+"K\n") );
00122 }
00123 else
00124 {
00125 progressBar.Append( "0.0K\n" );
00126 }
00127 progressBar.Append( message );
00128 progressBar.Append( " \n");
00129
00130 Console.Write(progressBar);
00131 this.SetCursorPos(cur.X, cur.Y);
00132 }
00133
00137 private string comma(int n)
00138 {
00139 string s = n.ToString();
00140 for(int i=s.Length-3; i>0; i=i-3)
00141 {
00142 s = s.Insert(i, ",");
00143 }
00144 return s;
00145 }
00146 }
00147 }