Skip to content

Commit

Permalink
#5 Add option to only show repos with relevant changes
Browse files Browse the repository at this point in the history
- New option "OnlyShowChanged"
- Changes are considered from GitStatusProvider and GitBranchProvider
- GitBranchProvider allows to define a list of default branches that are evaluated as "not changed"
- Change Cell to record type and add new field "IsChanged"
- Rename TableViewOptions to TableOptions
  • Loading branch information
MaxAtoms committed Feb 6, 2022
1 parent 93804fa commit 83f49dd
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 33 deletions.
10 changes: 9 additions & 1 deletion RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ Version 0.6 introduces table order options

## New features

- Repos in the table can now be sorted either alphabetically or according to their last modification. Ascending as well as descending can be set as options.
### Sorting

Repos in the table can now be sorted either alphabetically or according to their last modification.
Ascending as well as descending can be set as options.

### Only show changed

New option "OnlyShowChanged" allows to only print repos that have either a changed Git status or a HEAD that ponts to a branch that is not default.
Default branches are configurable.
10 changes: 1 addition & 9 deletions src/RepoStatusTable/CellProviders/Cell.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
namespace RepoStatusTable.CellProviders;

public class Cell
{
public Cell( string content )
{
Content = content;
}

public string Content { get; }
}
public record Cell( string Content, bool IsChanged = true );
8 changes: 7 additions & 1 deletion src/RepoStatusTable/CellProviders/GitBranchProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ public Task<Cell> GetCell( string path )
var branchName = _gitFacade.GetCurrentBranch( path );

return Task.FromResult( new Cell(
branchName
branchName,
IsChanged( branchName )
) );
}

private bool IsChanged( string branch )
{
return _options.DefaultBranches.Contains( branch );
}
}
4 changes: 2 additions & 2 deletions src/RepoStatusTable/CellProviders/GitStatusProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public Task<Cell> GetCell( string directory )
var status = _gitFacade.GetStatus( directory );

return Task.FromResult(
new Cell( GetStatusDescription( status ) ) );
new Cell( GetStatusDescription( status ), IsStatusChanged( status ) ) );
}

private static bool IsStatusChanged( IDictionary<string, int> status )
{
return !status.All( v => v.Value == 0 );
return status.Any( v => v.Value != 0 );
}

private static string GetStatusDescription( IDictionary<string, int> status )
Expand Down
1 change: 0 additions & 1 deletion src/RepoStatusTable/CellProviders/ICellProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ public interface ICellProvider
/// </summary>
public string Heading { get; }


/// <summary>
/// Determine whether the output of the cell provider should be shown in the output model
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/RepoStatusTable/DependencyInjection/OptionsBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private void AddOptionsSections( IConfiguration configurationRoot )
_collection.AddOptions<HeadlineOptions>()
.Bind( configurationRoot.GetSection( OptionsConstants.Headline ) )
.ValidateDataAnnotations();
_collection.AddOptions<TableViewOptions>()
_collection.AddOptions<TableOptions>()
.Bind( configurationRoot.GetSection( OptionsConstants.Table ) );

// Spectre Console
Expand Down
2 changes: 1 addition & 1 deletion src/RepoStatusTable/Model/ITableModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ public interface ITableModel
/// <returns>
/// Table as a list of rows each containing cells
/// </returns>
IAsyncEnumerable<IAsyncEnumerable<string>> GetTableAsync();
IAsyncEnumerable<IEnumerable<string>> GetTableAsync();
}
26 changes: 17 additions & 9 deletions src/RepoStatusTable/Model/TableModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using RepoStatusTable.CellProviders;
using RepoStatusTable.Options;
using RepoStatusTable.Utilities;

