Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 77 additions & 9 deletions Terminal.Gui/Views/DateField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
//
// Licensed under the MIT license
//
using NStack;
using System;
using System.Globalization;
using System.Linq;
using NStack;

namespace Terminal.Gui {
/// <summary>
Expand All @@ -25,6 +25,7 @@
string sepChar;
string longFormat;
string shortFormat;
bool isJalaali;
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Field 'isJalaali' can be 'readonly'.

Suggested change
bool isJalaali;
readonly bool isJalaali;

Copilot uses AI. Check for mistakes.

int fieldLen => isShort ? shortFieldLen : longFieldLen;
string format => isShort ? shortFormat : longFormat;
Expand All @@ -47,9 +48,14 @@
/// <param name="y">The y coordinate.</param>
/// <param name="date">Initial date contents.</param>
/// <param name="isShort">If true, shows only two digits for the year.</param>
public DateField (int x, int y, DateTime date, bool isShort = false) : base (x, y, isShort ? 10 : 12, "")
/// <param name="isJalaali">If true, parse will convert jalaali input fo georgian date</param>
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in documentation: "fo georgian" should be "to Gregorian".

Suggested change
/// <param name="isJalaali">If true, parse will convert jalaali input fo georgian date</param>
/// <param name="isJalaali">If true, parse will convert jalaali input to Gregorian date</param>

Copilot uses AI. Check for mistakes.
public DateField (int x, int y, DateTime date, bool isShort = false, bool isJalaali = false) : base (x, y, isShort ? 10 : 12, "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few unit tests that prove the various constructors work as they should would be good.

{

this.isShort = isShort;
this.isJalaali = isJalaali;
Initialize (date, isShort);

}

Comment on lines 53 to 60
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Unnecessary blank lines: Excessive vertical whitespace on lines 54, 58, 73, 78 reduces code readability. According to the Mono Coding Guidelines (referenced in CONTRIBUTING.md), blank lines should be used sparingly and purposefully to separate logical sections of code.

Suggested change
{
this.isShort = isShort;
this.isJalaali = isJalaali;
Initialize (date, isShort);
}
public class DateField : TextField {
this.isJalaali = isJalaali;
Initialize (date, isShort);

Copilot uses AI. Check for mistakes.
/// <summary>
Expand All @@ -61,19 +67,28 @@
/// Initializes a new instance of <see cref="DateField"/> using <see cref="LayoutStyle.Computed"/> layout.
/// </summary>
/// <param name="date"></param>
public DateField (DateTime date) : base ("")
/// <param name="isJalaali"></param>
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing XML documentation: The new isJalaali parameter lacks proper XML documentation. According to the CONTRIBUTING.md guidelines, all public API parameters must have clear, concise documentation using <param> tags.

Copilot uses AI. Check for mistakes.
public DateField (DateTime date, bool isJalaali = false) : base ("")
{
Width = fieldLen + 2;

this.isJalaali = isJalaali;
if (!isJalaali)
this.isShort = true;
Width = FieldLen + 2;

Check failure on line 77 in Terminal.Gui/Views/DateField.cs

View workflow job for this annotation

GitHub Actions / Non-Parallel Unit Tests (ubuntu-latest)

The name 'FieldLen' does not exist in the current context

Check failure on line 77 in Terminal.Gui/Views/DateField.cs

View workflow job for this annotation

GitHub Actions / Non-Parallel Unit Tests (ubuntu-latest)

The name 'FieldLen' does not exist in the current context
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case sensitivity issue: FieldLen should be fieldLen (lowercase). The property fieldLen is defined as a lowercase property expression, not FieldLen.

Suggested change
Width = FieldLen + 2;
Width = fieldLen + 2;

Copilot uses AI. Check for mistakes.

Comment on lines +73 to +78
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Unnecessary blank lines: Excessive vertical whitespace reduces code readability. According to the Mono Coding Guidelines (referenced in CONTRIBUTING.md), blank lines should be used sparingly and purposefully to separate logical sections of code.

Copilot uses AI. Check for mistakes.
Initialize (date);
}

void Initialize (DateTime date, bool isShort = false)
{
CultureInfo cultureInfo = CultureInfo.CurrentCulture;
sepChar = cultureInfo.DateTimeFormat.DateSeparator;
longFormat = GetLongFormat (cultureInfo.DateTimeFormat.ShortDatePattern);
shortFormat = GetShortFormat (longFormat);
longFormat = isJalaali ? $" yyyy{sepChar}MM{sepChar}dd" : GetLongFormat (cultureInfo.DateTimeFormat.ShortDatePattern);
shortFormat = isJalaali ? $" yy{sepChar}MM{sepChar}dd" : GetShortFormat (longFormat);
CursorPosition = 1;

this.isShort = isShort;

Date = date;
CursorPosition = 1;
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate CursorPosition assignment. Line 88 sets CursorPosition = 1; but it's set again on line 93. This is redundant - one of these assignments should be removed.

Suggested change
CursorPosition = 1;

Copilot uses AI. Check for mistakes.
TextChanged += DateField_Changed;
Expand Down Expand Up @@ -109,8 +124,10 @@
void DateField_Changed (ustring e)
{
try {

if (!DateTime.TryParseExact (GetDate (Text).ToString (), GetInvarianteFormat (), CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result))
Text = e;

} catch (Exception) {
Text = e;
}
Expand Down Expand Up @@ -155,14 +172,23 @@

var oldData = date;
date = value;
this.Text = value.ToString (format);
var args = new DateTimeEventArgs<DateTime> (oldData, value, format);

if (isJalaali) {

this.Text = ToJalaaliString (Format, date);

Check failure on line 178 in Terminal.Gui/Views/DateField.cs

View workflow job for this annotation

GitHub Actions / Non-Parallel Unit Tests (ubuntu-latest)

The name 'Format' does not exist in the current context

Check failure on line 178 in Terminal.Gui/Views/DateField.cs

View workflow job for this annotation

GitHub Actions / Non-Parallel Unit Tests (ubuntu-latest)

The name 'Format' does not exist in the current context
} else {
this.Text = value.ToString (Format);

Check failure on line 180 in Terminal.Gui/Views/DateField.cs

View workflow job for this annotation

GitHub Actions / Non-Parallel Unit Tests (ubuntu-latest)

The name 'Format' does not exist in the current context

Check failure on line 180 in Terminal.Gui/Views/DateField.cs

View workflow job for this annotation

GitHub Actions / Non-Parallel Unit Tests (ubuntu-latest)

The name 'Format' does not exist in the current context
}
Comment on lines +176 to +181
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both branches of this 'if' statement write to the same variable - consider using '?' to express intent better.

Suggested change
if (isJalaali) {
this.Text = ToJalaaliString (Format, date);
} else {
this.Text = value.ToString (Format);
}
this.Text = isJalaali ? ToJalaaliString (Format, date) : value.ToString (Format);

Copilot uses AI. Check for mistakes.
var args = new DateTimeEventArgs<DateTime> (oldData, value, Format);

Check failure on line 182 in Terminal.Gui/Views/DateField.cs

View workflow job for this annotation

GitHub Actions / Non-Parallel Unit Tests (ubuntu-latest)

The name 'Format' does not exist in the current context
Comment on lines +178 to +182
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case sensitivity issue: Format should be format (lowercase). The property format is defined as a lowercase property expression, not Format.

Suggested change
this.Text = ToJalaaliString (Format, date);
} else {
this.Text = value.ToString (Format);
}
var args = new DateTimeEventArgs<DateTime> (oldData, value, Format);
this.Text = ToJalaaliString (format, date);
} else {
this.Text = value.ToString (format);
}
var args = new DateTimeEventArgs<DateTime> (oldData, value, format);

Copilot uses AI. Check for mistakes.
Comment on lines +178 to +182
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case sensitivity issue: Format should be format (lowercase). The property format is defined as a lowercase property expression, not Format.

Suggested change
this.Text = ToJalaaliString (Format, date);
} else {
this.Text = value.ToString (Format);
}
var args = new DateTimeEventArgs<DateTime> (oldData, value, Format);
this.Text = ToJalaaliString (format, date);
} else {
this.Text = value.ToString (format);
}
var args = new DateTimeEventArgs<DateTime> (oldData, value, format);

Copilot uses AI. Check for mistakes.
Comment on lines +178 to +182
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case sensitivity issue: Format should be format (lowercase). The property format is defined as a lowercase property expression, not Format.

Suggested change
this.Text = ToJalaaliString (Format, date);
} else {
this.Text = value.ToString (Format);
}
var args = new DateTimeEventArgs<DateTime> (oldData, value, Format);
this.Text = ToJalaaliString (format, date);
} else {
this.Text = value.ToString (format);
}
var args = new DateTimeEventArgs<DateTime> (oldData, value, format);

