umwi_0.1.0_34abe384/info/src/umwi-info.adb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
with AAA.Strings;  use AAA.Strings;
with AAA.Table_IO; use AAA.Table_IO;

with Ada.Strings.UTF_Encoding.Wide_Wide_Strings;
use  Ada.Strings.UTF_Encoding.Wide_Wide_Strings;
with Ada.Strings.Wide_Wide_Fixed; use Ada.Strings.Wide_Wide_Fixed;
with Ada.Text_IO;
with Ada.Wide_Wide_Text_IO;
use  Ada.Wide_Wide_Text_IO;

with AnsiAda; use AnsiAda;

procedure Umwi.Info is

   subtype WChr is Wide_Wide_Character;
   subtype WStr is Wide_Wide_String;

   function Hex (I : Integer) return String is
      package Int_IO is new Ada.Text_IO.Integer_IO (Integer);
      Buffer : String (1 .. 19) := (others => ' ');
   begin
      Int_IO.Put (Buffer, I, 16);
      return Trim (Buffer);
   end Hex;

   function EAW (S : WStr; I : Integer := 1) return String
   is (if I > S'Last
       then ""
       else East_Asian_Width'(Count (S (I)).Width)'Image
            & " " & EAW (S, I + 1));

   function Codes (S : WStr; I : Integer := 1) return String
   is (if I > S'Last
       then ""
       else Hex (WChr'Pos (S (I))) & " " & Codes (S, I + 1));

begin
   Put ("Enter text: ");
   declare
      Text : constant WStr := Get_Line;
      Line : constant WStr := (Count (Text).Width + 4) * "-";
      Tab  : Table;
   begin
      Put_Line (Line);
      Put_Line ("- " & Text & " -");
      Put_Line (Line);

      Tab.Append ("Codes:").Append (Codes (Text, Text'First)).Append ("-")
        .New_Row;
      Tab.Append ("Code point count:").Append (Text'Length'Image).Append ("-")
        .New_Row;
      Tab.Append ("Width:").Append (Count (Text).Width'Image).Append ("-")
        .New_Row;
      Tab.Append ("East Asian width:").Append (EAW (Text, Text'First))
        .Append ("-").New_Row;
      Tab.Append ("Image:").Append (Encode (Text)).Append ("-")
        .New_Row;
      Tab.Append ("Color image:")
        .Append (Color_Wrap (Encode (Text), Foreground (Light_Red)))
        .Append ("-").New_Row;

      Tab.Print (Align => (1 => Ada.Strings.Right, 2 => <>));
   end;

end Umwi.Info;