PHP 8.2.30
Preview: history.py Size: 3.95 KB
//opt/alt/python36/lib64/python3.6/idlelib/history.py

"Implement Idle Shell history mechanism with History class"

from idlelib.config import idleConf


class History:
    ''' Implement Idle Shell history mechanism.

    store - Store source statement (called from pyshell.resetoutput).
    fetch - Fetch stored statement matching prefix already entered.
    history_next - Bound to <<history-next>> event (default Alt-N).
    history_prev - Bound to <<history-prev>> event (default Alt-P).
    '''
    def __init__(self, text):
        '''Initialize data attributes and bind event methods.

        .text - Idle wrapper of tk Text widget, with .bell().
        .history - source statements, possibly with multiple lines.
        .prefix - source already entered at prompt; filters history list.
        .pointer - index into history.
        .cyclic - wrap around history list (or not).
        '''
        self.text = text
        self.history = []
        self.prefix = None
        self.pointer = None
        self.cyclic = idleConf.GetOption("main", "History", "cyclic", 1, "bool")
        text.bind("<<history-previous>>", self.history_prev)
        text.bind("<<history-next>>", self.history_next)

    def history_next(self, event):
        "Fetch later statement; start with ealiest if cyclic."
        self.fetch(reverse=False)
        return "break"

    def history_prev(self, event):
        "Fetch earlier statement; start with most recent."
        self.fetch(reverse=True)
        return "break"

    def fetch(self, reverse):
        '''Fetch statememt and replace current line in text widget.

        Set prefix and pointer as needed for successive fetches.
        Reset them to None, None when returning to the start line.
        Sound bell when return to start line or cannot leave a line
        because cyclic is False.
        '''
        nhist = len(self.history)
        pointer = self.pointer
        prefix = self.prefix
        if pointer is not None and prefix is not None:
            if self.text.compare("insert", "!=", "end-1c") or \
                    self.text.get("iomark", "end-1c") != self.history[pointer]:
                pointer = prefix = None
                self.text.mark_set("insert", "end-1c")  # != after cursor move
        if pointer is None or prefix is None:
            prefix = self.text.get("iomark", "end-1c")
            if reverse:
                pointer = nhist  # will be decremented
            else:
                if self.cyclic:
                    pointer = -1  # will be incremented
                else:  # abort history_next
                    self.text.bell()
                    return
        nprefix = len(prefix)
        while 1:
            pointer += -1 if reverse else 1
            if pointer < 0 or pointer >= nhist:
                self.text.bell()
                if not self.cyclic and pointer < 0:  # abort history_prev
                    return
                else:
                    if self.text.get("iomark", "end-1c") != prefix:
                        self.text.delete("iomark", "end-1c")
                        self.text.insert("iomark", prefix)
                    pointer = prefix = None
                break
            item = self.history[pointer]
            if item[:nprefix] == prefix and len(item) > nprefix:
                self.text.delete("iomark", "end-1c")
                self.text.insert("iomark", item)
                break
        self.text.see("insert")
        self.text.tag_remove("sel", "1.0", "end")
        self.pointer = pointer
        self.prefix = prefix

    def store(self, source):
        "Store Shell input statement into history list."
        source = source.strip()
        if len(source) > 2:
            # avoid duplicates
            try:
                self.history.remove(source)
            except ValueError:
                pass
            self.history.append(source)
        self.pointer = None
        self.prefix = None


if __name__ == "__main__":
    from unittest import main
    main('idlelib.idle_test.test_history', verbosity=2, exit=False)

Directory Contents

Dirs: 3 × Files: 74

Name Size Perms Modified Actions
Icons DIR
- drwxr-xr-x 2024-05-23 07:06:19
Edit Download
idle_test DIR
- drwxr-xr-x 2024-05-23 07:06:19
Edit Download
- drwxr-xr-x 2024-05-23 07:06:19
Edit Download
9.11 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
19.36 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
3.14 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
8.09 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
5.92 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
6.94 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
55.04 KB lrw-r--r-- 2021-09-04 03:49:41
Edit Download
10.24 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
11.01 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
2.21 KB lrw-r--r-- 2021-09-04 03:49:41
Edit Download
2.62 KB lrw-r--r-- 2021-09-04 03:49:41
Edit Download
10.52 KB lrw-r--r-- 2021-09-04 03:49:41
Edit Download
3.05 KB lrw-r--r-- 2021-09-04 03:49:41
Edit Download
37.97 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
98.69 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
13.09 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
1.82 KB lrw-r--r-- 2021-09-04 03:49:41
Edit Download
18.65 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
11.86 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
3.96 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
1.06 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
1.02 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
1.97 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
65.70 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
3.56 KB lrw-r--r-- 2021-09-04 03:49:41
Edit Download
3.80 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
6.58 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
53.82 KB lrw-r--r-- 2021-09-04 03:49:41
Edit Download
11.06 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
8.77 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
3.95 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
10.07 KB lrw-r--r-- 2021-09-04 03:49:41
Edit Download
12.58 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
454 B lrw-r--r-- 2024-04-17 17:19:35
Edit Download
570 B lrw-r--r-- 2021-09-04 03:49:41
Edit Download
20.25 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
9.43 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
3.62 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
18.21 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
38.91 KB lrw-r--r-- 2021-09-04 03:49:41
Edit Download
26.54 KB lrw-r--r-- 2021-09-04 03:49:41
Edit Download
5.67 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
7.00 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
7.04 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
3.12 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
3.06 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
19.65 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
56.38 KB lrwxr-xr-x 2024-04-17 17:19:35
Edit Download
12.14 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
9.37 KB lrw-r--r-- 2021-09-04 03:49:41
Edit Download
6.71 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
7.33 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
20.64 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
868 B lrw-r--r-- 2024-04-17 17:19:35
Edit Download
16.87 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
7.66 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
4.35 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
3.09 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
7.28 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
7.30 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
13.00 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
4.35 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
1.41 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
5.98 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
8.28 KB lrw-r--r-- 2021-09-04 03:49:41
Edit Download
6.33 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
14.74 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
10.79 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
2.53 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
1.31 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
961 B lrw-r--r-- 2024-04-17 17:19:35
Edit Download
14.84 KB lrw-r--r-- 2024-04-17 17:19:35
Edit Download
396 B lrw-r--r-- 2024-04-17 17:19:35
Edit Download
159 B lrw-r--r-- 2024-04-17 17:19:35
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).