分页

internal class Program
{
    static void Main(string[] args)
    {
        var sTotal = 1;
        var total = 100;

        for (var t = sTotal; t <= total; t++)
        {
            for (var i = 1; i <= t; i++)
            {
                var page = new Page(i, t);
                Console.WriteLine(page.ToString());
                //Thread.Sleep(300);
            }
        }

        var page1 = new Page(8, 8);
        Console.WriteLine(page1.ToString());

        Console.ReadKey();
    }
}

/// <summary>
/// 1. 想象成纸片通过两头开放的卡盒
/// 2. 首先纸片与卡盒的头部对齐
/// 3. 翻页时,当前页未达到卡盒中间或尾部已处于卡盒内时纸片不做移动。相反,则需要移动纸片。
/// 4. 这样卡片处于卡盒内的收尾部分,分别代表_startPageNo, _endPageNo
/// 5. 根据以上思想实现如下代码
/// </summary>
public class Page
{
    private const int PageItemCount = 7;
    public int CurrentPageNo { get; set; }
    public int TotalPages { get; set; }
    private int _startPageNo { get; set; }
    private int _endPageNo { get; set; }
    private List<string> _items { get; set; } = new List<string>();

    public Page(int currentPageNo, int totalPages)
    {
        CurrentPageNo = currentPageNo;
        TotalPages = totalPages;
        GenerateItems();
    }

    private void GenerateItems()
    {
        if (TotalPages == 0) return;

        if (TotalPages <= PageItemCount)
        {
            // 一次性完全装下纸片的情形
            _startPageNo = 1;
            _endPageNo = TotalPages;
        }
        else
        {
            var middle = PageItemCount / 2 + 1;

            if (CurrentPageNo <= middle)
            {
                // Current未超过中部时,不移动卡片
                _startPageNo = 1;
                _endPageNo = PageItemCount;
            }
            else if (CurrentPageNo + middle - 1 >= TotalPages)
            {
                // 尾部卡盒内部时,不需要移动卡片
                _startPageNo = TotalPages - PageItemCount + 1;
                _endPageNo = TotalPages;
            }
            else
            {
                // 需要移动的情况
                _startPageNo = CurrentPageNo - middle + 1;
                _endPageNo = _startPageNo + PageItemCount - 1;
            }
        }

        GeneratePrev();
        GenerateFirstPage();
        GenerateMoreBefore();
        GeneratePageItems();
        GenerateMoreAfter();
        GenerateLastPage();
        GenerateNext();
    }

    private void GeneratePrev()
    {
        if (CurrentPageNo > 1) _items.Add("prev");
    }

    private void GenerateFirstPage()
    {
        if (_startPageNo > 1) _items.Add("1");
    }

    private void GenerateMoreBefore()
    {
        if (_startPageNo > 2) _items.Add("more");
    }

    private void GeneratePageItems()
    {
        for (var pageNo = _startPageNo; pageNo <= _endPageNo; pageNo++)
        {
            _items.Add(pageNo.ToString());
        }
    }

    private void GenerateMoreAfter()
    {
        if (TotalPages - _endPageNo >= 2) _items.Add("more");
    }

    private void GenerateLastPage()
    {
        if (_endPageNo + 1 <= TotalPages) _items.Add(TotalPages.ToString());
    }

    private void GenerateNext()
    {
        if (CurrentPageNo < this.TotalPages) _items.Add("next");
    }

    public override string ToString()
    {
        var sb = new StringBuilder();
        foreach (var item in _items)
        {
            if (item == CurrentPageNo.ToString())
            {
                sb.Append($"\u001b[31m{item}\u001b[0m ");
            }
            else
            {
                sb.Append(item + " ");
            }
        }

        return sb.ToString();
    }
}
posted @ 2024-10-25 15:34  pojianbing  阅读(4)  评论(0编辑  收藏  举报