forked from gdevic/GitForce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormGitRun.cs
168 lines (149 loc) · 5.59 KB
/
FormGitRun.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Drawing;
using System.Windows.Forms;
namespace GitForce
{
/// <summary>
/// Run a Git command using threading and a run window.
/// This form should be used for Git commands that take long time to complete,
/// such are clone, push and pull.
/// </summary>
public partial class FormGitRun : Form
{
private Exec job;
private ExecResult _result = new ExecResult();
/// <summary>
/// Class constructor that also pre-sets the command and argument to be run
/// </summary>
public FormGitRun(string cmd, string args)
{
InitializeComponent();
ClassWinGeometry.Restore(this);
// WAR: On Linux, remove status bar resizing grip (since it does not work under X)
if (ClassUtils.IsMono())
statusStrip.SizingGrip = false;
job = new Exec(cmd, args);
// Reuse the same font selected as fixed-pitch
textStdout.Font = Properties.Settings.Default.commitFont;
textStdout.Text += cmd + Environment.NewLine;
textStdout.Text += args + Environment.NewLine;
}
/// <summary>
/// Form is closing.
/// </summary>
private void FormGitRunFormClosing(object sender, FormClosingEventArgs e)
{
ClassWinGeometry.Save(this);
}
/// <summary>
/// Start running the preset command at the time form is first shown
/// </summary>
private void FormGitRunShown(object sender, EventArgs e)
{
// Start the job using our own output handlers
job.AsyncRun(PStdout, PStderr, PComplete);
}
/// <summary>
/// Returns the result structure from running a job
/// </summary>
public ExecResult GetResult()
{
return _result;
}
/// <summary>
/// Callback that handles process printing to stdout
/// </summary>
private void PStdout(String message)
{
textStdout.AppendText(ClassUtils.ToPlainAscii(message) + Environment.NewLine);
// Keep the newly added text visible
textStdout.SelectionStart = textStdout.TextLength;
textStdout.ScrollToCaret();
}
/// <summary>
/// Callback that handles process printing to stderr.
/// Prints the stderr to a log window.
/// </summary>
private void PStderr(String message)
{
textStdout.AppendText(ClassUtils.ToPlainAscii(message) + Environment.NewLine, Color.Red);
// Keep the newly added text visible
textStdout.SelectionStart = textStdout.TextLength;
textStdout.ScrollToCaret();
// Append the stderr stream message to a log window
App.PrintLogMessage("stderr: " + message);
}
/// <summary>
/// Callback that handles process completion event
/// </summary>
private void PComplete(ExecResult result)
{
_result = result;
if (result.Success())
{
toolStripStatus.Text = "Git command completed successfully.";
textStdout.AppendText("Git command completed successfully.", Color.Green);
}
else
{
toolStripStatus.Text = "Git command failed!";
textStdout.AppendText("Git command failed!", Color.Red);
}
btCancel.Text = "Done";
StopProgress();
}
/// <summary>
/// User presses a cancel button. This is a multi-function button
/// that starts as "Cancel"...
/// </summary>
private void BtCancelClick(object sender, EventArgs e)
{
StopProgress();
if (btCancel.Text == "Cancel")
{
textStdout.AppendText(Environment.NewLine + "Error: Git command interrupted!" + Environment.NewLine, Color.Purple);
toolStripStatus.Text = "Git command interrupted.";
job.Terminate();
btCancel.Text = "Close";
DialogResult = DialogResult.None;
}
else
{
if (btCancel.Text == "Done")
DialogResult = DialogResult.OK;
if (btCancel.Text == "Close")
DialogResult = DialogResult.Cancel;
}
}
/// <summary>
/// Call this function when the command completed, or is about to complete.
/// It signals to the user the end of command by enabling the text box
/// and disabling the progress indicator.
/// </summary>
private void StopProgress()
{
textStdout.ReadOnly = false;
timerProgress.Enabled = false;
labelProgress.Text = " ";
}
/// <summary>
/// The phase of the progress indicator (0..7)
/// </summary>
private int _progressPhase;
/// <summary>
/// Use timer to animate progress indicator.
/// </summary>
private void TimerProgressTick(object sender, EventArgs e)
{
labelProgress.Text = @"|/-\|/-\"[_progressPhase].ToString();
_progressPhase = (_progressPhase + 1)%8;
}
/// <summary>
/// Called when the user clicks on an HTML link inside the output text.
/// </summary>
private void textLinkClicked(object sender, LinkClickedEventArgs e)
{
ClassUtils.OpenWebLink(e.LinkText);
}
}
}