Skip to content
This repository was archived by the owner on Sep 5, 2023. It is now read-only.

Commit 3cd1176

Browse files
committed
feat: Implement history for text input fields
Each major input field (type, scope, description, body, and footer) will now save history of previously input values. This works as soon as a field has been submitted (does not need to complete the commit message sequence). This allows for users to abort and recall recent work. Note that this does not work for fields that have been partially filled but an abort has been called before enter (or Esc+Enter) has been pressed. In future, will probably need to rework this into a custom history class. This way text can be partially submitted even if the field hasn't been completed yet. It could also wrap up behaviours such as checking if certain key config files or directories exist. Resolve #24
1 parent ecdc507 commit 3cd1176

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

gitcommit/gitcommit.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import subprocess
2727
import textwrap
2828
from prompt_toolkit import PromptSession, prompt, ANSI
29+
from prompt_toolkit.history import FileHistory
2930
from prompt_toolkit.application.current import get_app
3031
from prompt_toolkit.styles import Style
3132
from .ansi import ANSI as Ansi
@@ -40,6 +41,8 @@
4041
from .updater import check_for_update
4142
from .utils import capitaliseFirst
4243

44+
CONFIG_HOME_DIR = "~/.gitcommit/"
45+
4346
IS_BREAKING_CHANGE = None # default for global variable
4447
try:
4548
WINDOW_WIDTH, _ = os.get_terminal_size()
@@ -139,8 +142,12 @@ def add_type(commit_msg):
139142

140143
print()
141144
text = Ansi.b_green("Type: ")
145+
history_file_path = os.path.join(CONFIG_HOME_DIR, "type_history")
142146
c_type = prompt(
143-
ANSI(text), completer=TypeCompleter(), validator=TypeValidator(valid_inputs)
147+
ANSI(text),
148+
completer=TypeCompleter(),
149+
validator=TypeValidator(valid_inputs),
150+
history=FileHistory(history_file_path),
144151
)
145152

146153
# Convert from number back to proper type name
@@ -158,7 +165,8 @@ def add_scope(commit_msg):
158165
)
159166
)
160167
text = Ansi.colour(Ansi.fg.bright_green, "Scope (optional): ")
161-
c_scope = prompt(ANSI(text)).strip()
168+
history_file_path = os.path.join(CONFIG_HOME_DIR, "scope_history")
169+
c_scope = prompt(ANSI(text), history=FileHistory(history_file_path)).strip()
162170

163171
if c_scope != "":
164172
commit_msg += "({})".format(c_scope)
@@ -207,7 +215,10 @@ def add_description(commit_msg):
207215
)
208216

209217
c_descr = ""
210-
session = PromptSession()
218+
219+
history_file_path = os.path.join(CONFIG_HOME_DIR, "description_history")
220+
session = PromptSession(history=FileHistory(history_file_path))
221+
211222
length_prompt = LineLengthPrompt(num_chars_remaining, session)
212223
while c_descr == "":
213224
text = Ansi.b_green("Description: ")
@@ -237,7 +248,11 @@ def add_body(commit_msg):
237248
if IS_BREAKING_CHANGE is None:
238249
raise ValueError("Global variable `IS_BREAKING_CHANGE` has not been set.")
239250

240-
session = PromptSession(prompt_continuation=custom_prompt_continuation)
251+
history_file_path = os.path.join(CONFIG_HOME_DIR, "body_history")
252+
session = PromptSession(
253+
prompt_continuation=custom_prompt_continuation,
254+
history=FileHistory(history_file_path),
255+
)
241256
body_validator = BodyValidator(session, IS_BREAKING_CHANGE)
242257

243258
if IS_BREAKING_CHANGE:
@@ -314,10 +329,12 @@ def add_footer(commit_msg):
314329
)
315330

316331
text = Ansi.colour(Ansi.fg.bright_green, "Footer (optional) ┃ ")
332+
history_file_path = os.path.join(CONFIG_HOME_DIR, "footer_history")
317333
session = PromptSession(
318334
completer=FooterCompleter(),
319335
multiline=False,
320336
prompt_continuation=custom_prompt_continuation,
337+
history=FileHistory(history_file_path),
321338
)
322339
c_footer = session.prompt(ANSI(text), validator=FooterValidator(session)).strip()
323340

@@ -346,12 +363,20 @@ def add_footer(commit_msg):
346363

347364
def run():
348365
# print(sys.version + "/n")
366+
# Ensure the config directory exists
367+
os.makedirs(CONFIG_HOME_DIR, exist_ok=True)
368+
349369
if WINDOW_WIDTH < 80:
350370
Ansi.print_error(
351371
f"It is recommended you increase your window width ({WINDOW_WIDTH}) to at least 80."
352372
)
353373

354-
Ansi.print_ok("Starting a conventional git commit...")
374+
print("Starting a conventional git commit...")
375+
print(
376+
Ansi.colour(
377+
Ansi.fg.bright_red, "Tip: Press the up arrow key to recall history!"
378+
)
379+
)
355380

356381
commit_msg = ""
357382
commit_msg = add_type(commit_msg)

0 commit comments

Comments
 (0)