Add "IsFavorite" column to shortcuts table

Enhance the shortcuts management in `Obsidean.cs` by introducing an "IsFavorite" column. Implement directory creation for markdown files if they don't exist, and update data structures to support the new column. Improve table data handling by filtering out non-data lines and ensuring consistent column counts during operations.
This commit is contained in:
Miguel 2025-06-15 02:45:08 +02:00
parent 6d2d75a833
commit 092f321478
1 changed files with 52 additions and 14 deletions

View File

@ -94,7 +94,12 @@ namespace libObsidean
// Si el archivo no existe, devolver cabecera vacía
if (!File.Exists(pathToTabla))
{
string[,] vacio = new string[2, 3] { { "Application", "Shortcut", "Description" }, { "", "", "" } };
// Crear el directorio si hace falta
string? dir = Path.GetDirectoryName(pathToTabla);
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
Directory.CreateDirectory(dir);
string[,] vacio = new string[2, 4] { { "Application", "Shortcut", "Description", "IsFavorite" }, { "", "", "", "false" } };
SaveTableToMarkdown(pathToTabla, vacio);
return vacio;
}
@ -115,28 +120,31 @@ namespace libObsidean
{
if (string.Equals(tabla[i, 0], applicationName, StringComparison.OrdinalIgnoreCase))
{
filas.Add(new[] { tabla[i, 0], tabla[i, 1], tabla[i, 2] });
string isFavorite = tabla.GetLength(1) > 3 ? tabla[i, 3] : "false";
filas.Add(new[] { tabla[i, 0], tabla[i, 1], tabla[i, 2], isFavorite });
}
}
if (filas.Count == 0)
return new string[0, 0];
string[,] resultado = new string[filas.Count + 1, 3];
string[,] resultado = new string[filas.Count + 1, 4];
// Copiar cabeceras
resultado[0, 0] = "Application";
resultado[0, 1] = "Shortcut";
resultado[0, 2] = "Description";
resultado[0, 3] = "IsFavorite";
for (int i = 0; i < filas.Count; i++)
{
resultado[i + 1, 0] = filas[i][0];
resultado[i + 1, 1] = filas[i][1];
resultado[i + 1, 2] = filas[i][2];
resultado[i + 1, 3] = filas[i][3];
}
return resultado;
}
public void AgregarOSobrescribirShortcut(string applicationName, string shortcut, string description)
public void AgregarOSobrescribirShortcut(string applicationName, string shortcut, string description, bool isFavorite = false)
{
if (string.IsNullOrWhiteSpace(applicationName) || string.IsNullOrWhiteSpace(shortcut))
return;
@ -153,6 +161,8 @@ namespace libObsidean
{
// Sobrescribir descripción
tabla[i, 2] = description;
if (tabla.GetLength(1) > 3)
tabla[i, 3] = isFavorite.ToString().ToLower();
encontrado = true;
break;
}
@ -162,17 +172,24 @@ namespace libObsidean
{
// Añadir nueva fila
int filas = tabla.GetLength(0);
string[,] nuevaTabla = new string[filas + 1, 3];
int columnas = Math.Max(tabla.GetLength(1), 4); // Asegurar al menos 4 columnas
string[,] nuevaTabla = new string[filas + 1, columnas];
for (int i = 0; i < filas; i++)
{
for (int j = 0; j < 3; j++)
for (int j = 0; j < tabla.GetLength(1); j++)
{
nuevaTabla[i, j] = tabla[i, j];
}
// Rellenar columnas nuevas si es necesario
for (int j = tabla.GetLength(1); j < columnas; j++)
{
nuevaTabla[i, j] = j == 3 ? "false" : "";
}
}
nuevaTabla[filas, 0] = applicationName;
nuevaTabla[filas, 1] = shortcut;
nuevaTabla[filas, 2] = description;
nuevaTabla[filas, 3] = isFavorite.ToString().ToLower();
tabla = nuevaTabla;
}
@ -217,7 +234,7 @@ namespace libObsidean
{
string word = match.Groups[1].Value;
// Verificar si la palabra está en el conjunto de términos técnicos, ignorando mayúsculas y minúsculas
if (technicalTerms.Contains(word, StringComparer.OrdinalIgnoreCase))
if (technicalTerms.Contains(word))
{
return $"[[{word}]]"; // Encerrar la palabra en corchetes si es técnica
}
@ -295,18 +312,33 @@ namespace libObsidean
if (tableLines.Count == 0)
return new string[0, 0];
var header = tableLines[0].Split('|').Select(h => h.Trim()).Where(h => !string.IsNullOrEmpty(h)).ToArray();
// Filtrar líneas que no sean separadores (líneas con solo guiones y pipes)
var dataLines = tableLines.Where(line => !line.All(c => c == '|' || c == '-' || c == ' ')).ToList();
if (dataLines.Count == 0)
return new string[0, 0];
// La primera línea es el header
var header = dataLines[0].Split('|').Select(h => h.Trim()).Where(h => !string.IsNullOrEmpty(h)).ToArray();
int columns = header.Length;
int rows = tableLines.Count - 1;
int totalRows = dataLines.Count; // Incluye header + filas de datos
string[,] tableArray = new string[rows, columns];
string[,] tableArray = new string[totalRows, columns];
for (int i = 1; i < rows; i++)
// Procesar todas las líneas (header + datos)
for (int i = 0; i < totalRows; i++)
{
var row = tableLines[i+1].Split('|').ToArray();
for (int j = 0; j < columns; j++)
var rowParts = dataLines[i].Split('|').Select(p => p.Trim()).Where(p => !string.IsNullOrEmpty(p)).ToArray();
for (int j = 0; j < columns && j < rowParts.Length; j++)
{
tableArray[i - 1, j] = row[j+1].Trim();
tableArray[i, j] = rowParts[j];
}
// Rellenar columnas faltantes con string vacío
for (int j = rowParts.Length; j < columns; j++)
{
tableArray[i, j] = "";
}
}
@ -316,6 +348,12 @@ namespace libObsidean
public static void SaveTableToMarkdown(string filePath, string[,] tableArray)
{
var tableLines = ConvertArrayToTableLines(tableArray);
// Asegurarse de que el directorio exista
string? dir = Path.GetDirectoryName(filePath);
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
File.WriteAllLines(filePath, tableLines);
}