trendy_terminal_0.0.5_f56da7f1/src/common/trendy_terminal-maps.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
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
-------------------------------------------------------------------------------
-- Copyright 2021, The Trendy Terminal Developers (see AUTHORS file)

-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at

--     http://www.apache.org/licenses/LICENSE-2.0

-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
-------------------------------------------------------------------------------

package body Trendy_Terminal.Maps is
    use all type ASU.Unbounded_String;
    function "+"(S : String) return ASU.Unbounded_String renames ASU.To_Unbounded_String;

    package Key_Maps is new Ada.Containers.Ordered_Maps (
        Key_Type     => ASU.Unbounded_String,
        Element_Type => Key,
        "<"          => ASU."<",
        "="          => "=");

    package Inverse_Key_Maps is new Ada.Containers.Ordered_Maps (
        Key_Type     => Key,
        Element_Type => ASU.Unbounded_String,
        "<"          => "<",
        "="          => ASU."=");

    function Make_Key_Map return Key_Maps.Map is
        KM : Key_Maps.Map;
    begin
        KM.Insert(CSI & (+"A"), Key_Up);
        KM.Insert(CSI & (+"B"), Key_Down);
        KM.Insert(CSI & (+"C"), Key_Right);
        KM.Insert(CSI & (+"D"), Key_Left);
        KM.Insert(CSI & (+"H"), Key_Home);
        KM.Insert(CSI & (+"F"), Key_End);

        KM.Insert(CSI & (+"1;5A"), Key_Ctrl_Up);
        KM.Insert(CSI & (+"1;5B"), Key_Ctrl_Down);
        KM.Insert(CSI & (+"1;5C"), Key_Ctrl_Right);
        KM.Insert(CSI & (+"1;5D"), Key_Ctrl_Left);

        KM.Insert(Characters.DEL & ASU.Null_Unbounded_String, Key_Backspace);
        KM.Insert(Characters.SUB & ASU.Null_Unbounded_String, Key_Pause);

        KM.Insert(CSI & (+"2~"), Key_Insert);
        KM.Insert(CSI & (+"3~"), Key_Delete);
        KM.Insert(CSI & (+"5~"), Key_Page_Up);
        KM.Insert(CSI & (+"6~"), Key_Page_Down);

        KM.Insert(Characters.ESC & (+"OP"), Key_F1);
        KM.Insert(Characters.ESC & (+"OQ"), Key_F2);
        KM.Insert(Characters.ESC & (+"OR"), Key_F3);
        KM.Insert(Characters.ESC & (+"OS"), Key_F4);

        KM.Insert(CSI & (+"15~"), Key_F5);
        KM.Insert(CSI & (+"17~"), Key_F6);
        KM.Insert(CSI & (+"18~"), Key_F7);
        KM.Insert(CSI & (+"19~"), Key_F8);

        KM.Insert(CSI & (+"20~"), Key_F9);
        KM.Insert(CSI & (+"21~"), Key_F10);
        KM.Insert(CSI & (+"23~"), Key_F11);
        KM.Insert(CSI & (+"24~"), Key_F12);

        KM.Insert(Characters.HT & (+""), Key_Tab);
        KM.Insert(CSI & (+"Z"), Key_Shift_Tab);

        KM.Insert(Characters.ETX & ASU.Null_Unbounded_String, Key_Ctrl_C);
        KM.Insert(Characters.EOT & ASU.Null_Unbounded_String, Key_Ctrl_D);

        return KM;
    end Make_Key_Map;

    function Make_Key_Lookup_Map return Inverse_Key_Maps.Map is
        KM : Inverse_Key_Maps.Map;
    begin
        -- https://vt100.net/docs/vt510-rm/DECFNK.html
        --
        -- 0,1  none
        -- 2    Shift
        -- 3    Alt
        -- 4    Alt + Shift
        -- 5    Control
        -- 6    Control + Shift
        -- 7    Alt + Control
        -- 8    Alt + Control + Shift

        KM.Insert(Key_Up,    CSI & (+"A"));
        KM.Insert(Key_Down,  CSI & (+"B"));
        KM.Insert(Key_Right, CSI & (+"C"));
        KM.Insert(Key_Left,  CSI & (+"D"));
        KM.Insert(Key_Home,  CSI & (+"H"));
        KM.Insert(Key_End,   CSI & (+"F"));

        KM.Insert(Key_Ctrl_Up,    CSI & (+"1;5A"));
        KM.Insert(Key_Ctrl_Down,  CSI & (+"1;5B"));
        KM.Insert(Key_Ctrl_Right, CSI & (+"1;5C"));
        KM.Insert(Key_Ctrl_Left,  CSI & (+"1;5D"));

        KM.Insert(Key_Backspace, Characters.DEL & (+""));
        KM.Insert(Key_Pause,     Characters.SUB & (+""));

        KM.Insert(Key_Insert,    CSI & (+"2~"));
        KM.Insert(Key_Delete,    CSI & (+"3~"));
        KM.Insert(Key_Page_Up,   CSI & (+"5~"));
        KM.Insert(Key_Page_Down, CSI & (+"6~"));

        KM.Insert(Key_F1, Characters.ESC & (+"OP"));
        KM.Insert(Key_F2, Characters.ESC & (+"OQ"));
        KM.Insert(Key_F3, Characters.ESC & (+"OR"));
        KM.Insert(Key_F4, Characters.ESC & (+"OS"));

        KM.Insert(Key_F5, CSI & (+"15~"));
        KM.Insert(Key_F6, CSI & (+"17~"));
        KM.Insert(Key_F7, CSI & (+"18~"));
        KM.Insert(Key_F8, CSI & (+"19~"));

        KM.Insert(Key_F9,  CSI & (+"20~"));
        KM.Insert(Key_F10, CSI & (+"21~"));
        KM.Insert(Key_F11, CSI & (+"23~"));
        KM.Insert(Key_F12, CSI & (+"24~"));

        KM.Insert(Key_Tab, Characters.HT & (+""));
        KM.Insert(Key_Shift_Tab, CSI & (+"Z"));

        KM.Insert(Key_Ctrl_C, Characters.ETX & (+""));
        KM.Insert(Key_Ctrl_D, Characters.EOT & (+""));

        return KM;
    end Make_Key_Lookup_Map;

    KM : constant Key_Maps.Map := Make_Key_Map;
    MK : constant Inverse_Key_Maps.Map := Make_Key_Lookup_Map;

    function Sequence_For (K : Key) return String is (ASU.To_String (MK(K)));

    function Is_Key (Sequence : String) return Boolean is (KM.Contains (ASU.To_Unbounded_String (Sequence)));

    function Key_For (Sequence : String) return Key is (KM (ASU.To_Unbounded_String (Sequence)));

end Trendy_Terminal.Maps;