Copilot uses AI. Check for mistakes.

if (oldData != value) {
OnDateChanged (args);
}
}
}



Comment on lines +190 to +191
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Unnecessary blank lines: Excessive vertical whitespace reduces code readability. According to the Mono Coding Guidelines (referenced in CONTRIBUTING.md), blank lines should be used sparingly and purposefully to separate logical sections of code.

Suggested change

Copilot uses AI. Check for mistakes.
/// <summary>
/// Get or set the date format for the widget.
/// </summary>
Expand Down Expand Up @@ -236,9 +262,29 @@
vals [idx] = day.ToString ();
} else
day = Int32.Parse (vals [idx].ToString ());
string d = GetDate (month, day, year, frm);
DateTime result;
if (isJalaali) {
try {
PersianCalendar pc = new PersianCalendar ();
if (year < 10) {
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magic number without explanation: The value 1400 on line 270 appears to be a base year for Jalaali calendar conversion when the year is less than 10, but there's no comment explaining this logic. This makes the code harder to understand and maintain. Consider adding a comment explaining why 1400 is used (likely the current Jalaali century).

Suggested change
if (year < 10) {
if (year < 10) {
// If the year is less than 10, assume it is a two-digit year in the current Jalaali century (1400s).
// The Jalaali century 1400 started in 2021 Gregorian.

Copilot uses AI. Check for mistakes.
year += 1400;
}
result = pc.ToDateTime (year, month, day, 12, 0, 0, 0);
year = result.Year;
month = result.Month;
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assignment to month is useless, since its value is never read.

Suggested change
month = result.Month;

Copilot uses AI. Check for mistakes.
day = result.Day;
Comment on lines +273 to +275
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assignment to year is useless, since its value is never read.

Suggested change
year = result.Year;
month = result.Month;
day = result.Day;
// Removed unused assignments to year, month, and day.

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assignment to day is useless, since its value is never read.

Suggested change
day = result.Day;

Copilot uses AI. Check for mistakes.
} catch {
return false;
Comment on lines +276 to +277
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generic catch clause.

Suggested change
} catch {
return false;
} catch (ArgumentOutOfRangeException) {
return false;
} catch (FormatException) {
return false;

Copilot uses AI. Check for mistakes.
}
} else {
string d = GetDate (month, day, year, frm);

if (!DateTime.TryParseExact (d, Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out result) ||

Check failure on line 282 in Terminal.Gui/Views/DateField.cs

View workflow job for this annotation

GitHub Actions / Non-Parallel Unit Tests (ubuntu-latest)

The name 'Format' does not exist in the current context
!isValidDate)
return false;
}

if (!DateTime.TryParseExact (d, format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result) ||

Check failure on line 287 in Terminal.Gui/Views/DateField.cs

View workflow job for this annotation

GitHub Actions / Non-Parallel Unit Tests (ubuntu-latest)

A local variable or function named 'result' is already defined in this scope

Check failure on line 287 in Terminal.Gui/Views/DateField.cs

View workflow job for this annotation

GitHub Actions / Non-Parallel Unit Tests (ubuntu-latest)

The name 'd' does not exist in the current context
!isValidDate)
return false;
Date = result;
Expand Down Expand Up @@ -420,6 +466,28 @@
{
DateChanged?.Invoke (args);
}

Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing XML documentation: The new ToJalaaliString method lacks XML documentation. According to the CONTRIBUTING.md guidelines, all methods should have clear, concise documentation including a <summary> tag and documentation for parameters and return values.

Suggested change
/// <summary>
/// Converts the specified <see cref="DateTime"/> to a Jalaali (Persian) date string using the given format.
/// </summary>
/// <param name="format">The format string to use for the Jalaali date. If null or empty, defaults to " yyyy/MM/dd".</param>
/// <param name="date">The <see cref="DateTime"/> value to convert.</param>
/// <returns>A string representing the Jalaali date in the specified format.</returns>

Copilot uses AI. Check for mistakes.
string ToJalaaliString (string format, DateTime date)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should have a unit test for this.

Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Local scope variable 'format' shadows DateField.format.

Copilot uses AI. Check for mistakes.
{
if (string.IsNullOrEmpty (format)) format = " yyyy/MM/dd";
PersianCalendar pc = new PersianCalendar ();


var year = pc.GetYear (date);
var month = pc.GetMonth (date);
var day = pc.GetDayOfMonth (date);
Comment on lines +470 to +478
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Local scope variable 'date' shadows DateField.date.

Suggested change
string ToJalaaliString (string format, DateTime date)
{
if (string.IsNullOrEmpty (format)) format = " yyyy/MM/dd";
PersianCalendar pc = new PersianCalendar ();
var year = pc.GetYear (date);
var month = pc.GetMonth (date);
var day = pc.GetDayOfMonth (date);
string ToJalaaliString (string format, DateTime inputDate)
{
if (string.IsNullOrEmpty (format)) format = " yyyy/MM/dd";
PersianCalendar pc = new PersianCalendar ();
var year = pc.GetYear (inputDate);
var month = pc.GetMonth (inputDate);
var day = pc.GetDayOfMonth (inputDate);

Copilot uses AI. Check for mistakes.


Comment on lines +474 to +480
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Unnecessary blank lines: Excessive vertical whitespace reduces code readability. According to the Mono Coding Guidelines (referenced in CONTRIBUTING.md), blank lines should be used sparingly and purposefully to separate logical sections of code.

Suggested change
var year = pc.GetYear (date);
var month = pc.GetMonth (date);
var day = pc.GetDayOfMonth (date);
var year = pc.GetYear (date);
var month = pc.GetMonth (date);
var day = pc.GetDayOfMonth (date);

Copilot uses AI. Check for mistakes.
Comment on lines +475 to +480
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Unnecessary blank lines: Excessive vertical whitespace reduces code readability. According to the Mono Coding Guidelines (referenced in CONTRIBUTING.md), blank lines should be used sparingly and purposefully to separate logical sections of code.

Suggested change
var year = pc.GetYear (date);
var month = pc.GetMonth (date);
var day = pc.GetDayOfMonth (date);
var year = pc.GetYear (date);
var month = pc.GetMonth (date);
var day = pc.GetDayOfMonth (date);

Copilot uses AI. Check for mistakes.
format = format.Replace ("yyyy", year.ToString (CultureInfo.InvariantCulture));
format = format.Replace ("yy", (year % 100).ToString ("00", CultureInfo.InvariantCulture));
format = format.Replace ("MM", month.ToString ("00", CultureInfo.InvariantCulture));
format = format.Replace ("M", month.ToString (CultureInfo.InvariantCulture));
format = format.Replace ("dd", day.ToString ("00", CultureInfo.InvariantCulture));
format = format.Replace ("d", day.ToString (CultureInfo.InvariantCulture));

return format;
}

}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions UICatalog/Scenarios/Text.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ void TextView_DrawContent (Rect e)
dateField.TextChanged += (prev) => {
labelMirroringDateField.Text = dateField.Text;
};


_timeField = new TimeField (DateTime.Now.TimeOfDay) {
X = Pos.Right (labelMirroringDateField) + 5,
Expand Down
Loading