search by file & cat not echo | search by 's'

This commit is contained in:
Kacper Marzecki 2025-03-18 19:20:25 +01:00
parent e3d48a09b6
commit 96ba769603
2 changed files with 11 additions and 6 deletions

View File

@ -3,7 +3,7 @@ import React, { useState, useEffect } from "react";
import { render, Box, Text, useApp, useInput } from "ink"; import { render, Box, Text, useApp, useInput } from "ink";
import fs from "fs"; import fs from "fs";
import { execSync, spawn } from "child_process"; import { execSync, spawn } from "child_process";
import { loadTasks, saveTasks, commitAndPushTasks, pullTasks, addLog, readLog } from "./tasks.js"; import { loadTasks, saveTasks, commitAndPushTasks, pullTasks, addLog, readLog, saveQueryFile } from "./tasks.js";
import { Tabs, Tab } from 'ink-tab'; import { Tabs, Tab } from 'ink-tab';
import Scrollbar from "./scrollbar.js"; import Scrollbar from "./scrollbar.js";
@ -224,10 +224,11 @@ export default function TaskApp() {
} }
} }
if (input === "/") { if (input === "s") {
try { try {
const taskNames = tasks.map(taskContents).flat().map(fzfLine).join("\n"); const taskNames = tasks.map(taskContents).flat().map(fzfLine).join("\n");
const query = execSync(`echo "${taskNames}" | fzf --prompt 'Search: '`, { stdio: "pipe" }).toString().trim(); const queryFile = saveQueryFile(taskNames)
const query = execSync(`cat ${queryFile} | fzf --prompt 'Search: ' --preview "echo {} | sed 's/|/\\n/g'"`, { stdio: "pipe" }).toString().trim();
const res = findTaskPathWithContent(tasks, query) const res = findTaskPathWithContent(tasks, query)
log("Path:", res) log("Path:", res)
if (res) { if (res) {
@ -311,11 +312,11 @@ export default function TaskApp() {
g/G - Go to top/bottom g/G - Go to top/bottom
h/l - Navigate parent/subtasks h/l - Navigate parent/subtasks
a - Add task a - Add task
s - Add subtask A - Add subtask
e - Edit task in Neovim e - Edit task in Neovim
s - Search
S - Sync (Push/Pull) S - Sync (Push/Pull)
space - Toggle completion space - Toggle completion
/ - Search
? - Show help ? - Show help
========================== ==========================
` }) ` })

View File

@ -5,11 +5,11 @@ if (!process.env.TD_TASK_DIR) {
throw new Error("Environment variable TD_TASK_DIR is not defined"); throw new Error("Environment variable TD_TASK_DIR is not defined");
} }
const TASK_FILE = path.join(process.env.TD_TASK_DIR, "tasks.json"); const TASK_FILE = path.join(process.env.TD_TASK_DIR, "tasks.json");
const QUERY_FILE = path.join(process.env.TD_TASK_DIR, "query");
const LOG_FILE = path.join(process.env.TD_TASK_DIR, "log"); const LOG_FILE = path.join(process.env.TD_TASK_DIR, "log");
if (fs.existsSync(LOG_FILE)) { if (fs.existsSync(LOG_FILE)) {
fs.rmSync(LOG_FILE) fs.rmSync(LOG_FILE)
} }
const ensureIds = (tasks) => { const ensureIds = (tasks) => {
return tasks.map(task => { return tasks.map(task => {
if (!task.id) { if (!task.id) {
@ -30,6 +30,10 @@ export function loadTasks() {
} }
}; };
export function saveQueryFile(content) {
fs.writeFileSync(QUERY_FILE, content);
return QUERY_FILE;
};
export function saveTasks(tasks) { export function saveTasks(tasks) {
const updatedTasks = ensureIds(tasks); const updatedTasks = ensureIds(tasks);
fs.writeFileSync(TASK_FILE, JSON.stringify(updatedTasks, null, 2)); fs.writeFileSync(TASK_FILE, JSON.stringify(updatedTasks, null, 2));