namespace RepoStatusTable.Model;
Expand All @@ -7,34 +8,41 @@ public class TableModel : ITableModel
{
private readonly IEnumerable<ICellProvider> _cellProviders;
private readonly IReposDirectoryUtility _reposDirectoryUtility;
private readonly TableOptions _tableOptions;

public TableModel( ICellProviderManager cellProviderManager, IReposDirectoryUtility reposDirectoryUtility )
public TableModel( ICellProviderManager cellProviderManager, IReposDirectoryUtility reposDirectoryUtility,
IOptions<TableOptions> tableOptions )
{
_cellProviders = cellProviderManager.GetOrderedListOfEnabledCellProviders();
_reposDirectoryUtility = reposDirectoryUtility;
_tableOptions = tableOptions.Value;
}

public IEnumerable<string> GetHeadings()
{
return _cellProviders.Select( p => p.Heading );
}

public async IAsyncEnumerable<IAsyncEnumerable<string>> GetTableAsync()
public async IAsyncEnumerable<IEnumerable<string>> GetTableAsync()
{
if ( !_cellProviders.Any() )
{
throw new ArgumentException( "No cell providers are enabled" );
}

foreach ( var dir in _reposDirectoryUtility.GetRepoDirectories() ) yield return GetRowsAsync( dir );
foreach ( var dir in _reposDirectoryUtility.GetRepoDirectories() )
{
var row = ( await GetRowAsync( dir ) ).ToList();
if ( _tableOptions.OnlyShowChanged == false || row.Any( r => r.IsChanged ) )
{
yield return row.Select( r => r.Content );
}
}
}

private async IAsyncEnumerable<string> GetRowsAsync( string path )
private async Task<IEnumerable<Cell>> GetRowAsync( string path )
{
foreach ( var provider in _cellProviders )
{
var cell = await provider.GetCell( path );
yield return cell.Content;
}
var cellTasks = _cellProviders.Select( p => p.GetCell( path ) );
return await Task.WhenAll( cellTasks );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ namespace RepoStatusTable.Options.CellProvider;
[SuppressMessage( "ReSharper", "AutoPropertyCanBeMadeGetOnly.Global" )]
public class GitBranchProviderOptions : ICellProviderOptions
{
/// <summary>
/// List of branches that are considered as not changed
/// </summary>
public IEnumerable<string> DefaultBranches { get; set; } = new List<string>();

/// <inheritdoc />
public bool Enable { get; set; } = true;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace RepoStatusTable.Options;

public class TableViewOptions
public class TableOptions
{
public string RenderMethod { get; set; } = "Spectre Table";

public bool OnlyShowChanged { get; set; } = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private async Task RenderRows( Table table )
{
await foreach ( var row in _tableModel.GetTableAsync() )
{
var tableRow = await row.ToArrayAsync();
var tableRow = row.ToArray();
table.AddRow( tableRow );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private async Task RenderRows( Table table, LiveDisplayContext ctx )
{
await foreach ( var row in _tableModel.GetTableAsync() )
{
var tableRow = await row.ToArrayAsync();
var tableRow = row.ToArray();
table.AddRow( tableRow );
ctx.Refresh();
}
Expand Down
4 changes: 2 additions & 2 deletions src/RepoStatusTable/View/TableViewProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ namespace RepoStatusTable.View;

public class TableViewProxy : ITableView
{
private readonly TableViewOptions _options;
private readonly TableOptions _options;
private readonly IEnumerable<ITableViewStrategy> _tableViewStrategies;

public TableViewProxy( IOptions<TableViewOptions> options, IEnumerable<ITableViewStrategy> tableViewStrategies )
public TableViewProxy( IOptions<TableOptions> options, IEnumerable<ITableViewStrategy> tableViewStrategies )
{
_tableViewStrategies = tableViewStrategies;
_options = options.Value;
Expand Down
7 changes: 6 additions & 1 deletion src/RepoStatusTable/exampleconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"Table": {
"RenderMethod": "Spectre Table Live",
"OnlyShowChanged": false,
"SpectreConsoleTable": {
"Alignment": "Center",
"Border": "Horizontal",
Expand All @@ -41,7 +42,11 @@
},
"GitBranchProvider": {
"Position": 1,
"Enable": true
"Enable": true,
"DefaultBranches": [
"develop",
"master"
]
},
"FileContentProvider": {
"Position": 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private void AddOptionsSections( IConfiguration configurationRoot )
_collection.AddOptions<HeadlineOptions>()
.Bind( configurationRoot.GetSection( OptionsConstants.Headline ) )
.ValidateDataAnnotations();
_collection.AddOptions<TableViewOptions>()
_collection.AddOptions<TableOptions>()
.Bind( configurationRoot.GetSection( OptionsConstants.Table ) );

// Spectre Console
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task RenderAsync()
{
var headings = _tableModel.GetHeadings().ToList();
var enumTable = await _tableModel.GetTableAsync().ToListAsync();
var table = await Task.WhenAll( enumTable.Select( async r => await r.ToListAsync() ) );
var table = enumTable.Select( r => r.ToList() );

AssertHeadings( headings );
AssertTable( table );
Expand Down

0 comments on commit 83f49dd

Please sign in to comment.