Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Fix Notepad error in edit command
Browse files Browse the repository at this point in the history
  • Loading branch information
dotnetian committed Mar 13, 2023
1 parent 688523a commit 95045f2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Takeep.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@

var editContent = new Option<string> ("--text", "The text of the item.");
editContent.AddAlias ("-t");
editContent.Arity = ArgumentArity.ExactlyOne;
editContent.Arity = ArgumentArity.ZeroOrOne;

var editNotepad = new Option<bool> ("--open", "Opens the item's content in notepad")
{
Expand Down Expand Up @@ -212,7 +212,7 @@
{
try
{
TakeepXml.Edit (new Item { Name = name, Text = content }, notepad, keepsheet);
TakeepXml.Edit (name, content, notepad, keepsheet);
}
catch (Exception exception)
{
Expand Down
3 changes: 2 additions & 1 deletion Takeep.Cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"profiles": {
"Takeep.Cli": {
"commandName": "Project"
"commandName": "Project",
"commandLineArgs": "edit -n me -k keep -o"
}
}
}
44 changes: 29 additions & 15 deletions Takeep.Core/TakeepXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,28 +303,31 @@ public static void List (string name = "", string keepsheet = "")
#endregion
}

public static void Edit (Item item, bool notepad, string keepsheet)
public static void Edit (string name, string content, bool notepad, string keepsheet)
{
#region Check if item is null
#region Check if should be opened in notepad

if (!CheckNulls (item))
if (notepad)
{
return;
}
content = EditNotepad (name, keepsheet);

#endregion
if (string.IsNullOrWhiteSpace (content))
{
return;
}
}

#region Check if should be opened in notepad
#region Check if item is null

if (notepad)
if (!CheckNulls (new Item { Name = name, Text = content}))
{
item.Text = EditNotepad (item.Name);

return;
}

#endregion

#endregion

#region Load Document

string defaultXml = CheckDirectory () + CheckKeepsheet (keepsheet);
Expand All @@ -340,9 +343,9 @@ public static void Edit (Item item, bool notepad, string keepsheet)

foreach (XmlNode node in nodes)
{
if (node["Name"].InnerText == item.Name)
if (node["Name"].InnerText == name)
{
node["Text"].InnerText = item.Text;
node["Text"].InnerText = content;

document.Save (defaultXml);

Expand All @@ -355,7 +358,7 @@ public static void Edit (Item item, bool notepad, string keepsheet)
}

Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine ($"The procces was aborted: No items found with name {item.Name}");
Console.WriteLine ($"The procces was aborted: No items found with name {name}");
Console.ForegroundColor = ConsoleColor.White;

#endregion
Expand Down Expand Up @@ -576,7 +579,7 @@ private static void ViewNotepad (string name)
p.Kill ();
}

private static string EditNotepad (string name)
private static string EditNotepad (string name, string keepsheet)
{
string directory = CheckDirectory ();

Expand All @@ -585,7 +588,18 @@ private static string EditNotepad (string name)

#region Get Text
File.WriteAllText (filePath, $"/# This is the content of item \"{name}\":{Environment.NewLine}");
File.AppendAllText (filePath, GetItem (name).Text);

Item? item = GetItem (name, keepsheet);

if (item == null)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine ($"The procces was aborted: No items found with name {name}");
Console.ForegroundColor = ConsoleColor.White;
return null;
}

File.AppendAllText (filePath, item.Text);

FileInfo fileInfo = new (filePath);
File.SetAttributes (filePath, FileAttributes.Hidden);
Expand Down

0 comments on commit 95045f2

Please sign in to comment.