Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
79 changes: 67 additions & 12 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 @@ public class DateField : TextField {
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 { get { return isShort ? shortFieldLen : longFieldLen; } }
string Format { get { return isShort ? shortFormat : longFormat; } }
Expand All @@ -47,9 +48,11 @@ public class DateField : TextField {
/// <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);
}

Expand All @@ -62,9 +65,13 @@ public DateField () : this (DateTime.MinValue) { }
/// 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 ("")
{
this.isShort = true;

this.isJalaali = isJalaali;
if (!isJalaali)
this.isShort = true;
Width = FieldLen + 2;
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.
Initialize (date);
}
Expand All @@ -73,8 +80,8 @@ void Initialize (DateTime date)
{
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;
Date = date;
TextChanged += DateField_Changed;
Expand All @@ -83,8 +90,10 @@ void Initialize (DateTime date)
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 @@ -129,14 +138,21 @@ public DateTime Date {

var oldData = date;
date = value;
this.Text = value.ToString (Format);
if (isJalaali) {

this.Text = ToJalaaliString (Format, date);
} else {
this.Text = value.ToString (Format);
}
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);
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 @@ -202,11 +218,28 @@ bool SetText (ustring text)
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) ||
!isValidDate)
return false;
}

if (!DateTime.TryParseExact (d, Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result) ||
!isValidDate)
return false;
Date = result;
return true;
}
Expand Down Expand Up @@ -377,6 +410,28 @@ public virtual void OnDateChanged (DateTimeEventArgs<DateTime> args)
{
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 All @@ -386,7 +441,7 @@ public class DateTimeEventArgs<T> : EventArgs {
/// <summary>
/// The old <see cref="DateField"/> or <see cref="TimeField"/> value.
/// </summary>
public T OldValue {get;}
public T OldValue { get; }

/// <summary>
/// The new <see cref="DateField"/> or <see cref="TimeField"/> value.
Expand Down
4 changes: 3 additions & 1 deletion UICatalog/Scenarios/Text.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public override void Setup ()
Y = Pos.Bottom (hexView) + 1,
Width = 20,
//ColorScheme = Colors.Dialog,
IsShortFormat = false
IsShortFormat = false,

};
Win.Add (dateField);

Expand All @@ -81,6 +82,7 @@ public override void Setup ()
dateField.TextChanged += (prev) => {
labelMirroringDateField.Text = dateField.Text;
};


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