Added option to open repository from command line

Fixes https://github.com/JetpackDuba/Gitnuro/issues/51
This commit is contained in:
Abdelilah El Aissaoui 2022-12-17 03:54:31 +01:00
parent ec6700dacf
commit 52539c2692
2 changed files with 60 additions and 7 deletions

View File

@ -25,6 +25,7 @@ import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberWindowState import androidx.compose.ui.window.rememberWindowState
import com.jetpackduba.gitnuro.di.DaggerAppComponent import com.jetpackduba.gitnuro.di.DaggerAppComponent
import com.jetpackduba.gitnuro.extensions.preferenceValue import com.jetpackduba.gitnuro.extensions.preferenceValue
import com.jetpackduba.gitnuro.extensions.systemSeparator
import com.jetpackduba.gitnuro.extensions.toWindowPlacement import com.jetpackduba.gitnuro.extensions.toWindowPlacement
import com.jetpackduba.gitnuro.logging.printLog import com.jetpackduba.gitnuro.logging.printLog
import com.jetpackduba.gitnuro.preferences.AppSettings import com.jetpackduba.gitnuro.preferences.AppSettings
@ -37,7 +38,10 @@ import com.jetpackduba.gitnuro.ui.components.TabInformation
import com.jetpackduba.gitnuro.ui.components.emptyTabInformation import com.jetpackduba.gitnuro.ui.components.emptyTabInformation
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.io.File
import java.nio.file.Paths
import javax.inject.Inject import javax.inject.Inject
private const val TAG = "App" private const val TAG = "App"
@ -59,8 +63,10 @@ class App {
private val tabsFlow = MutableStateFlow<List<TabInformation>>(emptyList()) private val tabsFlow = MutableStateFlow<List<TabInformation>>(emptyList())
fun start() { fun start(args: Array<String>) {
val windowPlacement = appSettings.windowPlacement.toWindowPlacement val windowPlacement = appSettings.windowPlacement.toWindowPlacement
val dirToOpen = getDirToOpen(args)
var defaultSelectedTabKey = 0
appStateManager.loadRepositoriesTabs() appStateManager.loadRepositoriesTabs()
@ -75,6 +81,9 @@ class App {
loadTabs() loadTabs()
if (dirToOpen != null)
defaultSelectedTabKey = addDirTab(dirToOpen)
application { application {
var isOpen by remember { mutableStateOf(true) } var isOpen by remember { mutableStateOf(true) }
val theme by appSettings.themeState.collectAsState() val theme by appSettings.themeState.collectAsState()
@ -109,7 +118,7 @@ class App {
customTheme = customTheme, customTheme = customTheme,
) { ) {
Box(modifier = Modifier.background(MaterialTheme.colors.background)) { Box(modifier = Modifier.background(MaterialTheme.colors.background)) {
AppTabs() AppTabs(defaultSelectedTabKey)
} }
} }
} }
@ -122,6 +131,31 @@ class App {
} }
} }
private fun addDirTab(dirToOpen: File): Int {
var defaultSelectedTabKey = 0
tabsFlow.update {
val newList = it.toMutableList()
val absolutePath = dirToOpen.normalize().absolutePath
.removeSuffix(systemSeparator)
.removeSuffix("$systemSeparator.git")
val newKey = it.count()
val existingIndex = newList.indexOfFirst { repo -> repo.path?.removeSuffix(systemSeparator) == absolutePath }
defaultSelectedTabKey = if(existingIndex == -1) {
newList.add(newAppTab(key = newKey, path = absolutePath))
newKey
} else {
existingIndex
}
newList
}
return defaultSelectedTabKey
}
private fun loadTabs() { private fun loadTabs() {
val repositoriesSavedTabs = appStateManager.openRepositoriesPathsTabs val repositoriesSavedTabs = appStateManager.openRepositoriesPathsTabs
var repoTabs = repositoriesSavedTabs.map { repositoryTab -> var repoTabs = repositoriesSavedTabs.map { repositoryTab ->
@ -144,10 +178,10 @@ class App {
@Composable @Composable
fun AppTabs() { fun AppTabs(defaultSelectedTabKey: Int) {
val tabs by tabsFlow.collectAsState() val tabs by tabsFlow.collectAsState()
val tabsInformationList = tabs.sortedBy { it.key } val tabsInformationList = tabs.sortedBy { it.key }
val selectedTabKey = remember { mutableStateOf(0) } val selectedTabKey = remember { mutableStateOf(defaultSelectedTabKey) }
Column( Column(
modifier = Modifier.background(MaterialTheme.colors.background) modifier = Modifier.background(MaterialTheme.colors.background)
@ -228,6 +262,25 @@ class App {
appComponent = appComponent, appComponent = appComponent,
) )
} }
fun getDirToOpen(args: Array<String>): File? {
if (args.isNotEmpty()) {
val repoToOpen = args.first()
val path = Paths.get(repoToOpen)
val repoDir = if (!path.isAbsolute)
File(System.getProperty("user.dir"), repoToOpen)
else
path.toFile()
return if (repoDir.isDirectory)
repoDir
else
null
}
return null
}
} }
@Composable @Composable

View File

@ -1,6 +1,6 @@
package com.jetpackduba.gitnuro package com.jetpackduba.gitnuro
fun main() { fun main(args: Array<String>) {
val main = App() val app = App()
main.start() app.start(args)
} }