Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

feat(bar): calendar week in clock component (#197)

s-v-odb569065ff4c
diff --git a/GlazeWM.Bar/Components/ClockComponentViewModel.cs b/GlazeWM.Bar/Components/ClockComponentViewModel.cs
index 394761ff..5dc149b2 100644
--- a/GlazeWM.Bar/Components/ClockComponentViewModel.cs
+++ b/GlazeWM.Bar/Components/ClockComponentViewModel.cs
@@ -1,6 +1,7 @@
-using System;
+using System;
using System.Globalization;
using System.Reactive.Linq;
+using System.Text;
using GlazeWM.Domain.UserConfigs;
namespace GlazeWM.Bar.Components
@@ -8,15 +9,59 @@ namespace GlazeWM.Bar.Components
public class ClockComponentViewModel : ComponentViewModel
{
private ClockComponentConfig _config => _componentConfig as ClockComponentConfig;
- private string _timeFormatting => _config.TimeFormatting;
+ private string _timeFormatting => formatCalendarWeek(_config.TimeFormatting);
/// <summary>
/// Format the current time with the user's formatting config.
/// </summary>
- public string FormattedTime => DateTime.Now.ToString(
- _timeFormatting,
- CultureInfo.InvariantCulture
- );
+ public string FormattedTime => DateTime.Now.ToString(_timeFormatting, CultureInfo.InvariantCulture);
+
+
+ private String formatCalendarWeek(string timeFormat)
+ {
+
+ if (!timeFormat.Contains('w')) return timeFormat;
+
+ var now = DateTime.Now;
+ var i = 0;
+ var result = new StringBuilder();
+
+ while (i < timeFormat.Length)
+ {
+ var nextChar = i + 1 < timeFormat.Length ? timeFormat[i + 1].ToString() : "";
+ switch (timeFormat[i])
+ {
+ case '\\':
+ {
+ result.Append(timeFormat[i]);
+ result.Append(nextChar);
+ i = i + 2;
+ break;
+ }
+ case 'w':
+ {
+ if (nextChar.Equals("w"))
+ {
+ i = i + 2;
+ result.Append(ISOWeek.GetWeekOfYear(now).ToString("00"));
+ }
+ else
+ {
+ i++;
+ result.Append(ISOWeek.GetWeekOfYear(now).ToString());
+ }
+ break;
+ }
+ default:
+ {
+ result.Append(timeFormat[i]);
+ i++;
+ break;
+ }
+ }
+ }
+ return result.ToString();
+ }
public ClockComponentViewModel(
BarViewModel parentViewModel,
diff --git a/GlazeWM.Bootstrapper/sample-config.yaml b/GlazeWM.Bootstrapper/sample-config.yaml
index acde0e03..690ee7c9 100644
--- a/GlazeWM.Bootstrapper/sample-config.yaml
+++ b/GlazeWM.Bootstrapper/sample-config.yaml
@@ -26,7 +26,11 @@ bar:
margin: "0 4px 0 0"
padding: "0 8px"
- type: "clock"
- time_formatting: "hh:mm tt ddd MMM d"
+ # supported time format specifier:
+ # https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
+ # "w" Calendarweek (ISO 8601 week) 1..53
+ # "ww" CalendarWeek (ISO 8601 week) 01..53
+ time_formatting: "hh:mm tt ddd MMM d"
margin: "0 0 0 10px"
workspaces:
diff --git a/GlazeWM.Domain/UserConfigs/ClockComponentConfig.cs b/GlazeWM.Domain/UserConfigs/ClockComponentConfig.cs
index 5b4f5dc6..a6a31b5f 100644
--- a/GlazeWM.Domain/UserConfigs/ClockComponentConfig.cs
+++ b/GlazeWM.Domain/UserConfigs/ClockComponentConfig.cs
@@ -1,3 +1,6 @@
+using System;
+using System.Globalization;
+
namespace GlazeWM.Domain.UserConfigs
{
public class ClockComponentConfig : BarComponentConfig
diff --git a/README.md b/README.md
index 805709f2..c53b6c87 100644
--- a/README.md
+++ b/README.md
@@ -215,6 +215,23 @@ Using the example of padding:
- When three values are specified, the first padding applies to the top, the second to the right and left, the third to the bottom.
- When four values are specified, the paddings apply to the top, right, bottom, and left in that order (clockwise).
+### Bar component: Clock
+
+The appearence of the clock component is defined by `time_formatting`. Supported time format specifier are the standard [Custom date and time format strings](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings).
+
+Additionally supported format specifier:
+
+| Specifier | Description | Example |
+| --- | --- | --- |
+| "w" | Week of year: 1..53 | 'HH:mm dd.MM.yyyy (ww CW)' => 13:05 21.12.2022 (51 CW) |
+| "ww" | Week of year 01..53 | 'HH:mm dd.MM.yyyy (ww CW)' => 13:05 02.01.2022 (02 CW) |
+
+```yaml
+- type: "clock"
+ time_formatting: "hh:mm tt ddd MMM d"
+ margin: "0 0 0 10px"
+```
+
## Window rules
Commands can be run when a window is initially launched. This can be used to assign an app to a specific workspace or to always start an app in floating mode.