-
Notifications
You must be signed in to change notification settings - Fork 0
history command: first draft #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
90f6fd0
feat: draft of a history command
mari4kaa a0a493d
chore: add lost dependencies
mari4kaa 6c95c60
chore(WIP): debuggin command
mari4kaa a75d34e
feat(mvp): make history command mvp
mari4kaa 9dee154
feat: final list display
mari4kaa 509ef93
chore: remove unnecessary deps
mari4kaa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,10 @@ | ||
| import Main from "./modules/Main"; | ||
| import { HistoryCommand } from "./modules/commands/HistoryCommand"; | ||
| import { MainCopyCommand } from "./modules/commands/MainCopyCommand"; | ||
|
|
||
| const commands = [new MainCopyCommand()]; | ||
| const historyCommand = new HistoryCommand(); | ||
| const mainCopyCommand = new MainCopyCommand(); | ||
|
|
||
| const argParser = new Main(commands); | ||
| const commands = [mainCopyCommand, historyCommand]; | ||
|
|
||
| new Main(commands); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| import readline from "readline"; | ||
| import chalk from "chalk"; | ||
| import { HistoryRecord, TCustomEvents } from "../../../types"; | ||
| import events from "../../events"; | ||
| import HistoryRepository from "./historyRepository"; | ||
|
|
||
| class HistoryCommand { | ||
| private records: HistoryRecord[] = []; | ||
| private currentIndex = 0; | ||
| private isDetailedView = false; | ||
| private selectedIndex = -1; | ||
| private terminalHeight = 0; | ||
|
|
||
| constructor() { | ||
| this.listenForNewHistoryItem(); | ||
| } | ||
|
|
||
| async execute() { | ||
| this.records = await HistoryRepository.getInstance().getAllRecords(); | ||
| if (this.records.length === 0) { | ||
| console.log("🙅 Copying history empty"); | ||
| return; | ||
| } | ||
| this.currentIndex = this.records.length - 1; | ||
| this.displayList(); | ||
| this.listenForNavigation(); | ||
| } | ||
|
|
||
| private displayList() { | ||
| // Get terminal height and reserve space for header and footer | ||
| this.terminalHeight = process.stdout.rows - 9; | ||
|
|
||
| console.clear(); | ||
| console.log("📋 Copy History"); | ||
|
|
||
| // Calculate visible range | ||
| let startIndex = Math.max( | ||
| 0, | ||
| this.currentIndex - Math.ceil(this.terminalHeight / 2), | ||
| ); | ||
| let endIndex = Math.min( | ||
| this.records.length, | ||
| startIndex + this.terminalHeight, | ||
| ); | ||
|
|
||
| // If we're near the start, show from the beginning | ||
| if (this.currentIndex < Math.ceil(this.terminalHeight / 2)) { | ||
| startIndex = 0; | ||
| endIndex = Math.min(this.records.length, this.terminalHeight); | ||
| } | ||
|
|
||
| // Show scroll indicators if there are more items | ||
| if (startIndex > 0) { | ||
| console.log( | ||
| chalk.bgBlue.white(` ↑ More items above (${startIndex} items) `), | ||
| ); | ||
| } else { | ||
| console.log(""); | ||
| } | ||
|
|
||
| // Display visible portion of the list | ||
| for (let i = startIndex; i < endIndex; i++) { | ||
| const record = this.records[i]; | ||
| const isSelected = i === this.currentIndex; | ||
| const isHighlighted = i === this.selectedIndex; | ||
|
|
||
| const line = `Copy #${i + 1} - ${record.source} → ${record.destination}`; | ||
|
|
||
| if (isSelected) { | ||
| console.log(chalk.bgYellow.black(line)); | ||
| } else if (isHighlighted) { | ||
| console.log(chalk.yellow(line)); | ||
| } else { | ||
| console.log(line); | ||
| } | ||
| } | ||
| if (endIndex < this.records.length) { | ||
| const remainingItems = this.records.length - endIndex; | ||
| console.log( | ||
| chalk.bgBlue.white(` ↓ More items below (${remainingItems} items) `), | ||
| ); | ||
| } else { | ||
| console.log(""); | ||
| } | ||
|
|
||
| console.log("\nNavigation:"); | ||
| console.log("j/k - Move up/down"); | ||
| console.log("Enter - Toggle detailed view"); | ||
| console.log("q - Back/Exit"); | ||
| } | ||
|
|
||
| private displayDetailedView() { | ||
| const record = this.records[this.selectedIndex]; | ||
| console.clear(); | ||
| console.log("📄 Copy Details"); | ||
| console.log("───────────────"); | ||
| console.log(`Copy #${this.selectedIndex + 1} of ${this.records.length}`); | ||
| console.log(`From: ${record.source}`); | ||
| console.log(`To: ${record.destination}`); | ||
| console.log(`When: ${record.timestamp}`); | ||
| console.log(`Size: ${record.size} bytes`); | ||
| console.log("\nPress Enter to return to list"); | ||
| console.log("Press q to exit"); | ||
| } | ||
|
|
||
| private listenForNavigation() { | ||
| readline.emitKeypressEvents(process.stdin); | ||
| if (process.stdin.isTTY) process.stdin.setRawMode(true); | ||
|
|
||
| const onKeyPress = (_: string, key: readline.Key) => { | ||
| if (key.ctrl && key.name === "c") return this.exit(); | ||
|
|
||
| switch (key.name) { | ||
| case "j": | ||
| this.navigateDown(); | ||
| break; | ||
| case "k": | ||
| this.navigateUp(); | ||
| break; | ||
| case "return": | ||
| case "enter": | ||
| this.toggleView(); | ||
| break; | ||
| case "q": | ||
| this.quitOrBack(); | ||
| break; | ||
| } | ||
| }; | ||
|
|
||
| process.stdin.on("keypress", onKeyPress); | ||
| } | ||
|
|
||
| private navigateDown() { | ||
| if (this.currentIndex < this.records.length - 1) { | ||
| this.currentIndex++; | ||
| this.displayList(); | ||
| } | ||
| } | ||
|
|
||
| private navigateUp() { | ||
| if (this.currentIndex > 0) { | ||
| this.currentIndex--; | ||
| this.displayList(); | ||
| } | ||
| } | ||
|
|
||
| private toggleView() { | ||
| if (this.isDetailedView) { | ||
| this.isDetailedView = false; | ||
| this.displayList(); | ||
| } else { | ||
| this.isDetailedView = true; | ||
| this.selectedIndex = this.currentIndex; | ||
| this.displayDetailedView(); | ||
| } | ||
| } | ||
|
|
||
| private quitOrBack() { | ||
| if (this.isDetailedView) { | ||
| this.isDetailedView = false; | ||
| this.displayList(); | ||
| } else { | ||
| this.exit(); | ||
| } | ||
| } | ||
|
|
||
| private exit() { | ||
| if (process.stdin.isTTY) process.stdin.setRawMode(false); | ||
| process.stdin.removeAllListeners("keypress"); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| private listenForNewHistoryItem() { | ||
| events.on(TCustomEvents.NEW_HISTORY_ITEM, (newRecord: HistoryRecord) => { | ||
| HistoryRepository.getInstance().addRecord(newRecord); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| export default HistoryCommand; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { EventEmitter } from "events"; | ||
| import path from "path"; | ||
| import sqlite3 from "sqlite3"; | ||
| import { type HistoryRecord } from "../../../types"; | ||
|
|
||
| class HistoryRepository { | ||
| private static instance: HistoryRepository; | ||
| private db: sqlite3.Database; | ||
|
|
||
| private constructor() { | ||
| const dbPath = path.resolve(__dirname, "../../history.sqlite"); | ||
| this.db = new sqlite3.Database(dbPath); | ||
| this.db.serialize(() => { | ||
| this.db.run(`CREATE TABLE IF NOT EXISTS history ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| source TEXT NOT NULL, | ||
| destination TEXT NOT NULL, | ||
| timestamp TEXT NOT NULL, | ||
| size INTEGER NOT NULL | ||
| )`); | ||
| }); | ||
| } | ||
|
|
||
| public static getInstance(): HistoryRepository { | ||
| if (!HistoryRepository.instance) { | ||
| HistoryRepository.instance = new HistoryRepository(); | ||
| } | ||
| return HistoryRepository.instance; | ||
| } | ||
|
|
||
| public addRecord(record: HistoryRecord): Promise<void> { | ||
| return new Promise((resolve, reject) => { | ||
| this.db.run( | ||
| `INSERT INTO history (source, destination, timestamp, size) VALUES (?, ?, ?, ?)`, | ||
| [record.source, record.destination, record.timestamp, record.size], | ||
| (err: any) => { | ||
| if (err) reject(err); | ||
| else resolve(); | ||
| }, | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| public getAllRecords(): Promise<HistoryRecord[]> { | ||
| return new Promise((resolve, reject) => { | ||
| console.log("Fetching all history records..."); | ||
| this.db.all( | ||
| `SELECT * FROM history ORDER BY timestamp`, | ||
| [], | ||
| (err: any, rows: HistoryRecord[] | PromiseLike<HistoryRecord[]>) => { | ||
| if (err) reject(err); | ||
| else resolve(rows); | ||
| }, | ||
| ); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| export default HistoryRepository; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { Command } from "commander"; | ||
| import type { CommandWrapper, TCommand } from "../../../types"; | ||
| import HistoryCommandLogic from "./history"; | ||
|
|
||
| export class HistoryCommand implements CommandWrapper { | ||
| command: TCommand; | ||
|
|
||
| constructor() { | ||
| const logic = new HistoryCommandLogic(); | ||
|
|
||
| this.command = new Command() | ||
| .name("history") | ||
| .description("Show copy history") | ||
| .action(async () => { | ||
| await logic.execute(); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.