Skip to content

Commit

Permalink
update push pop, delete table etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
shibisuriya committed Aug 11, 2024
1 parent 60e0e10 commit c606ec7
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 71 deletions.
7 changes: 4 additions & 3 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { push } from '../src/push.js'
import { end } from '../src/end.js'
import { jumpForward } from '../src/jump-forward.js'
import { jumpBackward } from '../src/jump-backward.js'
import { changeDirectory } from '../src/change-directory.js'

const program = new Command()

Expand All @@ -15,14 +16,14 @@ program
.version('0.0.1')

program
.command('push')
.command('change-directory')
.option(
'-s, --session-id <session-id>',
'The ID of the session to push changes to'
)
.option('-p, --path <path>', 'The path of the files or directory to push')
.description('Push a path and session ID onto the stack')
.action((options) => {
.action(async (options) => {
const { sessionId, path } = options
if (!sessionId) {
console.error('Error: The --session-id option is mandatory.')
Expand All @@ -33,7 +34,7 @@ program
console.error('Error: The --path option is mandatory.')
process.exit(1)
}
push(sessionId, path)
await changeDirectory({ sessionId, path })
})

program
Expand Down
22 changes: 22 additions & 0 deletions src/change-directory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { STACK_TYPE } from './constants.js'
import { push } from './push.js'
import { dropTable } from './drop-table.js'
import { getTableName } from './helpers.js'
import { db } from './db.js'

async function changeDirectory({ sessionId, path }) {
await push({
sessionId,
path,
stackType: STACK_TYPE.UNDO,
})
const tableName = getTableName({ sessionId, stackType: STACK_TYPE.REDO })
await dropTable(tableName)
db.close((err) => {
if (err) {
console.error('Error closing the database:', err.message)
}
})
}

export { changeDirectory }
7 changes: 6 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ const SUPPORTED_SHELLS = {
ZSH: 'zsh',
}

export { SUPPORTED_SHELLS }
const STACK_TYPE = {
UNDO: 'undo',
REDO: 'redo',
}

export { SUPPORTED_SHELLS, STACK_TYPE }
14 changes: 14 additions & 0 deletions src/drop-table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { db } from './db.js'

function dropTable(tableName) {
return new Promise((resolve, reject) => {
db.run(`DROP TABLE IF EXISTS ${tableName}`, (err) => {
if (err) {
reject(err)
}
resolve()
})
})
}

export { dropTable }
9 changes: 1 addition & 8 deletions src/end.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
import { main } from './test.js'

function end(sessionId) {
console.log('ending ', sessionId)
main().catch((err) => {
console.error('Error:', err.message)
})
}
function end(sessionId) {}

export { end }
5 changes: 5 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const getTableName = ({ stackType, sessionId }) => {
return `${stackType}_stack_${sessionId}`
}

export { getTableName }
2 changes: 1 addition & 1 deletion src/init/scripts/zsh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function go_forward() {
}

function chpwd() {
dir-bunny push -s "$$" -p "$PWD"
dir-bunny change-directory -s "$$" -p "$PWD"
}


Expand Down
15 changes: 13 additions & 2 deletions src/jump-backward.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
function jumpBackward(sessionId) {
console.log('Jumping 1 set back! ', sessionId)
import { STACK_TYPE } from './constants.js'
import { pop } from './pop.js'
import { push } from './push.js'
import { db } from './db.js'

async function jumpBackward(sessionId) {
const poppedPath = await pop({ sessionId, stackType: STACK_TYPE.UNDO })
push({ sessionId, path: poppedPath, stackType: STACK_TYPE.REDO })
db.close((err) => {
if (err) {
console.error('Error closing the database:', err.message)
}
})
}

export { jumpBackward }
15 changes: 13 additions & 2 deletions src/jump-forward.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
function jumpForward(sessionId) {
console.log('Jumping 1 step forward! ', sessionId)
import { pop } from './pop.js'
import { push } from './push.js'
import { STACK_TYPE } from './constants.js'
import { db } from './db.js'

async function jumpForward(sessionId) {
const poppedPath = await pop({ sessionId, stackType: STACK_TYPE.REDO })
push({ sessionId, path: poppedPath, stackType: STACK_TYPE.UNDO })
db.close((err) => {
if (err) {
console.error('Error closing the database:', err.message)
}
})
}

export { jumpForward }
58 changes: 58 additions & 0 deletions src/pop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { db } from './db.js'
import { getTableName } from './helpers.js'

async function pop({ sessionId, stackType }) {
const tableName = getTableName({ stackType, sessionId })

return new Promise((resolve, reject) => {
db.serialize(() => {
console.log('Popping from, ', tableName)
db.get(
`
SELECT id, path
FROM ${tableName}
ORDER BY id DESC
LIMIT 1
`,
(err, row) => {
if (err) {
reject(err)
}

db.run('BEGIN TRANSACTION', (err) => {
if (err) {
reject(err)
}
const { id } = row
db.run(
`DELETE FROM ${tableName} WHERE id = ?`,
[id],
function (err) {
if (err) {
db.run('ROLLBACK', (rollbackErr) => {
if (rollbackErr) {
reject(rollbackErr)
}
})
reject(err)
}

db.run('COMMIT', (commitErr) => {
if (commitErr) {
reject(commitErr)
db.run('ROLLBACK', (rollbackErr) => {
reject(rollbackErr)
})
}
resolve(row.path)
})
}
)
})
}
)
})
})
}

export { pop }
102 changes: 48 additions & 54 deletions src/push.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,59 @@
import { STACK_TYPE } from './constants.js'
import { db } from './db.js'
import { getTableName } from './helpers.js'

function push(sessionId, path) {
// Clear the redo stack if present!

db.serialize(() => {
const tableName = `undo_stack_${sessionId}`
// Create a table in the db for each shell instance, so
// that we can simply drop the table if a shell instance is
// killed.
db.run(
`CREATE TABLE IF NOT EXISTS ${tableName} (
async function push({ sessionId, path, stackType = STACK_TYPE.UNDO }) {
return new Promise((resolve, reject) => {
db.serialize(() => {
const tableName = getTableName({ stackType, sessionId })
// Create a table in the db for each shell instance, so
// that we can simply drop the table if a shell instance is
// killed.
db.run(
`CREATE TABLE IF NOT EXISTS ${tableName} (
id INTEGER PRIMARY KEY AUTOINCREMENT,
path TEXT NOT NULL
)`,
(err) => {
if (err) {
console.error('Error creating table:', err.message)
} else {
const stmt = db.prepare(
`INSERT INTO ${tableName} (path) VALUES (?)`
)
stmt.run(path, (err) => {
if (err) {
console.error('Error inserting data:', err.message)
} else {
console.log('Data inserted successfully.')

// Retrieve and print the contents of the table
db.all(
`SELECT * FROM ${tableName};`,
(err, rows) => {
if (err) {
console.error(
`Error retrieving data from ${tableName}:`,
err.message
)
} else {
console.log(
`Contents of table ${tableName}:`
)
console.log(rows)
}
(err) => {
if (err) {
reject(err)
} else {
const stmt = db.prepare(
`INSERT INTO ${tableName} (path) VALUES (?)`
)
stmt.run(path, (err) => {
if (err) {
reject(err)
} else {
// Retrieve and print the contents of the table
db.all(
`SELECT * FROM ${tableName};`,
(err, rows) => {
if (err) {
console.error(
`Error retrieving data from ${tableName}:`,
err.message
)
} else {
console.log(
`Contents of table ${tableName}:`
)
console.log(rows)
}

// Finalize the statement and close the database
stmt.finalize(() => {
db.close((err) => {
if (err) {
console.error(
'Error closing the database:',
err.message
)
}
// Finalize the statement and close the database
stmt.finalize(() => {
resolve()
})
})
}
)
}
})
}
)
resolve()
}
})
}
}
}
)
)
})
})
}

Expand Down

0 comments on commit c606ec7

Please sign in to comment.