11from __future__ import annotations
22
33import logging
4+ import os .path
5+ import readline
46from cmd import Cmd
57from typing import TYPE_CHECKING
68
79import fuzzywuzzy .process
10+ from xdg_base_dirs import xdg_cache_home
811
912from fantasy_forge .armour import Armour
1013from fantasy_forge .character import Character
@@ -22,6 +25,7 @@ class Shell(Cmd):
2225 player : Player
2326 messages : Messages
2427 prompt = "> "
28+ histfile : str
2529
2630 def __new__ (
2731 cls , messages : Messages , player : Player , stdin = None , stdout = None
@@ -49,6 +53,19 @@ def __init__(self, messages: Messages, player: Player, stdin=None, stdout=None):
4953 self .player = player
5054 self .messages = messages
5155
56+ if not stdin and not stdout : # check if singleplayer
57+ self .histfile = xdg_cache_home () / "fantasy_forge.hist"
58+ if readline and os .path .exists (self .histfile ):
59+ readline .read_history_file (self .histfile )
60+ else :
61+ self .histfile = None
62+
63+ def postloop (self ):
64+ histfile_size = 1000
65+ if readline and self .histfile :
66+ readline .set_history_length (histfile_size )
67+ readline .write_history_file (self .histfile )
68+
5269 def completenames (self , text , * ignored ):
5370 """This is called when completing the command itself.
5471
@@ -332,8 +349,10 @@ def complete_unequip(
332349 endidx : int ,
333350 ) -> list [str ]:
334351 item = line .removeprefix ("unequip " ).strip ()
335- equipped_items = [self .player .main_hand ] + list (self .player .armour_slots .values ())
336-
352+ equipped_items = [self .player .main_hand ] + list (
353+ self .player .armour_slots .values ()
354+ )
355+
337356 completions = [
338357 text + entity .name .removeprefix (item ).strip () + " "
339358 for entity in equipped_items
@@ -359,7 +378,7 @@ def complete_drop(
359378 endidx : int ,
360379 ) -> list [str ]:
361380 item = line .removeprefix ("drop " ).strip ()
362-
381+
363382 completions = [
364383 text + entity .name .removeprefix (item ).strip () + " "
365384 for entity in self .player .inventory
0 commit comments