De-snaggle some of the snaggle-toothed save routine nonsense

This commit is contained in:
Craig Everett 2026-02-13 15:07:22 +09:00
parent 3a570f000e
commit fcf44f55a1

View File

@ -945,15 +945,14 @@ open_hash3(State, Address, Source) ->
end.
% TODO: Break this down -- tons of things in here recur.
save(State = #s{frame = Frame, j = J, prefs = Prefs, code = {Codebook, Pages}}) ->
save(State = #s{prefs = Prefs, code = {Codebook, Pages}}) ->
case wxNotebook:getSelection(Codebook) of
?wxNOT_FOUND ->
State;
Index ->
case lists:nth(Index + 1, Pages) of
#p{path = {file, Path}, code = Widget} ->
Source = wxTextCtrl:getValue(Widget),
Source = wxStyledTextCtrl:getText(Widget),
case filelib:ensure_dir(Path) of
ok ->
case file:write_file(Path, Source) of
@ -968,7 +967,7 @@ save(State = #s{frame = Frame, j = J, prefs = Prefs, code = {Codebook, Pages}})
State
end;
Page = #p{path = {hash, Hash}, code = Widget} ->
DefaultDir =
DefDir =
case maps:find(dir, Prefs) of
{ok, PrefDir} ->
PrefDir;
@ -978,117 +977,78 @@ save(State = #s{frame = Frame, j = J, prefs = Prefs, code = {Codebook, Pages}})
D -> filename:basename(D)
end
end,
Options =
[{message, J("Save Location")},
{defaultDir, DefaultDir},
{defaultFile, unicode:characters_to_list([Hash, ".aes"])},
{wildCard, "*.aes"},
{style, ?wxFD_SAVE bor ?wxFD_OVERWRITE_PROMPT}],
Dialog = wxFileDialog:new(Frame, Options),
NewState =
case wxFileDialog:showModal(Dialog) of
?wxID_OK ->
Dir = wxFileDialog:getDirectory(Dialog),
case wxFileDialog:getFilename(Dialog) of
"" ->
State;
Name ->
File =
case filename:extension(Name) of
".aes" -> Name;
_ -> Name ++ ".aes"
end,
Path = filename:join(Dir, File),
Source = gd_sophia_editor:get_text(Widget),
case filelib:ensure_dir(Path) of
ok ->
case file:write_file(Path, Source) of
ok ->
true = wxNotebook:setPageText(Codebook, Index, File),
NewPrefs = maps:put(dir, Dir, Prefs),
NewPage = Page#p{path = {file, Path}},
NewPages = store_nth(Index + 1, NewPage, Pages),
NewCode = {Codebook, NewPages},
State#s{prefs = NewPrefs, code = NewCode};
Error ->
ok = handle_troubling(State, Error),
State
end;
Error ->
ok = handle_troubling(State, Error),
State
end
end;
?wxID_CANCEL ->
State
end,
ok = wxFileDialog:destroy(Dialog),
NewState
DefName = unicode:characters_to_list([Hash, ".aes"]),
save_dialog(State, DefDir, DefName, Widget, Index, Page)
end
end.
% TODO: Break this down -- tons of things in here recur.
rename(State = #s{frame = Frame, j = J, prefs = Prefs, code = {Codebook, Pages}}) ->
rename(State = #s{code = {Codebook, Pages}}) ->
case wxNotebook:getSelection(Codebook) of
?wxNOT_FOUND ->
State;
Index ->
case lists:nth(Index + 1, Pages) of
Page = #p{path = {file, Path}, code = Widget} ->
DefaultDir = filename:dirname(Path),
Options =
[{message, J("Save Location")},
{defaultDir, DefaultDir},
{defaultFile, filename:basename(Path)},
{wildCard, "*.aes"},
{style, ?wxFD_SAVE bor ?wxFD_OVERWRITE_PROMPT}],
Dialog = wxFileDialog:new(Frame, Options),
NewState =
case wxFileDialog:showModal(Dialog) of
?wxID_OK ->
Dir = wxFileDialog:getDirectory(Dialog),
case wxFileDialog:getFilename(Dialog) of
"" ->
State;
Name ->
File =
case filename:extension(Name) of
".aes" -> Name;
_ -> Name ++ ".aes"
end,
NewPath = filename:join(Dir, File),
Source = gd_sophia_editor:get_text(Widget),
case filelib:ensure_dir(NewPath) of
ok ->
case file:write_file(NewPath, Source) of
ok ->
true = wxNotebook:setPageText(Codebook, Index, File),
NewPrefs = maps:put(dir, Dir, Prefs),
NewPage = Page#p{path = {file, NewPath}},
NewPages = store_nth(Index + 1, NewPage, Pages),
NewCode = {Codebook, NewPages},
State#s{prefs = NewPrefs, code = NewCode};
Error ->
ok = handle_troubling(State, Error),
State
end;
Error ->
ok = handle_troubling(State, Error),
State
end
end;
?wxID_CANCEL ->
State
end,
ok = wxFileDialog:destroy(Dialog),
NewState;
DefDir = filename:dirname(Path),
DefName = filename:basename(Path),
save_dialog(State, DefDir, DefName, Widget, Index, Page);
#p{path = {hash, _}} ->
save(State)
end
end.
save_dialog(State = #s{frame = Frame, j = J, prefs = Prefs, code = {Codebook, Pages}},
DefDir, DefName, Widget, Index, Page) ->
Options =
[{message, J("Save Location")},
{defaultDir, DefDir},
{defaultFile, DefName},
{wildCard, "*.aes"},
{style, ?wxFD_SAVE bor ?wxFD_OVERWRITE_PROMPT}],
Dialog = wxFileDialog:new(Frame, Options),
NewState =
case wxFileDialog:showModal(Dialog) of
?wxID_OK ->
Dir = wxFileDialog:getDirectory(Dialog),
case wxFileDialog:getFilename(Dialog) of
"" ->
State;
Name ->
File =
case filename:extension(Name) of
".aes" -> Name;
_ -> Name ++ ".aes"
end,
NewPath = filename:join(Dir, File),
Source = gd_sophia_editor:get_text(Widget),
case filelib:ensure_dir(NewPath) of
ok ->
case file:write_file(NewPath, Source) of
ok ->
true = wxNotebook:setPageText(Codebook, Index, File),
NewPrefs = maps:put(dir, Dir, Prefs),
NewPage = Page#p{path = {file, NewPath}},
NewPages = store_nth(Index + 1, NewPage, Pages),
NewCode = {Codebook, NewPages},
State#s{prefs = NewPrefs, code = NewCode};
Error ->
ok = handle_troubling(State, Error),
State
end;
Error ->
ok = handle_troubling(State, Error),
State
end
end;
?wxID_CANCEL ->
State
end,
ok = wxFileDialog:destroy(Dialog),
NewState.
close_source(State = #s{code = {Codebook, Pages}}) ->
case wxNotebook:getSelection(Codebook) of
?wxNOT_FOUND ->