demoansi.pp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. program demoansi;
  2. {$mode objfpc}
  3. {$h+}
  4. uses sysutils, fpansi;
  5. var
  6. ANI : Array[0..3] of string = ('|','/','-','\');
  7. var
  8. i : Integer;
  9. aText : TAnsi;
  10. aLine : AnsiString;
  11. procedure Pause;
  12. begin
  13. Writeln;
  14. Write('Press Enter to continue...');
  15. Readln;
  16. Writeln(TAnsi.EraseDisplay(edScreen));
  17. Writeln(TAnsi.CursorAt(1,1));
  18. end;
  19. begin
  20. Writeln(TAnsi.EraseDisplay(edScreen));
  21. Writeln('--- Basic & Existing Demo ---');
  22. aText:='Hello world!';
  23. aLine:=aText.Bold.Fg(TAnsi.Red);
  24. Writeln(TAnsi.CursorAt(1,1),aLine);
  25. aText:='Hello world, again!';
  26. aText.Bold.Fg(TAnsi.BrightGreen).At(2,1).EmitLn;
  27. Write('Running animation... ');
  28. for I:=1 to 100 do
  29. begin
  30. aText:=Ani[i mod 4]+' '+Format('%.2d',[(100-I)]);
  31. AText.Backward(4).FG(TAnsi.BrightRed).Emit;
  32. Sleep(20);
  33. end;
  34. Pause;
  35. // --- Attributes Demo ---
  36. Writeln('--- Attributes Demo ---');
  37. aText := 'This text is BLINKING (may not work in all terminals)';
  38. aText.Blinking.EmitLn;
  39. aText := 'This text is FAINT';
  40. aText.Faint.EmitLn;
  41. aText := 'This text is STRIKETHROUGH';
  42. aText.Strikethrough.EmitLn;
  43. aText := 'This text is BOLD and FAINT combined';
  44. aText.Bold.Faint.EmitLn;
  45. Pause;
  46. // --- Colors Demo ---
  47. Writeln('--- Colors Demo ---');
  48. aText := 'Standard Blue Background with White Text';
  49. aText.Bg(TAnsi.Blue).Fg(TAnsi.White).EmitLn;
  50. aText := 'Custom RGB Foreground (Orange: 255, 165, 0)';
  51. aText.FgRGB(255, 165, 0).EmitLn;
  52. aText := 'Custom RGB Background (Purple: 128, 0, 128)';
  53. aText.BgRGB(128, 0, 128).Fg(TAnsi.White).EmitLn;
  54. Writeln('Grayscale Ramp (Bg):');
  55. for i := 0 to 23 do
  56. begin
  57. aText := ' ';
  58. aText.Bg(aText.GrayScale(i)).Emit;
  59. end;
  60. Writeln;
  61. Writeln('RGB Helper Demo (Red Gradient Bg):');
  62. for i := 0 to 5 do
  63. begin
  64. aText := ' ';
  65. aText.Bg(aText.RGB(i, 0, 0)).Emit;
  66. end;
  67. Writeln;
  68. Pause;
  69. // --- Cursor Movement Demo ---
  70. Writeln('--- Cursor Movement Demo ---');
  71. Writeln('Line 1: Origin');
  72. Writeln('Line 2: Target for Up/Down');
  73. Writeln('Line 3: Target for PreviousLine');
  74. Writeln;
  75. // Go back up to Line 1
  76. aText := ' <--- Appended on Line 1 via PreviousLine';
  77. aText.PreviousLine(4).Forward(15).Emit;
  78. // Go down to Line 2
  79. aText := ' <--- Appended on Line 2 via NextLine';
  80. aText.NextLine.Forward(15).Emit;
  81. // Move absolute
  82. aText := 'Absolute Position (Row 10, Col 20)';
  83. aText.At(20, 10).Emit;
  84. // Column movement
  85. Writeln;
  86. Writeln; // Ensure we are below
  87. Write('Column 1');
  88. aText := 'Column 30 via AtCol';
  89. aText.AtCol(30).EmitLn;
  90. // Directional
  91. Writeln;
  92. Write('Start');
  93. aText := 'Up and Right';
  94. // Move Up 1 and Right 5 from current position
  95. aText.Up(1).Forward(5).EmitLn;
  96. Writeln;
  97. Pause;
  98. // --- Erase Demo ---
  99. Writeln('--- Erase Demo ---');
  100. Writeln('1. This line will be partially erased from the END (Watch this part -> XXXXX)');
  101. Writeln('2. This line will be partially erased from the BEGINNING');
  102. Writeln('3. This line will be FULLY erased');
  103. Writeln('4. This line stays.');
  104. // 1. Erase End
  105. // Move up 4 lines (to line 1), move to column 55 approx
  106. Write(TAnsi.CursorPreviousLine(4));
  107. Write(TAnsi.CursorAtCol(55));
  108. Write(TAnsi.EraseLine(elEndOfLine));
  109. // 2. Erase Start
  110. Write(TAnsi.CursorNextLine(1));
  111. Write(TAnsi.CursorAtCol(10)); // Move in a bit
  112. Write(TAnsi.EraseLine(edBeginOfLine));
  113. // 3. Erase Full
  114. Write(TAnsi.CursorNextLine(1));
  115. Write(TAnsi.EraseLine(edLine));
  116. // Return to bottom
  117. Write(TAnsi.CursorNextLine(2));
  118. Pause;
  119. Writeln('Demo Complete.');
  120. end.